[英]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.