Java REST to convert JSON to POJO help needed.

Acid0

Executive Member
Joined
Feb 10, 2009
Messages
5,889
Reaction score
2,734
Location
Johannesburg
Hi all java gurus.

I need some help please.
We are starting to interface with web service from our side and I have started to look into the java side of the web service.

For now luckily just need to consume and at a later stage need to make POST and PUT calls (But that is for a later stage(for now its just GET request I need to worry about))

I have looked into the GSON and retrofit2 side and it seems that both should cater for what I need to do.
I will have a Base url and Uri that takes some parameters and one or two headers to make the call.

So far i have achieved of doing the above and actually get a response back in JSON as below

JSON in second post


As you can see this is just one record and multiple Contact details along with its child records could come through but for now I only focus on one main record and some children records attached.

Following the JSON2POJO
The following classes were created.
Code:
package rest;

import java.util.List;

public class CaptureQuestion {

private Integer captureId;
private Integer questionId;
private Integer questionTypeId;
private String questionTitle;
private List<Object> questionsOptions = null;
private Boolean required;
private String answer;

public Integer getCaptureId() {
return captureId;
}

public void setCaptureId(Integer captureId) {
this.captureId = captureId;
}

public Integer getQuestionId() {
return questionId;
}

public void setQuestionId(Integer questionId) {
this.questionId = questionId;
}

public Integer getQuestionTypeId() {
return questionTypeId;
}

public void setQuestionTypeId(Integer questionTypeId) {
this.questionTypeId = questionTypeId;
}

public String getQuestionTitle() {
return questionTitle;
}

public void setQuestionTitle(String questionTitle) {
this.questionTitle = questionTitle;
}

public List<Object> getQuestionsOptions() {
return questionsOptions;
}

public void setQuestionsOptions(List<Object> questionsOptions) {
this.questionsOptions = questionsOptions;
}

public Boolean getRequired() {
return required;
}

public void setRequired(Boolean required) {
this.required = required;
}

public String getAnswer() {
return answer;
}

public void setAnswer(String answer) {
this.answer = answer;
}

}
-----------------------------------rest.ContactQuestion.java-----------------------------------

package rest;

import java.util.List;

public class ContactQuestion {

private Integer questionId;
private Integer questionTypeId;
private String questionTitle;
private List<QuestionsOption> questionsOptions = null;
private Boolean required;
private String answer;

public Integer getQuestionId() {
return questionId;
}

public void setQuestionId(Integer questionId) {
this.questionId = questionId;
}

public Integer getQuestionTypeId() {
return questionTypeId;
}

public void setQuestionTypeId(Integer questionTypeId) {
this.questionTypeId = questionTypeId;
}

public String getQuestionTitle() {
return questionTitle;
}

public void setQuestionTitle(String questionTitle) {
this.questionTitle = questionTitle;
}

public List<QuestionsOption> getQuestionsOptions() {
return questionsOptions;
}

public void setQuestionsOptions(List<QuestionsOption> questionsOptions) {
this.questionsOptions = questionsOptions;
}

public Boolean getRequired() {
return required;
}

public void setRequired(Boolean required) {
this.required = required;
}

public String getAnswer() {
return answer;
}

public void setAnswer(String answer) {
this.answer = answer;
}

}
-----------------------------------rest.ListCaptureFrom.java-----------------------------------

package rest;

import java.util.List;

public class ListCaptureFrom {

private List<ListCaptureFrom_> listCaptureFrom = null;
private String status;
private Integer errorCode;
private String errorText;

public List<ListCaptureFrom_> getListCaptureFrom() {
return listCaptureFrom;
}

public void setListCaptureFrom(List<ListCaptureFrom_> listCaptureFrom) {
this.listCaptureFrom = listCaptureFrom;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public Integer getErrorCode() {
return errorCode;
}

public void setErrorCode(Integer errorCode) {
this.errorCode = errorCode;
}

public String getErrorText() {
return errorText;
}

public void setErrorText(String errorText) {
this.errorText = errorText;
}

}
-----------------------------------rest.ListCaptureFrom_.java-----------------------------------

package rest;

import java.util.List;

public class ListCaptureFrom_ {

private Integer formDataId;
private Integer establishmentId;
private Integer captureId;
private String captureTitle;
private String latitude;
private String longitude;
private String contactMasterId;
private Integer contactId;
private String contactQuestionsId;
private Integer appUserId;
private String createDate;
private List<ContactQuestion> contactQuestions = null;
private List<CaptureQuestion> captureQuestions = null;

public Integer getFormDataId() {
return formDataId;
}

public void setFormDataId(Integer formDataId) {
this.formDataId = formDataId;
}

public Integer getEstablishmentId() {
return establishmentId;
}

public void setEstablishmentId(Integer establishmentId) {
this.establishmentId = establishmentId;
}

public Integer getCaptureId() {
return captureId;
}

public void setCaptureId(Integer captureId) {
this.captureId = captureId;
}

public String getCaptureTitle() {
return captureTitle;
}

public void setCaptureTitle(String captureTitle) {
this.captureTitle = captureTitle;
}

public String getLatitude() {
return latitude;
}

public void setLatitude(String latitude) {
this.latitude = latitude;
}

public String getLongitude() {
return longitude;
}

public void setLongitude(String longitude) {
this.longitude = longitude;
}

public String getContactMasterId() {
return contactMasterId;
}

public void setContactMasterId(String contactMasterId) {
this.contactMasterId = contactMasterId;
}

public Integer getContactId() {
return contactId;
}

public void setContactId(Integer contactId) {
this.contactId = contactId;
}

public String getContactQuestionsId() {
return contactQuestionsId;
}

public void setContactQuestionsId(String contactQuestionsId) {
this.contactQuestionsId = contactQuestionsId;
}

public Integer getAppUserId() {
return appUserId;
}

public void setAppUserId(Integer appUserId) {
this.appUserId = appUserId;
}

public String getCreateDate() {
return createDate;
}

public void setCreateDate(String createDate) {
this.createDate = createDate;
}

public List<ContactQuestion> getContactQuestions() {
return contactQuestions;
}

public void setContactQuestions(List<ContactQuestion> contactQuestions) {
this.contactQuestions = contactQuestions;
}

public List<CaptureQuestion> getCaptureQuestions() {
return captureQuestions;
}

public void setCaptureQuestions(List<CaptureQuestion> captureQuestions) {
this.captureQuestions = captureQuestions;
}

}
-----------------------------------rest.QuestionsOption.java-----------------------------------

package rest;


public class QuestionsOption {

private Integer questionId;
private Integer optionId;
private String optionName;
private String optionValue;

public Integer getQuestionId() {
return questionId;
}

public void setQuestionId(Integer questionId) {
this.questionId = questionId;
}

public Integer getOptionId() {
return optionId;
}

public void setOptionId(Integer optionId) {
this.optionId = optionId;
}

public String getOptionName() {
return optionName;
}

public void setOptionName(String optionName) {
this.optionName = optionName;
}

public String getOptionValue() {
return optionValue;
}

public void setOptionValue(String optionValue) {
this.optionValue = optionValue;
}

}
Use this tool offline:Maven pluginGradle pluginAnt taskCLIJava API
Reference

Could some one please explain using the above how to actually make the conversion from json to the pojo's in the main class?

Any help would be appreciated
 
JSON
{
"ListCaptureFrom": [
{
"FormDataId": 0,
"EstablishmentId": 18400,
"CaptureId": 1052,
"CaptureTitle": "Today's Customer Visit",
"Latitude": "",
"Longitude": "",
"ContactMasterId": "0",
"ContactId": 390,
"ContactQuestionsId": "2570,2571,2572,2577,2574,2575,2576",
"AppUserId": 0,
"CreateDate": "0001-01-01T00:00:00",
"ContactQuestions": [
{
"QuestionId": 2570,
"QuestionTypeId": 4,
"QuestionTitle": "Name",
"QuestionsOptions": [],
"Required": true,
"Answer": ""
},
{
"QuestionId": 2571,
"QuestionTypeId": 4,
"QuestionTitle": "Surname",
"QuestionsOptions": [],
"Required": false,
"Answer": ""
},
{
"QuestionId": 2572,
"QuestionTypeId": 10,
"QuestionTitle": "Email",
"QuestionsOptions": [],
"Required": false,
"Answer": ""
},
{
"QuestionId": 2577,
"QuestionTypeId": 11,
"QuestionTitle": "Contact Number",
"QuestionsOptions": [],
"Required": true,
"Answer": ""
},
{
"QuestionId": 2574,
"QuestionTypeId": 4,
"QuestionTitle": "Store Name",
"QuestionsOptions": [],
"Required": false,
"Answer": ""
},
{
"QuestionId": 2575,
"QuestionTypeId": 21,
"QuestionTitle": "Select Retail Outlet",
"QuestionsOptions": [
{
"QuestionId": 2575,
"OptionId": 948,
"OptionName": "Customer 1",
"OptionValue": "Customer 1"
},
{
"QuestionId": 2575,
"OptionId": 949,
"OptionName": "Customer 2",
"OptionValue": "Customer 2"
},
{
"QuestionId": 2575,
"OptionId": 950,
"OptionName": "Customer 3",
"OptionValue": "Customer 3"
},
{
"QuestionId": 2575,
"OptionId": 951,
"OptionName": "Customer 4",
"OptionValue": "Customer 4"
},
{
"QuestionId": 2575,
"OptionId": 952,
"OptionName": "Customer 5",
"OptionValue": "Customer 6"
},
{
"QuestionId": 2575,
"OptionId": 953,
"OptionName": "Other",
"OptionValue": "Other"
}
],
"Required": true,
"Answer": ""
},
{
"QuestionId": 2576,
"QuestionTypeId": 4,
"QuestionTitle": "If Other, what is the Name if the outlet?",
"QuestionsOptions": [],
"Required": false,
"Answer": ""
}
],
"CaptureQuestions": [
{
"CaptureId": 1052,
"QuestionId": 25143,
"QuestionTypeId": 4,
"QuestionTitle": "Name",
"QuestionsOptions": [],
"Required": false,
"Answer": ""
},
{
"CaptureId": 1052,
"QuestionId": 25144,
"QuestionTypeId": 4,
"QuestionTitle": "Surname",
"QuestionsOptions": [],
"Required": false,
"Answer": ""
},
{
"CaptureId": 1052,
"QuestionId": 25145,
"QuestionTypeId": 10,
"QuestionTitle": "Email",
"QuestionsOptions": [],
"Required": false,
"Answer": ""
},
{
"CaptureId": 1052,
"QuestionId": 25146,
"QuestionTypeId": 11,
"QuestionTitle": "Contact Number",
"QuestionsOptions": [],
"Required": false,
"Answer": ""
},
{
"CaptureId": 1052,
"QuestionId": 25147,
"QuestionTypeId": 4,
"QuestionTitle": "Store Name",
"QuestionsOptions": [],
"Required": false,
"Answer": ""
}
]
}
],
"Status": "OK",
"ErrorCode": 0,
"ErrorText": ""
}
 
Why are you using Integer, Boolean etc instead of int, boolean...actually I don't care.

Use the Jackson library

EDIT: you're looking for marshaling/unmarshalling. I can't remember it out of my head but here is an example:

 
Paste in your JSON, select Java, change options if needed, copy the code.
 
Paste in your JSON, select Java, change options if needed, copy the code.

Didn’t know about this. Thanks!
 
Good golly that looks like a lot of code for something that won't be 10 lines in php. :)
 
Good golly that looks like a lot of code for something that won't be 10 lines in php. :)

It's overkill. You can use that same library (fasterxml/Jackson) and write two short generic functions that will work for all classes.
 
Good golly that looks like a lot of code for something that won't be 10 lines in php. :)
It's a full statically defined class with matching properties and attribute tags to link to the lJSON, and in many cases also some custom type translator functions. If you however have existing classes; then many of the JSON frameworks are more typically able to use reflection to match up to the similarly named properties.

Statically typed languages are generally far more verbose than dynamic languages; but it's not always the case; for example:
  • F# Type Providers; you get the best of both worlds; static typing and very terse code; reading and decoding JSON, XML, ... can be done in 2 lines in F# without any prior definition of classes, and unlike PHP it's statically typed. You even get code assist (intellisync) that's available with C# and Java's static class definitions.
 
Last edited:
It's a full statically defined class with matching properties and attribute tags to link to the lJSON, and in many cases also some custom type translator functions. If you however have existing classes; then many of the JSON frameworks are more typically able to use reflection to match up to the similarly named properties.

Statically typed languages are generally far more verbose than dynamic languages; but it's not always the case; for example:
  • F# Type Providers; you get the best of both worlds; static typing and very terse code; reading and decoding JSON, XML, ... can be done in 2 lines in F# without any prior definition of classes, and unlike PHP it's statically typed. You even get code assist (intellisync) that's available with C# and Java's static class definitions.
True - I was more taking the piss.

That said... I prefer SOAP (what is done is done) if a client complains or something then I will create a JSON wrapper - I assume here he is trying to do something similar - ie building out the verbose part with the aim to offer a JSON endpoint to the outside world?
 
Some people like to do historic re-enactments, behaving, dressing and using tools like people did 100 or more years ago.

Thor prefers SOAP.

Tomahto, tomayto.

I don't think anybody prefers dial up over fibre though.
 
Top
Sign up to the MyBroadband newsletter
X