Web services @QueryParam未设置值

Web services @QueryParam未设置值,web-services,jax-rs,Web Services,Jax Rs,我有一个web服务,如下所示: @POST @Path("/push") @Consumes(MediaType.APPLICATION_JSON) public String push(@QueryParam("comment") String comment, @QueryParam("type") String type){ // do something } http://<HOST:port>/push?comment=co

我有一个web服务,如下所示:

@POST
@Path("/push")
@Consumes(MediaType.APPLICATION_JSON)
public String push(@QueryParam("comment") String comment,
                   @QueryParam("type") String type){
     // do something
}
http://<HOST:port>/push?comment=comment1&type=type1
我的请求机构是:

{
    "comment" : "comment1",
    "type" : "type1"
}

当发出适当的post请求但参数
comment
type
具有
null
值时,将调用My web服务。这里出了什么问题?

您将查询参数与Post请求负载混淆了

查询参数从请求URI查询参数中提取,并通过在方法参数参数中使用javax.ws.rs.QueryParam注释来指定

查询参数是可选的,如果不设置它们,它们将被设置为null

如果要保留“comment&type”作为查询参数,则应按如下方式传递它们:

@POST
@Path("/push")
@Consumes(MediaType.APPLICATION_JSON)
public String push(@QueryParam("comment") String comment,
                   @QueryParam("type") String type){
     // do something
}
http://<HOST:port>/push?comment=comment1&type=type1
您的rest API入口点如下所示:

@POST
@Path("/push")
@Consumes(MediaType.APPLICATION_JSON)
public String push(MyMessage message){
     String comment = message.getComment()
     String type = message.getType()
     // do something
}

非常感谢你。在你提供的第二种方法中可以使用多个参数吗?简短回答:不!您可以做的是:接受包含您需要的所有参数(包括要添加的第二个参数)的对象,或者接受对象列表,例如:list。显然,您需要相应地调整JSON负载。