Java Jersey 在 POST 上使用 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19083873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jersey consume JSON on POST
提问by cherit
I am trying to send some json data via jquery POST to a jersey REST service in my local machine.
我正在尝试通过 jquery POST 将一些 json 数据发送到我本地机器上的 jersey REST 服务。
In my server side, I have Jersey method to consume this JSON which is POSTed.
在我的服务器端,我有 Jersey 方法来使用这个 POSTed 的 JSON。
@Path("/question")
public class QuestionAPI {
private final static Logger LOGGER = Logger.getLogger(HelloWorldApi.class .getName());
@POST
@Path("/askquestion")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public TQARequest askquestion(TQARequest tqaRequest, @Context HttpServletRequest request) {
LOGGER.info("Inside-->askquestion-->TQARequest"+tqaRequest.getQuestion());
return tqaRequest;
}
}
I am wrapping the json data in request. So that in server , I can get all data sent in request in that wrapper class. My wrapper class for request is
我正在请求中包装 json 数据。这样在 server 中,我可以在该包装类中获取请求中发送的所有数据。我的请求包装类是
public class TQARequest {
private Question question;
public Question getQuestion() {
return question;
}
public void setQuestion(Question question) {
this.question = question;
}
@Override
public String toString() {
return "TQARequest [question=" + question + "]";
}
}
The Question pojo class
问题 pojo 类
public class Question {
@Id
private Long questionID;
private String questionText;
private long createdOn;
private String questionURL;
private String questionTrackingURL;
@Override
public String toString() {
return "Question [questionID=" + questionID + ", questionText="
+ questionText + ", createdOn=" + createdOn + ", questionURL="
+ questionURL + ", questionTrackingURL=" + questionTrackingURL
+ "]";
}
public Question(String questionText, long createdOn, String questionURL,
String questionTrackingURL) {
super();
this.questionText = questionText;
this.createdOn = createdOn;
this.questionURL = questionURL;
this.questionTrackingURL = questionTrackingURL;
}
public Long getQuestionID() {
return questionID;
}
public void setQuestionID(Long questionID) {
this.questionID = questionID;
}
public String getQuestionText() {
return questionText;
}
public void setQuestionText(String questionText) {
this.questionText = questionText;
}
public long getCreatedOn() {
return createdOn;
}
public void setCreatedOn(long createdOn) {
this.createdOn = createdOn;
}
public String getQuestionURL() {
return questionURL;
}
public void setQuestionURL(String questionURL) {
this.questionURL = questionURL;
}
public String getQuestionTrackingURL() {
return questionTrackingURL;
}
public void setQuestionTrackingURL(String questionTrackingURL) {
this.questionTrackingURL = questionTrackingURL;
}
public Question(){
}
}
Whenever I make a request from the jquery as shown below ,
每当我从 jquery 发出请求时,如下所示,
function askQuestion(){
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/api/question/askquestion",
data:
JSON.stringify({
"tqaRequest" : {
"question" : {
"createdOn" : "sfddsf",
"questionText" : "fsdfsd",
"questionTrackingURL" : "http://www.google.com",
"questionURL" : "ssdf"
}
}
}
),
dataType: "json",
success: function(response){
console.log(response);
}
});
}
I get this error in the console :
我在控制台中收到此错误:
WARNING: /api/question/askquestion: org.codehaus.Hymanson.map.exc.UnrecognizedPropertyException: Unrecognized field "tqaRequest" (Class com.netsquid.tqa.entity.TQARequest), not marked as ignorable
at [Source: org.mortbay.jetty.HttpParser$Input@899e3e; line: 1, column: 16] (through reference chain: com.netsquid.tqa.entity.TQARequest["tqaRequest"])
I can fix this by sending Question json from jquery and accepting question parameter in the method. But I need to wrap all the jquery requests in TQARequest and accept all the request as TQARequest and then extract the question object from it. How do I do this ?
我可以通过从 jquery 发送 Question json 并在方法中接受 question 参数来解决这个问题。但是我需要将所有 jquery 请求包装在 TQARequest 中并将所有请求作为 TQARequest 接受,然后从中提取问题对象。我该怎么做呢 ?
My POJO mapping in web.xml is:
我在 web.xml 中的 POJO 映射是:
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
采纳答案by Michael Doyle
I believe you can simplify the JSON document as follows:
我相信您可以将 JSON 文档简化如下:
{
"question" : {
"createdOn" : "sfddsf",
"questionText" : "fsdfsd",
"questionTrackingURL" : "http://www.google.com",
"questionURL" : "ssdf"
}
}
It's still a "tqaRequest" object in this form.
它仍然是这种形式的“tqaRequest”对象。
If you want to support a list of questions, your JSON might look like this (JSON arrays go inside square brackets):
如果您想支持问题列表,您的 JSON 可能如下所示(JSON 数组位于方括号内):
{
"questions" : [
{
"createdOn" : "date 1",
"questionText" : "question 1",
"questionTrackingURL" : "http://www.google.com",
"questionURL" : "question 1 url"
},
{
"createdOn" : "date 2",
"questionText" : "question 2",
"questionTrackingURL" : "http://www.google.com",
"questionURL" : "question 2 url"
}]
}
}
And you would adjust your TQARequest class to reference:
你会调整你的 TQARequest 类来引用:
private List<Question> questions;
instead of
代替
private Question question;
回答by Syed Shahul
Hope this solves the problem.
希望这能解决问题。
@POST
@Path("/askquestion")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public TQARequest askquestion(String jsonRequest){
TQARequest tqaRequest = MapperUtil
.readAsObjectOf(TQARequest.class, jsonRequest);
}
MapperUtil.java
映射器实用程序
com.fasterxml.Hymanson.databind.ObjectMapper MAPPER = new ObjectMapper();
public static <T> T readAsObjectOf(Class<T> clazz, String value)
throws MYPException {
try {
return MAPPER.readValue(value, clazz);
} catch (Exception e) {
LOGGER.error("{}, {}", e.getMessage(), e.fillInStackTrace());
}
}