【问题标题】:Unable to read path parameters when POST body is added添加 POST 正文时无法读取路径参数
【发布时间】:2016-11-25 14:29:46
【问题描述】:

当我添加 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
}

我发布到 /test/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);

【问题讨论】:

  • bookId 和评论变量在方法执行时不被重视?
  • 带有 book id 和 comment 的代码是上面 resteasy 链接中的一个示例。我的代码有参数 a、b 和 pojo
  • 我明白了。但是当方法被执行时你的路径参数是否没有被赋值(null)?
  • 当我的方法测试被执行时,变量“a”具有 POST 主体的值(应该去 pojo)。变量“b”和“pojo”为空。
  • 你的代码看起来不错。你能展示进口吗?问题可能就在那里。

标签: java rest wildfly resteasy


【解决方案1】:

谢谢@davidhxxx,我的问题是 PathParam 的导入不正确。

我有 import javax.websocket.server.PathParam; 而不是 import javax.ws.rs.PathParam;

【讨论】:

    猜你喜欢
    • 2013-06-19
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2018-09-10
    • 2015-11-17
    • 2012-07-02
    相关资源
    最近更新 更多