java - Java Rest API JAX-RS 中的多个参数 - GET 方法

标签 java rest tomcat jax-rs

我想将多个参数传递到我的方法中。我该怎么做呢?我希望 url 看起来像这样 http://host/one/two/three/four

到目前为止我有以下代码

@GET
@Produces({MediaType.APPLICATION_JSON})
@Path("/{one,two,three}") 

public List<Person> getPeople(@PathParam ("one") String one, @PathParam ("two") String two, @PathParam ("three") String three){
   String one = one; 
   String two = two; 
   String three = three;

}

这是获取参数并将其传递给我的方法的正确语法吗?我见过 @Path 中使用的一些正则表达式,但我不明白。老实说,我真的只是希望能够获取参数并在可能的情况下将它们放入变量中。

最佳答案

固定数量的路径参数:

@GET
@Path("/{one}/{two}/{three}")
@Produces(MediaType.APPLICATION_JSON)
public Response foo(@PathParam("one") String one,
                    @PathParam("two") String two,
                    @PathParam("three") String three) {

    ...
}

可变数量路径参数:

@GET
@Path("/{path: .+}")
@Produces(MediaType.APPLICATION_JSON)
public Response foo(@PathParam("path") String path) {

    String[] paths = path.split("/");

    ...
}

关于java - Java Rest API JAX-RS 中的多个参数 - GET 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38745745/

相关文章:

java - 未指定 @DefaultValue 时 @QueryParam 的默认值是什么?

java - 无法在 Java 中启动 Tomcat

java - 如何找到所有 "(("并将它们替换为 "("?

休息 : return 'method not allowed' or not do the method?

java - 在结果中显示 JMH StackProfiler 摘要

java - 如何使用 Jersey ContainerRequestFilter 和 ContainerResponseFilter 匹配 http 请求和响应

java - 使用 'webapp-runner' 运行 Spring boot 应用程序后,它停止在一行 "INFO: Starting ProtocolHandler ["http-nio-808 0"]"

tomcat - 尝试使用智能卡向 Tomcat 进行身份验证

java - 存储库找到 3 条记录,结果列表返回 6 条记录

java - 如何在没有此 ClassNotFoundException 的情况下连接到 Java 中的本地 SQLite?