当我添加 POST 正文时,无法读取路径参数。
public class POJO {
public int id;
public void setId(int id){
this.id = id;
}
}
...
@POST
@Path("/test/{a}/{b}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public Response test(@PathParam("a") String a, @PathParam("b") String b, POJO pojo){
// a has the value of the POST body
// b is empty
// pojo is null
}
我发布到 /测试/x/y
正文:
{
"id" : 1
}
标题:
Content-Type : application/json
我查看了示例 https://docs.jboss.org/resteasy/2.0.0.GA/userguide/html_single/ 并且不明白为什么我无法读取路径参数。 这是一个 JBoss 示例:
@POST
@Path("book/{id}/comments")
public void addComment(@PathParam("id") String bookId, Comment comment);
最佳答案
谢谢@davidhxxx,我的问题是 PathParam 的导入不正确。
我有import javax.websocket.server.PathParam;
而不是import javax.ws.rs.PathParam;
关于java - 添加 POST 正文时无法读取路径参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38513682/