I am trying to send some json data via jquery POST to a jersey REST service in my local machine.
In my server side, I have Jersey method to consume this JSON which is POSTed.
@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
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
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 ,
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.jackson.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 ?
My POJO mapping in web.xml is:
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
Jersey uses Jackson internally to convert Java objects to JSON and vice versa.
Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T> providers distributed with jersey-json module. These providers enable using three basic approaches when working with JSON format: POJO support.
I believe you can simplify the JSON document as follows:
{
"question" : {
"createdOn" : "sfddsf",
"questionText" : "fsdfsd",
"questionTrackingURL" : "http://www.google.com",
"questionURL" : "ssdf"
}
}
It's still a "tqaRequest" object in this form.
If you want to support a list of questions, your JSON might look like this (JSON arrays go inside square brackets):
{
"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:
private List<Question> questions;
instead of
private Question question;
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.jackson.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());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With