【问题标题】:Jersey application/json to parametersJersey 应用程序/json 到参数
【发布时间】:2014-02-27 02:12:17
【问题描述】:

我想使用 JSON 从我的客户端应用程序/json 请求发送

{ "content" : "My content", "question" : "WHY?" }

Jersey rest 中是否有某种方法可以在服务器端检索此 JSON,例如:

@POST
@Path("/myMethod")
@Consumes(MediaType.APPLICATION_JSON)
public void myServerMethod(@FormParam("content") String content, @FormParam("question") String question) {
         System.out.println(content+" "+question);
}

现在我无法从 json 的正文中检索值。

最好的问候

【问题讨论】:

标签: java json rest jersey


【解决方案1】:

首先,创建一个代表您的帖子数据的对象:

public class MyPostData
{
    private String content;
    private String question;

    // getters and setters here
}

然后,将资源方法更改为:

@POST
@Path("/myMethod")
@Consumes(MediaType.APPLICATION_JSON)
public void myServerMethod(MyPostData data)
{
    System.out.println(data.getContent() + " " + data.getQuestion());
}

【讨论】:

  • 谢谢。我知道,我可以使用类来检索应用程序/json。但我不想为此创建单独的类。
  • 嗯,好的。您是否使用 Jackson、Moxy 或 Jettison 作为 Jersey 的 JSON 库?如果是这样,您可以将第一个方法参数设为 JsonObject(根据您使用的 json 库而有所不同)并从中提取字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-12
  • 1970-01-01
  • 2017-02-04
  • 2016-02-21
  • 2014-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多