Model classes looks like this MailFilterCondition has map of EmailCondition where Filter is a class and MailAttributes and Operator are enums:
public class EmailCondition implements Serializable {
private static final long serialVersionUID = -5429392022485346125L;
private Filter.MailAttributes key;
private Filter.Operator op;
private String value;
public Filter.MailAttributes getKey() {
return key;
}
public void setKey(Filter.MailAttributes key) {
this.key = key;
}
public Operator getOp() {
return op;
}
public void setOp(Operator op) {
this.op = op;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
#Override
public String toString() {
return "EmailCondition [key=" + key + ", op=" + op + ", value=" + value + "]";
}
}
public class MailFilterCondition implements Serializable {
private static final long serialVersionUID = -2691329267596354267L;
private int automationId;
private Map<String, EmailCondition> emailConditionMap;
private String filterString;
public int getAutomationId() {
return automationId;
}
public void setAutomationId(int automationId) {
this.automationId = automationId;
}
public Map<String, EmailCondition> getEmailConditions() {
return emailConditionMap;
}
public void setEmailConditions(Map<String, EmailCondition> emailConditionMap) {
this.emailConditionMap = emailConditionMap;
}
public String getFilterString() {
return filterString;
}
public void setFilterString(String filterString) {
this.filterString = filterString;
}
#Override
public String toString() {
return "MailFilterCondition [automationId=" + automationId + ", emailConditions=" + emailConditionMap
+ ", filterString=" + filterString + "]";
}
}
Controller:
#PostMapping(value = "/email")
public void createAutomationFilter(#RequestParam(value="automation_id") int automationId, #RequestBody MailFilterCondition filterData) {
System.out.println(filterData);
}
Request body:
{
"automationId": 123,
"filterString": "(c1_and_c2)_or_(c1_and_c3)",
"emailConditionMap":
{
"c1":{
"key": "from",
"op": "contains",
"value": "jhon"
},
"c2":{
"key": "from1",
"op": "starts",
"value": "ron"
},
"c3":{
"key": "from",
"op": "contains",
"value": "payment"
}
}
}
Filter enums:
public class Filter {
public enum Operator {
contains("contains"), starts("starts"), ends("ends"), gt("gt"), gteq("gteq"), lt("lt"), lteq("lteq"), eq("eq"),
inlist("inlist"), pattern("pattern");
private final String operator;
private Operator(String opt) {
this.operator = opt;
}
public String getValue() {
return this.operator;
}
}
public enum MailAttributes {
from("from"), to("to"), subject("subject"), body("body"), received_time("received_time");
private final String attribute;
private MailAttributes(String attribute) {
this.attribute = attribute;
}
public String getValue() {
return this.attribute;
}
}
public enum LogicOperator {
AND("_and_"), OR("_or_");
private final String operator;
private LogicOperator(String opt) {
this.operator = opt;
}
public String getValue() {
return this.operator;
}
}
}
Output:
MailFilterCondition [automationId=123, emailConditions=null, filterString=(c1_and_c2)_or_(c1_and_c3)]
While passing key-value pair in json the map attribute reamins null. I tried everything but every-time emailConditions prints null.
Do you have proper getters/setters in the classes?
I am not sure what the Filter class is, but changing those two fields to String it is working perfectly. So the problem may be there.
You need to use Jackson Marshalling. It will convert the object automatically before being sent in the request body.
Jackson uses getters and setters so your object must have them.
#PostMapping(value = "/email")
public void createAutomationFilter(#RequestParam(value="automation_id") int automationId, #RequestBody MailFilterCondition filterData) {
System.out.println(filterData);
}
Here in createAutomationFilter method you are passing #RequestParam(value="automation_id") int automationId as parameter, which is not correct as you sending all parameter in request body in Postman. So you have to map all parameter in one Model class (i.e MailFilterCondition) and by using
#RequestBody annotation. Please see the below controller.
Controller:
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping(value = "/mail")
public class TestController {
#PostMapping(value = "/create", produces = { "application/json" })
public void createAutomationFilter(#RequestBody final MailFilterCondition emailConditionMap,
final HttpServletRequest request) {
System.out.println(emailConditionMap);
}
}
In this model MailFilterCondition your setter and getter for emailConditionMap field is not appropiate.
public Map<String, EmailCondition> getEmailConditions() {
return emailConditionMap;
}
public void setEmailConditions(Map<String, EmailCondition> emailConditionMap) {
this.emailConditionMap = emailConditionMap;
}
Update your setter and getter with (Spring used to map fields with exactly the same name of the field setters)
public Map<String, EmailCondition> getEmailConditionMap() {
return emailConditionMap;
}
public void setEmailConditionMap(Map<String, EmailCondition>
emailConditionMap) {
this.emailConditionMap = emailConditionMap;
}
Model: MailFilterCondition
import java.io.Serializable;
import java.util.Map;
public class MailFilterCondition implements Serializable {
private static final long serialVersionUID = -2691329267596354267L;
private int automationId;
private Map<String, EmailCondition> emailConditionMap;
private String filterString;
public int getAutomationId() {
return automationId;
}
public void setAutomationId(int automationId) {
this.automationId = automationId;
}
public Map<String, EmailCondition> getEmailConditionMap() {
return emailConditionMap;
}
public void setEmailConditionMap(Map<String, EmailCondition> emailConditionMap) {
this.emailConditionMap = emailConditionMap;
}
public String getFilterString() {
return filterString;
}
public void setFilterString(String filterString) {
this.filterString = filterString;
}
#Override
public String toString() {
return "MailFilterCondition [automationId=" + automationId + ", emailConditions=" + emailConditionMap
+ ", filterString=" + filterString + "]";
}
}
In model EmailCondition, i am passing String as param in
public void setKey(String key) {
this.key = Filter.MailAttributes.valueOf(key);
}
public void setOp(String op) {
this.op = Filter.Operator.valueOf(op);
}
as you can see in request body you are passing key and op value as String,
these value are not any kind of custom java class. At run time Spring could not figure out the type, which you passing
public void setKey(Filter.MailAttributes key) {
this.key = key;
}
public void setOp(Operator op) {
this.op = op;
}
as per my knowledge, you can also bind these fields using #InitBinder annotation.
Model: EmailCondition
import java.io.Serializable;
import com.spring.web.controller.Filter.MailAttributes;
import com.spring.web.controller.Filter.Operator;
public class EmailCondition implements Serializable {
private static final long serialVersionUID = -5429392022485346125L;
private Filter.MailAttributes key;
private Filter.Operator op;
private String value;
public void setKey(String key) {
this.key = Filter.MailAttributes.valueOf(key);
}
public void setOp(String op) {
this.op = Filter.Operator.valueOf(op);
}
public void setValue(String value) {
this.value = value;
}
public MailAttributes getKey() {
return key;
}
public Operator getOp() {
return op;
}
public String getValue() {
return value;
}
#Override
public String toString() {
return "EmailCondition [key=" + key + ", op=" + op + ", value=" + value + "]";
}
}
Model: Filter
public class Filter {
public enum Operator {
contains("contains"), starts("starts"), ends("ends"), gt("gt"), gteq("gteq"), lt("lt"), lteq("lteq"), eq(
"eq"), inlist("inlist"), pattern("pattern");
private final String operator;
private Operator(String opt) {
this.operator = opt;
}
public String getValue() {
return this.operator;
}
}
public enum MailAttributes {
from("from"), to("to"), subject("subject"), body("body"), received_time("received_time");
private final String attribute;
private MailAttributes(String attribute) {
this.attribute = attribute;
}
public String getValue() {
return this.attribute;
}
}
public enum LogicOperator {
AND("_and_"), OR("_or_");
private final String operator;
private LogicOperator(String opt) {
this.operator = opt;
}
public String getValue() {
return this.operator;
}
}
}
You can send ajax request as below:
var payLoad = {
"automationId": 123,
"filterString": "(c1_and_c2)_or_(c1_and_c3)",
"emailConditionMap":
{
"c1":{
"key": "from",
"op": "contains",
"value": "jhon"
},
"c2":{
"key": "to",
"op": "starts",
"value": "ron"
},
"c3":{
"key": "subject",
"op": "ends",
"value": "payment"
}
}
};
$.ajax({
url: appContextJs+"/mail/create",
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(payLoad),
success: callbackfunction
});
appContextJs: as your base URL e.g: http://localhost:8080/webapp
Please do not forget to set contentType as application/json in Postman
Postman console output:
{
"automationId": 123,
"emailConditionMap": {
"c1": {
"key": "from",
"op": "contains",
"value": "jhon"
},
"c2": {
"key": "to",
"op": "starts",
"value": "ron"
},
"c3": {
"key": "subject",
"op": "ends",
"value": "payment"
}
},
"filterString": "(c1_and_c2)_or_(c1_and_c3)"
}
Eclipse console output:
MailFilterCondition [automationId=123, emailConditions={c1=EmailCondition [key=from, op=contains, value=jhon], c2=EmailCondition [key=to, op=starts, value=ron], c3=EmailCondition [key=subject, op=ends, value=payment]}, filterString=(c1_and_c2)_or_(c1_and_c3)]
Related
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.
below written is my input json:
{
"apiData": [{
"apiName": "API",
"arguments": [{
"callFlow": "A"
}, {
"directoryName1": "CFAActivation"
}, {
"recDurationIVR1": "6000"
}, {
"numToDial": "*72"
}, {
"sleepDuration": "0000"
}],
"step": "1"
}]
}
//here "arguments" json array has dynamic json objects..
Respected Java Class According to your JSON
public class Arguments
{
private String callFlow;
public String getCallFlow ()
{
return callFlow;
}
public void setCallFlow (String callFlow)
{
this.callFlow = callFlow;
}
#Override
public String toString()
{
return "ClassPojo [callFlow = "+callFlow+"]";
}
}
public class ApiData
{
private Arguments[] arguments;
private String apiName;
private String step;
public Arguments[] getArguments ()
{
return arguments;
}
public void setArguments (Arguments[] arguments)
{
this.arguments = arguments;
}
public String getApiName ()
{
return apiName;
}
public void setApiName (String apiName)
{
this.apiName = apiName;
}
public String getStep ()
{
return step;
}
public void setStep (String step)
{
this.step = step;
}
#Override
public String toString()
{
return "ClassPojo [arguments = "+arguments+", apiName = "+apiName+", step = "+step+"]";
}
}
public class Jobj
{
private ApiData[] apiData;
public ApiData[] getApiData ()
{
return apiData;
}
public void setApiData (ApiData[] apiData)
{
this.apiData = apiData;
}
#Override
public String toString()
{
return "ClassPojo [apiData = "+apiData+"]";
}
}
The Gist
I tried to deserialize some JSON text with GSON. The JSON string had values defined. However, the deserialized string has null values.
Exactly what I did
I tried to deserialize some JSON text with GSON
SomeSpec deserializedJson = GSON.fromJson(serializedJson, SomeSpec.class);
where serializedJson is a string containing
{
"some_class": "abc.def.SomeClass",
"stuff": [
"FOO",
"BAR",
],
"definition": {
"values": [
{ "feature": "FOO", "value": 1.0 },
{ "feature": "BAR", "value": 1.0 },
]
}
}
and SomeSpec is a java class containing:
package somepackagepath;
import java.util.List;
public class SomeSpec {
private List<FeatureValueSpec> _values;
private List<String> _postProcessFunctions;
public List<FeatureValueSpec> getValues() {
return _values;
}
public List<String> getPostProcessFunctions() {
return _postProcessFunctions;
}
public static class FeatureValueSpec {
private String _feature;
private float _value;
public String getFeature() {
return _feature;
}
public float getValue() {
return _value;
}
}
}
The deserialized object had only null fields even though the JSON clearly had those fields defined.
First: There are two errors in your JSON in Arrays. There are extra commas at end of each array.
Second your models should look like this
public class Values
{
private String value;
private String feature;
public String getValue ()
{
return value;
}
public void setValue (String value)
{
this.value = value;
}
public String getFeature ()
{
return feature;
}
public void setFeature (String feature)
{
this.feature = feature;
}
}
public class Definition
{
private Values[] values;
public Values[] getValues ()
{
return values;
}
public void setValues (Values[] values)
{
this.values = values;
}
}
public class MyPojo
{
private Definition definition;
private String[] stuff;
private String some_class;
public Definition getDefinition ()
{
return definition;
}
public void setDefinition (Definition definition)
{
this.definition = definition;
}
public String[] getStuff ()
{
return stuff;
}
public void setStuff (String[] stuff)
{
this.stuff = stuff;
}
public String getSome_class ()
{
return some_class;
}
public void setSome_class (String some_class)
{
this.some_class = some_class;
}
}
In JSON Response there is key "Value" but its response have multiple forms like String and Array with same key "Value".
So how to make Retrofit model class to maintain String and Array with same key "Value".
{
"RespCode":"SUCCESS",
"RespText":"Transaction Details",
"Data":{
"Record":[
{
"group_title":"Seller Information",
"group_values":[
{
"key":"Listing Agent",
"value":[
{
"key":"Agent First Name",
"value":"Myks"
},
{
"key":"Agent Last Name",
"value":"Joe"
},
{
"key":"Company",
"value":"bdfjdlfdf"
},
{
"key":"Phone",
"value":"712.336.4967"
},
{
"key":"Email",
"value":"abc#gmail.com"
}
]
},
{
"key":"Cell Phone",
"value":"012.345.6789"
},
{
"key":"Email",
"value":"balt#gmail.com.com"
},
{
"key":"Preferred Contact Method",
"value":"Phone"
}
]
},
]
}
}
Just use an arraylist that contains multiple hashmaps maybe? Or... You have to define a pojo that has list of arrays with type map or something to that effect
Check this:
public class ModelBean {
private String RespCode;
private String RespText;
private DataBean Data;
public String getRespCode() {
return RespCode;
}
public void setRespCode(String RespCode) {
this.RespCode = RespCode;
}
public String getRespText() {
return RespText;
}
public void setRespText(String RespText) {
this.RespText = RespText;
}
public DataBean getData() {
return Data;
}
public void setData(DataBean Data) {
this.Data = Data;
}
public static class DataBean {
private List<RecordBean> Record;
public List<RecordBean> getRecord() {
return Record;
}
public void setRecord(List<RecordBean> Record) {
this.Record = Record;
}
public static class RecordBean {
private String group_title;
private List<GroupValuesBean> group_values;
public String getGroup_title() {
return group_title;
}
public void setGroup_title(String group_title) {
this.group_title = group_title;
}
public List<GroupValuesBean> getGroup_values() {
return group_values;
}
public void setGroup_values(List<GroupValuesBean> group_values) {
this.group_values = group_values;
}
public static class GroupValuesBean {
private String key;
private List<ValueBean> value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public List<ValueBean> getValue() {
return value;
}
public void setValue(List<ValueBean> value) {
this.value = value;
}
public static class ValueBean {
private String key;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
}
}
}
given the following json:
{ "response": {
"totalProcessingTime": "271.0",
"resultSets": {
"products": {
"firstHit": "1",
"lastHit": "10",
"totalHits": "77",
"hits": [
{
"number": "1",
"dmsubcategory": "TV, lyd og bilde",
"collection": "tilbud",
"title": "<b>TV</b> Panasonic 47 TX-LU 7ET5Y"
},
{
"number": "2",
"dmsubcategory": "TV, lyd og bilde",
"collection": "tilbud",
"title": "<b>TV</b> Panasonic 47 TX-LU 7ET5Y"
},
{
"number": "3",
"dmsubcategory": "TV, lyd og bilde",
"collection": "tilbud",
"title": "<b>TV</b> Panasonic 47 TX-LU 7ET5Y"
}
]
}
}
}
}
I'm using the following code to call jackson:
ObjectMapper mapper = new ObjectMapper();
SearchResult searchResult = mapper.readValue(new URL(jsonUrl + queryUrl), SearchResult.class);
I have ganerated POJOs for the whole hiearchy where the products class looks like:
public class Products {
public List<Hits> hits;
public String totalHits;
#JsonAnySetter
public void handleUnknown(String key, Object value) {
// do something: put to a Map; log a warning, whatever
}
public List<Hits> getHits() {
return hits;
}
public void setHits(List<Hits> hits) {
this.hits = hits;
}
public String getTotalHits() {
return totalHits;
}
public void setTotalHits(String totalHits) {
this.totalHits = totalHits;
}
}
and the hits class:
public class Hits {
public String number;
public String title;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
#JsonAnySetter
public void handleUnknown(String key, Object value) {
// do something: put to a Map; log a warning, whatever
}
}
All the other properties are mapped correct, but not the list containing hits. It's all empty. How can I map this to get it right?
Thanks!
You model is not compatible.
In order to see what is going wrong, maybe it is a good idea to have some toStrings and you could easy see where the mapping is failing.
You have a Object that needs to hold a property response, that needs to hold a property resultSets that needs to hold a property products that needs to hold hits.
I implemented like this:
GeneralResponse
- Response
- ResultSets
- Products
- Hits
- number
- title
Please test following implementation:
package snippet;
public class GeneralResponse {
private Response response;
public Response getResponse() {
return response;
}
public void setResponse(Response response) {
this.response = response;
}
#Override
public String toString() {
return "GeneralResponse [response=" + response + "]";
}
}
package snippet;
public class ResultSets {
private Products products;
public Products getProducts() {
return products;
}
public void setProducts(Products products) {
this.products = products;
}
#Override
public String toString() {
return "ResultSets [products=" + products + "]";
}
}
package snippet;
import java.util.List;
import org.codehaus.jackson.annotate.JsonAnySetter;
public class Products {
public List<Hits> hits;
public String totalHits;
#JsonAnySetter
public void handleUnknown(String key, Object value) {
// do something: put to a Map; log a warning, whatever
}
public List<Hits> getHits() {
return hits;
}
public void setHits(List<Hits> hits) {
this.hits = hits;
}
public String getTotalHits() {
return totalHits;
}
public void setTotalHits(String totalHits) {
this.totalHits = totalHits;
}
#Override
public String toString() {
return "Products [hits=" + hits + ", totalHits=" + totalHits + "]";
}
}
package snippet;
import org.codehaus.jackson.annotate.JsonAnySetter;
public class Response {
private ResultSets resultSets;
public ResultSets getResultSets() {
return resultSets;
}
public void setResultSets(ResultSets resultSets) {
this.resultSets = resultSets;
}
#Override
public String toString() {
return "Response [resultSets=" + resultSets + "]";
}
#JsonAnySetter
public void handleUnknown(String key, Object value) {
// do something: put to a Map; log a warning, whatever
}
}
package snippet;
import org.codehaus.jackson.annotate.JsonAnySetter;
public class Hits {
public String number;
public String title;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
#JsonAnySetter
public void handleUnknown(String key, Object value) {
// do something: put to a Map; log a warning, whatever
}
#Override
public String toString() {
return "Hits [number=" + number + ", title=" + title + "]";
}
}
after all you can do something like:
ObjectMapper om = new ObjectMapper();
Object r = om.readValue(inputStream, GeneralResponse.class);
The code looks fine. The error may be in json response
"title": "<b>TV</b> Panasonic 47”TX-LU 7ET5Y"
The backquotes after 47 and before TX, might be troublesome. Please check if you can parse this response.