简体   繁体   English

如何在 Java API 的 URI 中使用与 Go 相同的 `/{id}/class` 动态 ID?

[英]How can I use `/{id}/class` dynamic id set in URI in Java APIs same as in Go?

I use gorilla mux for Go for creating the API's我为 Go 使用 gorilla mux 来创建 API

/{id}/class

and I can get the id using我可以使用

Vars := mux.Vars(r)["id"]

then how can I create the same API in Java Spring?那么如何在 Java Spring 中创建相同的 API?

I use spring to serve Java APIs我使用 spring 来服务 Java API

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.0.5.RELEASE</version>
</dependency>
server.createContext("/{id}/class", new Class());

the above doesn't work and how to do that?以上不起作用,如何做到这一点?

your question is somewhat misleading.你的问题有点误导。 I hope i have interpreted it correctly.我希望我已经正确解释了它。 I believe you want to define an API with path "/{id}/class", and fetch value of "{id}" in code.我相信您想用路径“/{id}/class”定义一个 API,并在代码中获取“{id}”的值。

If so, then it can achieved using below:如果是这样,则可以使用以下方法实现:

// Any request type
@Path("/{id}/class")
public void methodA(@PathParam("id") Integer idPassedInUrl) {
// code here
}

And to call this URL, just do it like "/1/class" or "/2/class".并且要调用这个 URL,就像“/1/class”或“/2/class”一样。

Let me know if you still face any issues.如果您仍然遇到任何问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确设置 Spring 安全匹配器来处理在 URI 中间包含 ID 的请求? - How can I correctly set a Spring Security matcher to handle request containing an ID in the middle of the URI? 如何单击具有相同名称,没有ID和相同类别的2个链接中的1个。 硒java - How do I click 1 of 2 links with same name, no id and same class. selenium java 我应该如何构造这个 ID class? - JAVA - How should I construct this ID class? - JAVA 我如何比较ID与Java的值 - How i can compare the value of an id with java 如何在java中获取会话ID - How can i get session id in java 使用getElementById运行JavascriptExecutor时如何使用动态的ID字段 - How can I use an ID field which is dynamic when running JavascriptExecutor with getElementById 如何生成在java类中创建的ID到r.java - How to generate ID i created in java class to r.java 使用Java创建自定义类,保留ID还是可以在自己的类中使用ID? - Creating a custom class in Java, is id reserved or can you use id in your own classes? 帧 ID 动态变化 - 如何使用 java 收集那些 id selenium webdriver - Frame ID dynamic changes - how can collect those id selenium webdriver with java 如何获取 MainActivity.java 类文件之外的任何小部件的 ID? - How can i get ID of any widget outside the MainActivity.java class file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM