【问题标题】:How to annotate jersey method if I want to pass json?如果我想传递 json,如何注释 jersey 方法?
【发布时间】:2015-01-22 08:42:17
【问题描述】:

我有以下球衣方法声明:

    @POST
    @Path("/fooPath")
    @Produces({MediaType.APPLICATION_JSON})
    @Consumes({MediaType.APPLICATION_JSON})
    public Response isSellableOnline(@FormParam("productCodes") final List<String> productCodes,
                                     @FormParam("storeName") final String storeName,
                                     @Context HttpServletRequest request) {

在休息客户端中,我尝试像这样调用以下方法:

当我调试方法时,我看到接收到的参数为空:

如何重写方法声明?

【问题讨论】:

  • 服务器日志中一定有内容。也发一下。
  • 你是对的。现在我将改写问题。
  • @Gimby 主题已更新。

标签: java json rest jersey form-parameter


【解决方案1】:

这是因为在 isSellableOnlie 方法上您期望或尝试提取表单参数,但传入的 POST 请求是 JSON。

如果你想要 JSON,你应该让 POJO 类能够序列化 JSON。

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Store {

private String storeName;
private List<String> productCodes;

public Store() {
}

public String getName() {
    return name;
}

public List<String> getProductCodes() {
    return productCodes;
}
}

然后在你的方法中:

@POST
@Path("/fooPath")
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
public Response isSellableOnline(Store store) {
   store.getName();
...
}

【讨论】:

  • 如何重写我的方法?
猜你喜欢
  • 2016-07-22
  • 1970-01-01
  • 2020-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-24
  • 1970-01-01
  • 2011-12-08
相关资源
最近更新 更多