I am trying to parse a JSON with Java and I do not get the expected result.
Example json:
"productId": "NDAtOS0wLS0=",
"branchId": 5,
"branchCustomers":[
{"branchId":615,"customerNumber":4918,"products":[]},
{"branchId":615,"customerNumber":9753,"products":[]},
{"branchId":615,"customerNumber":9761,"products":[]}
],
"customerNumber": 240,
"subAccountNumber": 0,
"productType": 9,
"openingDate": "2016-10-02",
"values": [
{
"key": "accountingMethod",
"value": "1"
},
{
"key": "accountType",
"value": "1"
},
{
"key": "assetCashAccountId-branchId",
"value": "615"
},
{
"key": "assetCashAccountId-customerNumber",
"value": "4041240"
},
{
"key": "assetCashAccountId-subAccountNumber",
"value": "2"
},
{
"key": "assetMarginAccountId-branchId",
"value": "615"
},
{
"key": "assetMarginAccountId-customerNumber",
"value": "4041240"
},
{
"key": "assetMarginAccountId-subAccountNumber",
"value": "2"
},
{
"key": "blockingAmount",
"value": "1000000"
},
{
"key": "collateral",
"value": "C"
},
{
"key": "collateralBlockingType",
"value": "1"
},
{
"key": "executingSecurityAccountId-branchId",
"value": "615"
},
{
"key": "executingSecurityAccountId-customerNumber",
"value": "4041240"
},
{
"key": "executingSecurityAccountId-subAccountNumber",
"value": "0"
},
{
"key": "limit",
"value": "1000000"
},
{
"key": "marginAccountId-branchId",
"value": "0"
},
{
"key": "marginAccountId-customerNumber",
"value": "0"
},
{
"key": "marginAccountId-subAccountNumber",
"value": "0"
},
{
"key": "marginMarkup",
"value": "100"
},
{
"key": "rolfNolanLedger",
"value": "B01429"
},
{
"key": "settlementMethod",
"value": "1"
}
]
}
]
}],
"instances": []
}
Not all the JSONs have this structure, some may miss some of the fields. I created some DTO classes for parsing it. This is my code:
public class Response {
private String partnerId;
private byte branchId;
private long customerNumber;
private Long subAccountNumber;
private Byte productType;
private String openingDate;
private String closingDate;
private List<Values> values;
private List<Instances> instances;
private List<BranchCustomers> branchCustomers;
public String getProductid() {
return partnerId;
}
public void setProductid(String productid) {
this.partnerId = productid;
}
public byte getBranchid() {
return branchId;
}
public void setBranchid(byte branchid) {
this.branchId = branchid;
}
public long getCustomernumber() {
return customerNumber;
}
public void setCustomernumber(long customernumber) {
this.customerNumber = customernumber;
}
public Long getSubaccountnumber() {
return subAccountNumber;
}
public void setSubaccountnumber(Long subAccountNumber) {
this.subAccountNumber = subAccountNumber;
}
public Byte getProducttype() {
return productType;
}
public void setProducttype(Byte productType) {
this.productType = productType;
}
public String getOpeningdate() {
return openingDate;
}
public void setOpeningdate(String openingDate) {
this.openingDate = openingDate;
}
public String getClosingdate() {
return closingDate;
}
public void setClosingdate(String closingDate) {
this.closingDate = closingDate;
}
public List<Values> getValues() {
return values;
}
public void setValues(List<Values> values) {
this.values = values;
}
public List<Instances> getInstances() {
return instances;
}
public void setInstances(List<Instances> instances) {
this.instances = instances;
}
public List<BranchCustomers> getBranchCustomers() {
return branchCustomers;
}
public void setBranchCustomers(List<BranchCustomers> branchCustomers) {
this.branchCustomers = branchCustomers;
}
}
public class BranchCustomers {
private byte branchId;
private long customerNumber;
private List<Products> products;
public byte getBranchid() {
return branchId;
}
public void setBranchid(byte branchId) {
this.branchId = branchId;
}
public long getCustomernumber() {
return customerNumber;
}
public void setCustomernumber(long customerNumber) {
this.customerNumber = customerNumber;
}
public List<Products> getProducts() {
return products;
}
public void setProducts(List<Products> products) {
this.products = products;
}
}
public void parseJson(String response) {
try{
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Response result = mapper.readValue(response, Response.class);
System.out.println(result);
} catch (JsonMappingException e) {
e.printStackTrace
} catch (JsonProcessingException e) {
e.printStackTrace();
}
The other DTO classes "Values", "Instances" and "Products" are empty at this moment, but I do not need them yet. The problem is that the "result" value has the right structure and the right fields, but they are null or 0, depending on their data type and I cannot see what I have done wrong.
Can anyone help me?
Your getters, setters have incorrect names.
As an example, in your Response object, you have getProductid, getBranchid, and getCustomernumber with similar setter methods. Your JSON however has productId, branchId, and customerNumber. They differ as you didn't use the correct capitalization. You should use camelCase, which would turn your naming to getProductId, getBranchId, and getCustomerNumber. This then matches the JSON and it will map accordingly.
This:
public class Response {
private String partnerId;
private byte branchId;
private long customerNumber;
private Long subAccountNumber;
private Byte productType;
private String openingDate;
private String closingDate;
private List<BranchCustomers> branchCustomers;
public String getProductId() {
return partnerId;
}
public void setProductId(String productid) {
this.partnerId = productid;
}
public byte getBranchId() {
return branchId;
}
public void setBranchId(byte branchid) {
this.branchId = branchid;
}
public long getCustomerNumber() {
return customerNumber;
}
public void setCustomerNumber(long customernumber) {
this.customerNumber = customernumber;
}
...
Leads to:
Response{productId='NDAtOS0wLS0=', branchId=5, customerNumber=240 ...
As a sidenote, I suggest either auto-generating the boilerplate code with your IDE or look into Project Lombok which can generate the boilerplate code by just adding an annotation. This also stops you from making naming mistakes.
Also, try and get a toString method for each pojo, as it will help you during logging or debugging.
Related
Here I want to send a hashMap as a Post request data.
In my Post request I have to send Data like
{
"dd98e151-3a57-45f5-b201-e7f5250bce8b": {
"ratingid": "64d2fa13-bf17-4242-9106-c1a722cd1063",
"compid": "dd98e151-3a57-45f5-b201-e7f5250bce8b",
"avgrat": "4.00",
"continue": " ",
"improve": " ",
"ratingindicators": [
{
"internalname": "c1",
"name": "CI 1",
"rating": "4"
},
{
"internalname": "CI 3",
"name": "CI 3 only ",
"feedback": "text"
},
{
"internalname": "I2",
"name": "CI 2",
"rating": "0"
}
]
},
"e6c6d37c-a9e6-491f-9f76-8375cee2096f": {
"ratingid": "64d2fa13-bf17-4242-9106-c1a722cd1063",
"compid": "dd98e151-3a57-45f5-b201-e7f5250bce8b",
"avgrat": "4.00",
"continue": " ",
"improve": " ",
"ratingindicators": [
{
"internalname": "c1",
"name": "CI 1",
"rating": "4"
},
{
"internalname": "CI 3",
"name": "CI 3 only ",
"feedback": "text"
},
{
"internalname": "I2",
"name": "CI 2",
"rating": "0"
}
]
}
}
To post that data in API call I have created a HashMap like
Map<String,PostIndicatorsModel>> map=new HashMap<String,PostIndicatorsModel>();
And Here PostIndicatorsModel is an pojo/model class
PostIndicatorsModel.java
public class FeeddbackPostIndicatorsModel {
#SerializedName("ratingid")
#Expose
private String feedbackratingid;
#SerializedName("compid")
#Expose
private String competencyid;
#SerializedName("avgrat")
#Expose
private String avgrating;
#SerializedName("continue")
#Expose
private String whattocontinue = "";
#SerializedName("improve")
#Expose
private String whattoimprove = "";
#SerializedName("ratingindicators")
#Expose
private List<Ratingindicator> ratingindicators = null;
public String getRatingid() {
return ratingid;
}
public void setRatingid(String ratingid) {
this.ratingid = ratingid;
}
public String getCompid() {
return compid;
}
public void setCompid(String compid) {
this.compid = compid;
}
public String getAvgrat() {
return avgrat;
}
public void setAvgrat(String avgrat) {
this.avgrat = avgrat;
}
public String getContinue() {
return _continue;
}
public void setContinue(String _continue) {
this._continue = _continue;
}
public String getImprove() {
return improve;
}
public void setImprove(String improve) {
this.improve = improve;
}
public List<Ratingindicator> getRatingindicators() {
return ratingindicators;
}
public void setRatingindicators(List<Ratingindicator> ratingindicators) {
this.ratingindicators = ratingindicators;
}
public static class Ratingindicator{
#SerializedName("internalname")
#Expose
private String internalname;
#SerializedName("name")
#Expose
private String name;
#SerializedName("rating")
#Expose
private String rating;
#SerializedName("feedback")
#Expose
private String feedback;
public String getInternalname() {
return internalname;
}
public void setInternalname(String internalname) {
this.internalname = internalname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
public String getFeedback() {
return feedback;
}
public void setFeedback(String feedback) {
this.feedback = feedback;
}
}
}
Please help me to understand how to call post API with this HashMap
Map<String,PostIndicatorsModel>> map=new HashMap<String,PostIndicatorsModel>();
In API call
#FormUrlEncoded
#POST(SURL.POST_INDICATORS)
Call<PostIndicatorsModel> postRate360(#Path("rateId") String rateId, #Body Map<String,PostIndicatorsModel> post);
I tried with #Body, #FeildMap,#QueryMap but it shows an error in call or server not getting data from post-call as required.
Please help me to understand how can i do this
I am trying to call a Spring Cloud Data Flow REST Endpoint which is supposed to return a list of all the executions of a task whose name is passed in the input.
For starters, I ran the following URL in the browser :
http://dataflow-server.myhost.net/tasks/executions?task1225
The following JSON is shown on the browser :
{
"_embedded": {
"taskExecutionResourceList": [
{
"executionId": 2908,
"exitCode": 0,
"taskName": "task1225",
"startTime": "2021-06-25T18:40:24.823+0000",
"endTime": "2021-06-25T18:40:27.585+0000",
"exitMessage": null,
"arguments": [
"--spring.datasource.username=******",
"--spring.cloud.task.name=task1225",
"--spring.datasource.url=******",
"--spring.datasource.driverClassName=org.h2.Driver",
"key=******",
"batchId=20210625_025755702",
"--spring.cloud.data.flow.platformname=default",
"--spring.cloud.task.executionid=2908"
],
"jobExecutionIds": [],
"errorMessage": null,
"externalExecutionId": "task1225-kp7mvwkmll",
"parentExecutionId": null,
"resourceUrl": "Docker Resource [docker:internal.artifactrepository.myhost.net/myProject/myimage:0.1]",
"appProperties": {
"spring.datasource.username": "******",
"spring.cloud.task.name": "task1225",
"spring.datasource.url": "******",
"spring.datasource.driverClassName": "org.h2.Driver"
},
"deploymentProperties": {
"spring.cloud.deployer.kubernetes.requests.memory": "512Mi",
"spring.cloud.deployer.kubernetes.limits.cpu": "1000m",
"spring.cloud.deployer.kubernetes.limits.memory": "8192Mi",
"spring.cloud.deployer.kubernetes.requests.cpu": "100m"
},
"taskExecutionStatus": "COMPLETE",
"_links": {
"self": {
"href": "http://dataflow-server.myhost.net/tasks/executions/2908"
}
}
}
]
},
"_links": {
"first": {
"href": "http://dataflow-server.myhost.net/tasks/executions?page=0&size=20"
},
"self": {
"href": "http://dataflow-server.myhost.net/tasks/executions?page=0&size=20"
},
"next": {
"href": "http://dataflow-server.myhost.net/tasks/executions?page=1&size=20"
},
"last": {
"href": "http://dataflow-server.myhost.net/tasks/executions?page=145&size=20"
}
},
"page": {
"size": 20,
"totalElements": 2908,
"totalPages": 146,
"number": 0
}
}
Next, I tried to call the same REST endpoint through Java; however, no matter what I try, the response object seems to be empty with none of the attributes populated :
Approach 1 : Custom domain classes created to deserialize the response. (Did not work. Empty content recieved in response)
ParameterizedTypeReference<Resources<TaskExecutions>> ptr = new ParameterizedTypeReference<Resources<TaskExecutions>>() {
};
ResponseEntity<Resources<TaskExecutions>> entity = restTemplate.exchange(
"http://dataflow-server.myhost.net/tasks/executions?task1225",
HttpMethod.GET, null, ptr);
System.out.println(entity.getBody().getContent()); **//empty content**
Where, TaskExecutions domain is as follows :
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonPropertyOrder({ "taskExecutionResourceList" })
#JsonIgnoreProperties(ignoreUnknown = true)
public class TaskExecutions {
public TaskExecutions() {
}
#JsonProperty("taskExecutionResourceList")
List<TaskExecutionResource> taskExecutionResourceList = new ArrayList<>();
#JsonProperty("taskExecutionResourceList")
public List<TaskExecutionResource> getTaskExecutionResourceList() {
return taskExecutionResourceList;
}
#JsonProperty("taskExecutionResourceList")
public void setTaskExecutionResourceList(List<TaskExecutionResource> taskExecutionResourceList) {
this.taskExecutionResourceList = taskExecutionResourceList;
}
}
And TaskExecutionResource is as follows :
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonPropertyOrder({
"executionId",
"exitCode",
"taskName",
"startTime",
"endTime",
"exitMessage",
"arguments",
"jobExecutionIds",
"errorMessage",
"externalExecutionId",
"parentExecutionId",
"resourceUrl",
"appProperties",
"deploymentProperties",
"taskExecutionStatus",
"_links" })
#JsonIgnoreProperties(ignoreUnknown = true)
public class TaskExecutionResource {
#JsonProperty("executionId")
private Integer executionId;
#JsonProperty("exitCode")
private Integer exitCode;
#JsonProperty("taskName")
private String taskName;
#JsonProperty("startTime")
private String startTime;
#JsonProperty("endTime")
private String endTime;
#JsonProperty("exitMessage")
private Object exitMessage;
#JsonProperty("arguments")
private List<String> arguments = new ArrayList<String>();
#JsonProperty("jobExecutionIds")
private List<Object> jobExecutionIds = new ArrayList<Object>();
#JsonProperty("errorMessage")
private Object errorMessage;
#JsonProperty("externalExecutionId")
private String externalExecutionId;
#JsonProperty("parentExecutionId")
private Object parentExecutionId;
#JsonProperty("resourceUrl")
private String resourceUrl;
#JsonProperty("appProperties")
private AppProperties appProperties;
#JsonProperty("deploymentProperties")
private DeploymentProperties deploymentProperties;
#JsonProperty("taskExecutionStatus")
private String taskExecutionStatus;
#JsonProperty("_links")
private Links links;
#JsonProperty("executionId")
public Integer getExecutionId() {
return executionId;
}
#JsonProperty("executionId")
public void setExecutionId(Integer executionId) {
this.executionId = executionId;
}
#JsonProperty("exitCode")
public Integer getExitCode() {
return exitCode;
}
#JsonProperty("exitCode")
public void setExitCode(Integer exitCode) {
this.exitCode = exitCode;
}
#JsonProperty("taskName")
public String getTaskName() {
return taskName;
}
#JsonProperty("taskName")
public void setTaskName(String taskName) {
this.taskName = taskName;
}
#JsonProperty("startTime")
public String getStartTime() {
return startTime;
}
#JsonProperty("startTime")
public void setStartTime(String startTime) {
this.startTime = startTime;
}
#JsonProperty("endTime")
public String getEndTime() {
return endTime;
}
#JsonProperty("endTime")
public void setEndTime(String endTime) {
this.endTime = endTime;
}
#JsonProperty("exitMessage")
public Object getExitMessage() {
return exitMessage;
}
#JsonProperty("exitMessage")
public void setExitMessage(Object exitMessage) {
this.exitMessage = exitMessage;
}
#JsonProperty("arguments")
public List<String> getArguments() {
return arguments;
}
#JsonProperty("arguments")
public void setArguments(List<String> arguments) {
this.arguments = arguments;
}
#JsonProperty("jobExecutionIds")
public List<Object> getJobExecutionIds() {
return jobExecutionIds;
}
#JsonProperty("jobExecutionIds")
public void setJobExecutionIds(List<Object> jobExecutionIds) {
this.jobExecutionIds = jobExecutionIds;
}
#JsonProperty("errorMessage")
public Object getErrorMessage() {
return errorMessage;
}
#JsonProperty("errorMessage")
public void setErrorMessage(Object errorMessage) {
this.errorMessage = errorMessage;
}
#JsonProperty("externalExecutionId")
public String getExternalExecutionId() {
return externalExecutionId;
}
#JsonProperty("externalExecutionId")
public void setExternalExecutionId(String externalExecutionId) {
this.externalExecutionId = externalExecutionId;
}
#JsonProperty("parentExecutionId")
public Object getParentExecutionId() {
return parentExecutionId;
}
#JsonProperty("parentExecutionId")
public void setParentExecutionId(Object parentExecutionId) {
this.parentExecutionId = parentExecutionId;
}
#JsonProperty("resourceUrl")
public String getResourceUrl() {
return resourceUrl;
}
#JsonProperty("resourceUrl")
public void setResourceUrl(String resourceUrl) {
this.resourceUrl = resourceUrl;
}
#JsonProperty("appProperties")
public AppProperties getAppProperties() {
return appProperties;
}
#JsonProperty("appProperties")
public void setAppProperties(AppProperties appProperties) {
this.appProperties = appProperties;
}
#JsonProperty("deploymentProperties")
public DeploymentProperties getDeploymentProperties() {
return deploymentProperties;
}
#JsonProperty("deploymentProperties")
public void setDeploymentProperties(DeploymentProperties deploymentProperties) {
this.deploymentProperties = deploymentProperties;
}
#JsonProperty("taskExecutionStatus")
public String getTaskExecutionStatus() {
return taskExecutionStatus;
}
#JsonProperty("taskExecutionStatus")
public void setTaskExecutionStatus(String taskExecutionStatus) {
this.taskExecutionStatus = taskExecutionStatus;
}
#JsonProperty("_links")
public Links getLinks() {
return links;
}
#JsonProperty("_links")
public void setLinks(Links links) {
this.links = links;
}
}
Approach 2 : Add spring-cloud-data-flow-rest as a maven dependency in my project and use the TaskExectuionResource entity defined in this project. :
TaskExecutionResource.Page = restTemplate.getForObject("http://dataflow-server.myhost.net/tasks/executions?task1225",
TaskExecutionResource.Page.class);//**Empty content**
Question : How can I deserialize the response of the JSON returned by a rest enndpoint that is using HATEOAS? It seems like a very daunting task to get this to work.
Not sure how you constructed RestTemplate but it doesn't work as is with hateoas and there's some additional steps you need to do.
To get idea what we've done see ObjectMapper config. There's hal module and additional mixin's what mapper needs to be aware of for these things to work.
I have this JSON file:
{
"mapping": {
"trips": [
{
"starttime": "15:10:50.000",
"endtime": "15:17:30.000",
"name": "island1",
"program": [
{
"starttime": "15:14:27.000",
"endtime": "15:14:54.000",
"name": "Breakfast"
},
{
"starttime": "15:16:35.000",
"endtime": "15:16:56.000",
"name": "Swimming"
},
{
"starttime": "15:15:41.000",
"endtime": "15:16:07.000",
"name": "Lunch"
},
{
"starttime": "15:10:50.000",
"endtime": "15:11:19.000",
"name": "Swimming"
},
{
"starttime": "15:17:01.000",
"endtime": "15:17:30.000",
"name": "Dinner"
}
]
},
{
"starttime": "15:18:43.000",
"endtime": "15:27:34.000",
"name": "island2",
"program": [
{
"starttime": "15:20:53.000",
"endtime": "15:21:15.000",
"name": "Yoga"
},
{
"starttime": "15:20:17.000",
"endtime": "15:20:43.000",
"name": "Breakfast"
},
{
"starttime": "15:20:28.000",
"endtime": "15:20:55.000",
"name": "Swimming"
},
{
"starttime": "15:23:23.000",
"endtime": "15:23:46.000",
"name": "Swimming"
},
{
"starttime": "15:20:24.000",
"endtime": "15:20:45.000",
"name": "Dinner"
},
{
"starttime": "15:26:17.000",
"endtime": "15:26:38.000",
"name": "Clubbing"
},
{
"starttime": "15:20:04.000",
"endtime": "15:20:28.000",
"name": "Sleeping"
}
]
}
]
}
}
How can I parse it in Java with Gson? I tried something like that but I received com.example.Model#43556938
null
public static void main(String[] args) {
Gson gson = new Gson();
try (Reader reader = new FileReader("myjsonfile.json")) {
Mapping mapping = gson.fromJson(reader, Mapping.class);
Trip trip = gson.fromJson(reader, Trip.class);
System.out.println(mapping);
System.out.println(trip);
} catch (IOException e) {
e.printStackTrace();
}
}
I want to read all the information (JSON objects and arrays). Can I do it from line to line or any suggestions?
You are fetching the Mapping object and priting the same object
Do it like this:
private static final Type REVIEW_TYPE = new TypeToken<List<Mapping>>() {
}.getType();
Gson gson = new Gson();
JsonReader reader = new JsonReader(new FileReader(YOUR_JSON_FILE));
List<Mapping> data = gson.fromJson(reader, REVIEW_TYPE); // contains the whole mapping list
data.toScreen(); // print some value of data through iteration
When you are reading from Gson assign it to Hashmap and then read it from map. Hope This will help you
Below is the code to do it:
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import com.google.gson.Gson;
public class JsontoObj {
public static void main(String[] args) {
Gson gson = new Gson();
try (Reader reader = new FileReader("/Documents/sample.json")) {
HashMap<String, Mapping> mappings= gson.fromJson(reader, HashMap.class);
//System.out.println(mappings);
}catch (IOException e) {
e.printStackTrace();
}
}
}
class Mapping{
List<Trip> trips;
public List<Trip> getTrips() {
return trips;
}
public void setTrips(List<Trip> trips) {
this.trips = trips;
}
}
class Trip{
private String startTime;
private String endTime;
private String name;
private Program program[] = new Program[5];
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Program[] getProgram() {
return program;
}
public void setProgram(Program[] program) {
this.program = program;
}
}
class Program{
private String startTime;
private String endTime;
private String name;
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
How can I describe the POJO for such an answer so that the retrofit understands it? the response comes in this form. I cut it, in order to see the structure of JSON. nested objects I converted to POJO. I want to learn how to convert the main object?
[
[
"all_areas",
{
"6": {
"id": "6",
"parent_id": "0",
"left_key": "1",
"right_key": "6594",
"level": "1",
"title": "Вся Россия",
"alias": "vsya_rossiya",
"sort": "1",
"navigatorListItems": []
},
"7": {
"id": "7",
"parent_id": "6",
"left_key": "2",
"right_key": "31",
"level": "2",
"title": "Адыгея респ.",
"alias": "adygeya_resp",
"sort": "1",
"navigatorListItems": []
}
}
],
[
"current_rubrics",
[
{
"id": "7",
"parent_id": "6",
"left_key": "2",
"right_key": "19",
"level": "2",
"title": "Недвижимость",
"alias": "nedvizhimost",
"sort": "1"
},
{
"id": "8",
"parent_id": "6",
"left_key": "20",
"right_key": "47",
"level": "2",
"title": "Транспорт",
"alias": "transport",
"sort": "2"
}
]
]
]
I suppose this is what a Json should look like
{
"all_areas": [{
"6": {
"id": "6",
"parent_id": "0",
"left_key": "1",
"right_key": "6594",
"level": "1",
"title": "Вся Россия",
"alias": "vsya_rossiya",
"sort": "1",
"navigatorListItems": []
}
},
{
"7": {
"id": "7",
"parent_id": "6",
"left_key": "2",
"right_key": "31",
"level": "2",
"title": "Адыгея респ.",
"alias": "adygeya_resp",
"sort": "1",
"navigatorListItems": []
}
}
],
"current_rubrics": [{
"id": "7",
"parent_id": "6",
"left_key": "2",
"right_key": "19",
"level": "2",
"title": "Недвижимость",
"alias": "nedvizhimost",
"sort": "1"
},
{
"id": "8",
"parent_id": "6",
"left_key": "20",
"right_key": "47",
"level": "2",
"title": "Транспорт",
"alias": "transport",
"sort": "2"
}
]
}
Copy this and convert it to pojo using the website http://www.jsonschema2pojo.org or any another website which does the conversion for you
This will be your main model class:
public class Testing
{
private 7 7;
private 6 6;
public 7 get7 ()
{
return 7;
}
public void set7 (7 7)
{
this.7 = 7;
}
public 6 get6 ()
{
return 6;
}
public void set6 (6 6)
{
this.6 = 6;
}
#Override
public String toString()
{
return "ClassPojo [7 = "+7+", 6 = "+6+"]";
}
}
then there will be two subclasses (7.java & 6.java):
7.java
public class 7
{
private String[] navigatorListItems;
private String id;
private String left_key;
private String title;
private String sort;
private String level;
private String alias;
private String right_key;
private String parent_id;
public String[] getNavigatorListItems ()
{
return navigatorListItems;
}
public void setNavigatorListItems (String[] navigatorListItems)
{
this.navigatorListItems = navigatorListItems;
}
public String getId ()
{
return id;
}
public void setId (String id)
{
this.id = id;
}
public String getLeft_key ()
{
return left_key;
}
public void setLeft_key (String left_key)
{
this.left_key = left_key;
}
public String getTitle ()
{
return title;
}
public void setTitle (String title)
{
this.title = title;
}
public String getSort ()
{
return sort;
}
public void setSort (String sort)
{
this.sort = sort;
}
public String getLevel ()
{
return level;
}
public void setLevel (String level)
{
this.level = level;
}
public String getAlias ()
{
return alias;
}
public void setAlias (String alias)
{
this.alias = alias;
}
public String getRight_key ()
{
return right_key;
}
public void setRight_key (String right_key)
{
this.right_key = right_key;
}
public String getParent_id ()
{
return parent_id;
}
public void setParent_id (String parent_id)
{
this.parent_id = parent_id;
}
#Override
public String toString()
{
return "ClassPojo [navigatorListItems = "+navigatorListItems+", id = "+id+", left_key = "+left_key+", title = "+title+", sort = "+sort+", level = "+level+", alias = "+alias+", right_key = "+right_key+", parent_id = "+parent_id+"]";
}
}
6.java
public class 6
{
private String[] navigatorListItems;
private String id;
private String left_key;
private String title;
private String sort;
private String level;
private String alias;
private String right_key;
private String parent_id;
public String[] getNavigatorListItems ()
{
return navigatorListItems;
}
public void setNavigatorListItems (String[] navigatorListItems)
{
this.navigatorListItems = navigatorListItems;
}
public String getId ()
{
return id;
}
public void setId (String id)
{
this.id = id;
}
public String getLeft_key ()
{
return left_key;
}
public void setLeft_key (String left_key)
{
this.left_key = left_key;
}
public String getTitle ()
{
return title;
}
public void setTitle (String title)
{
this.title = title;
}
public String getSort ()
{
return sort;
}
public void setSort (String sort)
{
this.sort = sort;
}
public String getLevel ()
{
return level;
}
public void setLevel (String level)
{
this.level = level;
}
public String getAlias ()
{
return alias;
}
public void setAlias (String alias)
{
this.alias = alias;
}
public String getRight_key ()
{
return right_key;
}
public void setRight_key (String right_key)
{
this.right_key = right_key;
}
public String getParent_id ()
{
return parent_id;
}
public void setParent_id (String parent_id)
{
this.parent_id = parent_id;
}
#Override
public String toString()
{
return "ClassPojo [navigatorListItems = "+navigatorListItems+", id = "+id+", left_key = "+left_key+", title = "+title+", sort = "+sort+", level = "+level+", alias = "+alias+", right_key = "+right_key+", parent_id = "+parent_id+"]";
}
}
I have a basic spring boot standalone executable jar using the bott 2.0.0.0 I think this is simple, but Google won't give up the answer. :) I am am using the latest stable jackson versions (2.9.4) but they ARE being managed by spring. This is a Boolean problem:
here is the JSON I am trying to turn into a Java Pojo (it is wrapped in a higher object but I don't think that's the problem. I haveing problems qith the boolean.
{
"guid": "a5182918-8d69-11e6-acb6-0a97227b08ed",
"organizationId": 1,
"region": "Tariff Picker",
"stages": [{
"nextStages": [],
"activities": [{
"nextActivities": [],
"name": "New Activity",
"suspensionReason": "",
"rules": [],
"isSuspend": false,
"sequence": 1,
"allowedRoles": [{
"userApplications": [],
"name": "submitApplication",
"organizationId": 0,
"workQueues": [],
"roleApplicationsForSystemRoleId": [],
"isPublic": 0,
"widgetRoles": [],
"userRoles": [],
"roleTariffReports": [],
"roleTypeId": 0,
"distributionListRoles": [],
"organizationRoles": [],
"publicationRoles": [],
"roleTariffDataSets": [],
"roleApplicationsForApplicationRoleId": [],
"workQueueArchives": [],
"id": 11,
"rolePrivileges": []
}],
"label": "New Activity",
"irrevocable": false,
"stageId": 0,
"id": 0,
"buttonPrompt": "Submit",
"guid": "2e195e0c-83d2-491f-b2e8-3ad1159d1d99",
"dataBlock": {
"sections": [{
"info": "",
"prompt": "",
"name": "First Section",
"sequence": 0,
"fields": [],
"gatingConditions": [],
"guid": "480d160c-c34f-4022-97b0-e8a1f28c49ae",
"id": -2
}],
"prompt": "",
"id": -1,
"name": ""
},
"autoExecute": false
}],
"name": "Tariff Selection Stage",
"sequence": 1,
"rules": [],
"completionMessage": "",
"guid": "65a73280-c587-486f-be8b-9107426f4730",
"id": 0,
"description": ""
}],
"stop": "3000-01-01",
"workflowTypeId": 2,
"isUserAction": false,
"start": "1900-01-01",
"isSandbox": false,
"gatingConditions": [],
"tariffId": 49,
"businessCalendarId": 1,
"applicationForms": [],
"id": 49,
"rules": []
}
I am getting an error saying there is not a field named "isSuspend" and as you can see it is there (line 3) and even set to false. here is my pojo:
private int id;
private String name;
private List<DataBlockObject> dataBlocks;
private int sequence;
private List<RuleObject> rules;
private List<AllowedRoleObject> allowedRoles;
private List<NextActivityObject> nextActivities;
private List<ActivityPermissionObject> activityPermissions;
private boolean autoExecute;
private boolean irrevocable;
private String label;
private String buttonPrompt;
private int stageId;
private boolean isSuspend;
private String suspensionReason;
private String guid;
private List<ActivitySubmitErrorPromptObject> activitySubmitErrorPrompts;
private int activitySubmitErrorTimeout;
private String breadcrumbClass;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<DataBlockObject> getDataBlocks() {
return dataBlocks;
}
public void setDataBlocks(List<DataBlockObject> dataBlocks) {
this.dataBlocks = dataBlocks;
}
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
public List<RuleObject> getRules() {
return rules;
}
public void setRules(List<RuleObject> rules) {
this.rules = rules;
}
public List<AllowedRoleObject> getAllowedRoles() {
return allowedRoles;
}
public void setAllowedRoles(List<AllowedRoleObject> allowedRoles) {
this.allowedRoles = allowedRoles;
}
public List<NextActivityObject> getNextActivities() {
return nextActivities;
}
public void setNextActivities(List<NextActivityObject> nextActivities) {
this.nextActivities = nextActivities;
}
public List<ActivityPermissionObject> getActivityPermissions() {
return activityPermissions;
}
public void setActivityPermissions(List<ActivityPermissionObject> activityPermissions) {
this.activityPermissions = activityPermissions;
}
public boolean isAutoExecute() {
return autoExecute;
}
public void setAutoExecute(boolean autoExecute) {
this.autoExecute = autoExecute;
}
public boolean isIrrevocable() {
return irrevocable;
}
public void setIrrevocable(boolean irrevocable) {
this.irrevocable = irrevocable;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getButtonPrompt() {
return buttonPrompt;
}
public void setButtonPrompt(String buttonPrompt) {
this.buttonPrompt = buttonPrompt;
}
public int getStageId() {
return stageId;
}
public void setStageId(int stageId) {
this.stageId = stageId;
}
public boolean isSuspend() {
return isSuspend;
}
public void setSuspend(boolean isSuspend) {
this.isSuspend = isSuspend;
}
public String getSuspensionReason() {
return suspensionReason;
}
public void setSuspensionReason(String suspensionReason) {
this.suspensionReason = suspensionReason;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public List<ActivitySubmitErrorPromptObject> getActivitySubmitErrorPrompts() {
return activitySubmitErrorPrompts;
}
public void setActivitySubmitErrorPrompts(List<ActivitySubmitErrorPromptObject> activitySubmitErrorPrompts) {
this.activitySubmitErrorPrompts = activitySubmitErrorPrompts;
}
public int getActivitySubmitErrorTimeout() {
return activitySubmitErrorTimeout;
}
public void setActivitySubmitErrorTimeout(int activitySubmitErrorTimeout) {
this.activitySubmitErrorTimeout = activitySubmitErrorTimeout;
}
public String getBreadcrumbClass() {
return breadcrumbClass;
}
public void setBreadcrumbClass(String breadcrumbClass) {
this.breadcrumbClass = breadcrumbClass;
}
Can the problem be because of the getter and setter names for the field isSuspend? Try naming getter and setter getIsSuspend and setIsSuspend
Your POJO doesn't follow Java Beans naming convention. If you want Jackson to not look at the getters/setters, but only fields, see this: how to specify jackson to only use fields - preferably globally