I want to extract "msg" value from below json using fasterxml.jackson - Can anyone suggest me how my model class should look like?
{
"statusCode": 422,
"error": "Unprocessable Entity",
"message": "Bad data received",
"err_data": {
"payment_details.type": {
"location": "body",
"param": "payment_details.type",
"msg": "Must be either etransfer or cheque"
}
}
}
This is what I have done, but it is always returning "null" !
#JsonInclude(JsonInclude.Include.ALWAYS)
public class MyApiResponse extends ParentResponse implements Serializable {
private static final long serialVersionUID = 1L;
#JsonProperty("payment_details")
private PaymentDetails payment_details;
#JsonProperty("payment_details")
public PaymentDetails getPayment_details() {
return payment_details;
}
#JsonProperty("payment_details")
public void setPayment_details(PaymentDetails payment_details) {
this.payment_details = payment_details;
}
}
ParentResponse model class extends ErrorResponse model class and this is how it looks like..
This ErrorResponse model represents above mentioned JSON.
#JsonInclude(JsonInclude.Include.NON_NULL)
public class ErrorResponse implements Serializable {
private static final long serialVersionUID = 1L;
#JsonProperty("statusCode")
private int statusCode;
#JsonProperty("error")
private String error;
#JsonProperty("message")
private String message;
#JsonProperty("err_data")
private ErrData err_data;
#JsonProperty("statusCode")
public int getStatusCode() {
return statusCode;
}
#JsonProperty("statusCode")
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
#JsonProperty("message")
public String getMessage() {
return message;
}
#JsonProperty("message")
public void setMessage(String message) {
this.message = message;
}
#JsonProperty("error")
public String getError() {
return error;
}
#JsonProperty("error")
public void setError(String error) {
this.error = error;
}
#JsonProperty("err_data")
public ErrData getErr_data() {
return err_data;
}
#JsonProperty("err_data")
public void setErr_data(ErrData err_data) {
this.err_data = err_data;
}
}
err_data object is represented by below model class.
#JsonInclude(JsonInclude.Include.ALWAYS)
public class ErrData implements Serializable {
private static final long serialVersionUID = 1L;
#JsonProperty("email")
private Email email;
#JsonProperty("payment_details.type")
private PaymentDetailsType payment_details_type;
#JsonProperty("email")
public Email getEmail() {
return email;
}
#JsonProperty("email")
public void setEmail(Email email) {
this.email = email;
}
#JsonProperty("payment_details.type")
public PaymentDetailsType getPayment_details_type() {
return payment_details_type;
}
#JsonProperty("payment_details.type")
public void setPayment_details_type(PaymentDetailsType payment_details_type) {
this.payment_details_type = payment_details_type;
}
}
payment_details.type object represented by below class.
#JsonInclude(JsonInclude.Include.ALWAYS)
public class PaymentDetailsType extends ErrorMessage implements Serializable {
private static final long serialVersionUID = 1L;
}
#JsonInclude(JsonInclude.Include.ALWAYS)
public class Email extends ErrorMessage implements Serializable {
private static final long serialVersionUID = 1L;
}
And finally ErrorMessage which is extended by PaymentDetailsType as below.
#JsonPropertyOrder({"location", "param", "value", "msg"})
public class ErrorMessage implements Serializable {
private static final long serialVersionUID = 1L;
#JsonProperty("location")
private String location;
#JsonProperty("param")
private String param;
#JsonProperty("value")
private String value;
#JsonProperty("msg")
private String msg;
#JsonProperty("location")
public String getLocation() {
return location;
}
#JsonProperty("location")
public void setLocation(String location) {
this.location = location;
}
#JsonProperty("param")
public String getParam() {
return param;
}
#JsonProperty("param")
public void setParam(String param) {
this.param = param;
}
#JsonProperty("value")
public String getValue() {
return value;
}
#JsonProperty("value")
public void setValue(String value) {
this.value = value;
}
#JsonProperty("msg")
public String getMsg() {
return msg;
}
#JsonProperty("msg")
public void setMsg(String msg) {
this.msg = msg;
}
}
And finally I am trying to get "msg" field value as below -
new Gson().fromJson(response.asString(), MyApiResponse.class).getErr_data().getPayment_details_type().getMsg();
I think there is something wrong with this one - Not sure how to define getter method if field name in json as . (dot).
#JsonProperty("payment_details.type")
public PaymentDetailsType getPayment_details_type() {
return payment_details_type;
}
Similar to above, I am doing it for below json to retrieve "msg" value and it is working fine.
{
"statusCode": 422,
"error": "Unprocessable Entity",
"message": "Bad data received",
"err_data": {
"email": {
"location": "body",
"param": "email",
"value": "test # com",
"msg": "Must be a valid email"
}
}
}
This is returning correct "msg" value.
new Gson().fromJson(response.asString(), MyApiResponse.class).getErr_data().getEmail().getMsg();
Please suggest!
Thank you.
Here is a minimal example showing how to parse a JSON with Jackson, where the property names may contain dots:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
class Main {
public static void main(String[] args) throws IOException {
String json = "{" +
" \"payment_details.type\": {" +
" \"location\": \"body\"" +
" }" +
"}";
ObjectMapper mapper = new ObjectMapper();
Response response = mapper.readValue(json, Response.class);
System.out.println(response.getPaymentDetailsType().getLocation());
}
}
class Response {
#JsonProperty("payment_details.type")
private PaymentDetailsType paymentDetailsType;
public PaymentDetailsType getPaymentDetailsType() { return paymentDetailsType; }
}
class PaymentDetailsType {
private String location;
public String getLocation() { return location; }
}
Note that you only need #JsonProperty when the expected property name in JSON cannot be deduced from a setter or variable name
Its also working with #JsonProperty.
You just have to escape the dot:
#JsonProperty("payment_details\\.type")
I've make HttpsURLConnection to receive some information about my server.
The result of response is :
{"about":{"title":"NiFi","version":"1.1.0","uri":"https://localhost:443/api/","contentViewerUrl":"/nifi-content-viewer/","timezone":"CET"}}
How is possible to extract all attributes and key/value ?
About.class file
public class About {
private List<AboutObject> about;
public About()
{
// this.about = about;
}
public List<AboutObject> getAbout() {
return this.about;
}
public void setAbout(List<AboutObject> about) {
this.about = about;
}
}
AboutObject.class
public class AboutObject {
private String title;
private String uri;
private String contentViewerUrl;
private String timezone;
public String getTitle()
{
return this.title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getUri()
{
return this.uri;
}
public void setUri(String uri)
{
this.uri = uri;
}
public String getContentViewerUrl()
{
return this.contentViewerUrl;
}
public void setContentViewerUrl(String contentViewerUrl)
{
this.contentViewerUrl = contentViewerUrl;
}
public String getTimeZone()
{
return this.timezone;
}
public void setTimeZone(String timezone)
{
this.timezone = timezone;
}
}
Main.class
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
System.out.println("Contenu de in = " + in.toString());
ObjectMapper mapper = new ObjectMapper();
//Staff objStaff = new Staff();
System.out.println("Object to JSON in file");
mapper.writeValue(new File("output/file.json"), response);
System.out.println("Convert JSON string from file to Object");
//String about = mapper.readValue(new File("output/file.json"), String.class);
About about = mapper.readValue(new File("output/file.json"), About.class);
Error
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of About: no String-argument constructor/factory method to deserialize from String value ('{"about":{"title":"NiFi","version":"1.1.0","uri":"https://localhost:443/api/","contentViewerUrl":"/nifi-content-viewer/","timezone":"CET"}}') at [Source: output/file.json; line: 1, column: 1]
Thanks for you help
The test json you show doesn't have the array wrapper used in your About object. You're also missing the version field in your AboutObject and the timezone field uses the wrong case.
Your example worked when I updated your objects:
public class About {
private AboutObject about;
public AboutObject getAbout() {
return about;
}
public void setAbout(AboutObject about) {
this.about = about;
}
}
public class AboutObject {
private String title;
private String uri;
private String contentViewerUrl;
private String timezone;
private String version;
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUri() {
return this.uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getContentViewerUrl() {
return this.contentViewerUrl;
}
public void setContentViewerUrl(String contentViewerUrl) {
this.contentViewerUrl = contentViewerUrl;
}
public String getTimezone() {
return timezone;
}
public void setTimezone(String timezone) {
this.timezone = timezone;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
Test:
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
String obj = "{\"about\":{\"title\":\"NiFi\",\"version\":\"1.1.0\",\"uri\":\"https://localhost:443/api/\",\"contentViewerUrl\":\"/nifi-content-viewer/\",\"timezone\":\"CET\"}}";
About about = mapper.readValue(obj, About.class);
}
My json request is as follows
{
"division":"XX",
"category":"XX",
"operation":"XXX",
"transactionId":"XX",
"trackNumber":"XXx",
"attentionReason":"",
"carNeedAttention":"",
"chargableDamage":"X",
"missingItems":"",
"offences":"N",
"outInAgentNumber":"XX",
"cList":{
{
"id":"230",
"elementCode":"XXX",
"value":"XXX",
"comment":"XX",
"label":"",
"uiComponent":"",
"featureType":""
}
},
"outInCprNumber":"XX",
"outInDate":"",
"outInDuration":"",
"outInFuel":"75",
"outInKm":"9999",
"outInRem1":"",
"outInRem2":"",
"outInRem3":"",
"userName":"XX",
"vehicleRetBy":""
}
I have a spring rest controller class
#Controller
#RequestMapping("/services")
public class CheckListController {
#RequestMapping(value = "/checkList", method = RequestMethod.POST, consumes="application/json",produces="application/json")
public ModelMap updateCheckList(#RequestBody CheckList checkList){
ModelMap modelMap = new ModelMap();
return modelMap;
}
}
CheckList class is as follows
import java.util.List;
public class CheckList {
String division;
String category;
String operation;
String transactionId;
String trackNumber;
String attentionReason;
String carNeedAttention;
String chargableDamage;
String missingItems;
String offences;
String outInAgentNumber;
List<MetaData> cList;
String outInCprNumber;
String outInDate;
String outInDuration;
String outInFuel;
String outInKm;
String outInRem1;
String outInRem2;
String outInRem3;
String userName;
String vehicleRetBy;
String updateMasterImage;
public String getDivision() {
return division;
}
public void setDivision(String division) {
this.division = division;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
public String getTransactionId() {
return transactionId;
}
public void setTransactionId(String transactionId) {
this.transactionId = transactionId;
}
public String getTrackNumber() {
return trackNumber;
}
public void setTrackNumber(String trackNumber) {
this.trackNumber = trackNumber;
}
public String getAttentionReason() {
return attentionReason;
}
public void setAttentionReason(String attentionReason) {
this.attentionReason = attentionReason;
}
public String getCarNeedAttention() {
return carNeedAttention;
}
public void setCarNeedAttention(String carNeedAttention) {
this.carNeedAttention = carNeedAttention;
}
public String getChargableDamage() {
return chargableDamage;
}
public void setChargableDamage(String chargableDamage) {
this.chargableDamage = chargableDamage;
}
public String getMissingItems() {
return missingItems;
}
public void setMissingItems(String missingItems) {
this.missingItems = missingItems;
}
public String getOffences() {
return offences;
}
public void setOffences(String offences) {
this.offences = offences;
}
public List<MetaData> getcList() {
return cList;
}
public void setcList(List<MetaData> cList) {
this.cList = cList;
}
// public AccessoryList getAccessoryList() {
// return accessoryList;
// }
//
// public void setAccessoryList(AccessoryList accessoryList) {
// this.accessoryList = accessoryList;
// }
public String getOutInCprNumber() {
return outInCprNumber;
}
public void setOutInCprNumber(String outInCprNumber) {
this.outInCprNumber = outInCprNumber;
}
public String getOutInDate() {
return outInDate;
}
public void setOutInDate(String outInDate) {
this.outInDate = outInDate;
}
public String getOutInRem1() {
return outInRem1;
}
public void setOutInRem1(String outInRem1) {
this.outInRem1 = outInRem1;
}
public String getOutInRem2() {
return outInRem2;
}
public void setOutInRem2(String outInRem2) {
this.outInRem2 = outInRem2;
}
public String getOutInRem3() {
return outInRem3;
}
public void setOutInRem3(String outInRem3) {
this.outInRem3 = outInRem3;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getVehicleRetBy() {
return vehicleRetBy;
}
public void setVehicleRetBy(String vehicleRetBy) {
this.vehicleRetBy = vehicleRetBy;
}
public String getUpdateMasterImage() {
return updateMasterImage;
}
public void setUpdateMasterImage(String updateMasterImage) {
this.updateMasterImage = updateMasterImage;
}
public String getOutInAgentNumber() {
return outInAgentNumber;
}
public void setOutInAgentNumber(String outInAgentNumber) {
this.outInAgentNumber = outInAgentNumber;
}
public String getOutInDuration() {
return outInDuration;
}
public void setOutInDuration(String outInDuration) {
this.outInDuration = outInDuration;
}
public String getOutInFuel() {
return outInFuel;
}
public void setOutInFuel(String outInFuel) {
this.outInFuel = outInFuel;
}
public String getOutInKm() {
return outInKm;
}
public void setOutInKm(String outInKm) {
this.outInKm = outInKm;
}
}
MetaData is as folows
public class MetaData{
Integer id;
String label;
String uiComponent;
String featureType;
String value;
String comment;
String elementCode;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void setId(int id)
{
this.id = id;
}
public String getLabel()
{
return label;
}
public void setLabel(String label)
{
this.label = label;
}
public String getUiComponent()
{
return uiComponent;
}
public void setUiComponent(String uiComponent)
{
this.uiComponent = uiComponent;
}
public String getFeatureType()
{
return featureType;
}
public void setFeatureType(String featureType)
{
this.featureType = featureType;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getElementCode() {
return elementCode;
}
public void setElementCode(String elementCode) {
this.elementCode = elementCode;
}
}
But when i submitting the json request it is giving 415 unsuporrted media type error.
What is wrong with this code. Do anybody havve the answer. Thanks in advance.
Nothing with the code. You just need to make sure that Your POST request has the HTTP Content-Type header set to "application/json".
If you use curl to POST the data you can use the following parameter to set the header value:
curl -H "Content-Type:application/json"
Add an Accept header too:
curl -H "Content-Type:application/json" -H "Accept:application/json"
I am a bit lost here,
I have a JSON string like this:
{
"type":"fuu",
"message":"bar",
"data":{
"5":{
"post":"foo",
"type":"bar",
},
"0":{
"post":"foo",
"type":"bar",
},
"1":{
"post":"foo",
"type":"bar",
},
// and so on...
}
}
Please how do I parse it into POJOs using Gson? (I need to get the list of objects)
I am a bit confused by the number in front of the elements of the list of objects....
Try this -
Pojo.java
import java.util.Map;
public class Pojo {
private String type;
private String message;
private Map<Integer, InnerPojo> data;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Map<Integer, InnerPojo> getData() {
return data;
}
public void setData(Map<Integer, InnerPojo> data) {
this.data = data;
}
#Override
public String toString() {
return "Pojo [type=" + type + ", message=" + message + ", data=" + data
+ "]";
}
}
InnerPojo.java
public class InnerPojo {
private String type;
private String post;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
#Override
public String toString() {
return "InnerPojo [type=" + type + ", post=" + post + "]";
}
}
Main.java
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.testgson.beans.Pojo;
public class Main {
private static Gson gson;
static {
gson = new GsonBuilder().create();
}
public static void main(String[] args) {
String j = "{\"type\": \"fuu\", \"message\": \"bar\", \"data\":{ \"0\":{\"post\": \"foo\", \"type\": \"bar\"}, \"1\":{\"post\": \"foo\", \"type\": \"bar\"}, \"5\":{\"post\": \"foo\", \"type\": \"bar\"}}}";
Pojo p = gson.fromJson(j, Pojo.class);
System.out.println(p);
}
}
And Result is -
Pojo [type=fuu, message=bar, data={0=InnerPojo [type=bar, post=foo], 1=InnerPojo [type=bar, post=foo], 5=InnerPojo [type=bar, post=foo]}]
For the "data" part, I'd try to parse it into a Map<Integer, TypedPost> structure, see this thread for instructions.
I'm just trying to parse a json input, which is an implementation of AbstractList, containing custom objects. But, for some reason, when it hits the deserializer for CustomerDealerRecord, it never gets past the first node, and then ends up throwing a StackOverflowException. I've been pounding at this for like 4 hours, and I've done numerous different Google searches to no avail. So, my last ditch effort is coming here. Any light you all can shed on this will be much appreciated. Thank you. The code is below.
public class PhoneDeserializer extends StdDeserializer<Phone>{
public PhoneDeserializer() {
super(Phone.class);
}
#SuppressWarnings("unchecked")
#Override
public Phone deserialize(JsonParser jp,
DeserializationContext ctx) throws IOException,
JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
return mapper.readValue(jp, Phone.class);
}
}
--
public class CustomerDealerRecordDeserializer extends StdDeserializer<CustomerDealerRecord>{
public CustomerDealerRecordDeserializer() {
super(CustomerDealerRecord.class);
}
#SuppressWarnings("unchecked")
#Override
public CustomerDealerRecord deserialize(JsonParser jp,
DeserializationContext ctx) throws IOException,
JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
return mapper.readValue(jp, CustomerDealerRecord.class);
}
}
-- This is the Custom List
public class CustomerDealerRecordList extends AbstractList<CustomerDealerRecord> {
private List<CustomerDealerRecord> records = new ArrayList<CustomerDealerRecord>();
#Override
public CustomerDealerRecord get(int index) {
// TODO Auto-generated method stub
return records.get(index);
}
#Override
public int size() {
// TODO Auto-generated method stub
return records.size();
}
public boolean add(CustomerDealerRecord cdr){
return records.add(cdr);
}
}
-- This is the Controller method
#SuppressWarnings("unchecked")
public String getCustomerDealerReportAsExcel(HttpServletRequest req, HttpServletResponse resp){
VelocityContext vc = new VelocityContext();
vc.put("response", resp);
// Jackson stuff
CustomerDealerRecordDeserializer deser = new CustomerDealerRecordDeserializer();
PhoneDeserializer phoneDeser = new PhoneDeserializer();
// We have to create a module for the alias'ed class
SimpleModule cdrModule = new SimpleModule("CustomerDealerRecord", new Version(1,0,0,null));
SimpleModule phoneModule = new SimpleModule("Phone", new Version(1,0,0,null));
// Add the deserializer to the module
cdrModule.addDeserializer(CustomerDealerRecord.class, deser);
phoneModule.addDeserializer(Phone.class, phoneDeser);
// Now, create our mapper and then register the module to it.
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(cdrModule);
mapper.registerModule(phoneModule);
CustomerDealerRecordList list = null;
try {
JsonParser jp = mapper.getJsonFactory().createJsonParser(req.getParameter("json"));
list = mapper.readValue(jp, CustomerDealerRecordList.class);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return format(req, vc, "reports/customer_dealer_report_excel");
}
-- This is the Phone Model object
public class Phone {
public Phone(String areaCode, String phone){
this.areaCode = areaCode;
this.phoneNumber = phone;
}
private String areaCode;
private String phoneNumber;
public String getAreaCode() {
return areaCode;
}
public void setAreaCode(String areaCode) {
this.areaCode = areaCode;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
-- This is the CustomerDealerRecord Model object
public class CustomerDealerRecord {
private String fleetName;
private String fleetNumber;
private String dealerName;
private String dealerNumber;
private String territoryName;
private String territoryNumber;
private String city;
private String state;
private Date downTime;
private String failureDescription;
private String tireManufacturer;
private String tireSize;
private String tireType;
private String tirePosition;
private String category;
private String callerName;
private Phone callerPhone;
private String caseNumber;
private Date caseCloseDate;
private String poNumber;
private String truckNumber;
private String trailerNumber;
private String tractorNumber;
private String serviceDetailStatus;
private String refusalReason;
public String getFleetName() {
return fleetName;
}
public void setFleetName(String fleetName) {
this.fleetName = fleetName;
}
public String getFleetNumber() {
return fleetNumber;
}
public void setFleetNumber(String fleetNumber) {
this.fleetNumber = fleetNumber;
}
public String getDealerName() {
return dealerName;
}
public void setDealerName(String dealerName) {
this.dealerName = dealerName;
}
public String getDealerNumber() {
return dealerNumber;
}
public void setDealerNumber(String dealerNumber) {
this.dealerNumber = dealerNumber;
}
public String getTerritoryName() {
return territoryName;
}
public void setTerritoryName(String territoryName) {
this.territoryName = territoryName;
}
public String getTerritoryNumber() {
return territoryNumber;
}
public void setTerritoryNumber(String territoryNumber) {
this.territoryNumber = territoryNumber;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Date getDownTime() {
return downTime;
}
public void setDownTime(Date downTime) {
this.downTime = downTime;
}
public String getFailureDescription() {
return failureDescription;
}
public void setFailureDescription(String failureDescription) {
this.failureDescription = failureDescription;
}
public String getTireManufacturer() {
return tireManufacturer;
}
public void setTireManufacturer(String tireManufacturer) {
this.tireManufacturer = tireManufacturer;
}
public String getTireSize() {
return tireSize;
}
public void setTireSize(String tireSize) {
this.tireSize = tireSize;
}
public String getTireType() {
return tireType;
}
public void setTireType(String tireType) {
this.tireType = tireType;
}
public String getTirePosition() {
return tirePosition;
}
public void setTirePosition(String tirePosition) {
this.tirePosition = tirePosition;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getCallerName() {
return callerName;
}
public void setCallerName(String callerName) {
this.callerName = callerName;
}
public Phone getCallerPhone() {
return callerPhone;
}
public void setCallerPhone(Phone callerPhone) {
this.callerPhone = callerPhone;
}
public String getCaseNumber() {
return caseNumber;
}
public void setCaseNumber(String caseNumber) {
this.caseNumber = caseNumber;
}
public Date getCaseCloseDate() {
return caseCloseDate;
}
public void setCaseCloseDate(Date caseCloseDate) {
this.caseCloseDate = caseCloseDate;
}
public String getPoNumber() {
return poNumber;
}
public void setPoNumber(String poNumber) {
this.poNumber = poNumber;
}
public String getTruckNumber() {
return truckNumber;
}
public void setTruckNumber(String truckNumber) {
this.truckNumber = truckNumber;
}
public String getTrailerNumber() {
return trailerNumber;
}
public void setTrailerNumber(String trailerNumber) {
this.trailerNumber = trailerNumber;
}
public String getTractorNumber() {
return tractorNumber;
}
public void setTractorNumber(String tractorNumber) {
this.tractorNumber = tractorNumber;
}
public String getServiceDetailStatus() {
return serviceDetailStatus;
}
public void setServiceDetailStatus(String serviceDetailStatus) {
this.serviceDetailStatus = serviceDetailStatus;
}
public String getRefusalReason() {
return refusalReason;
}
public void setRefusalReason(String refusalReason) {
this.refusalReason = refusalReason;
}
}
-- Sample JSON
[
{
"fleetName":"sycamore specialzed carriers",
"fleetNumber":"CF00002760",
"dealerName":null,
"dealerNumber":null,
"territoryName":null,
"territoryNumber":null,
"city":null,
"state":null,
"downTime":"3000-01-01",
"failureDescription":null,
"tireManufacturer":"Continental Tire",
"tireSize":"10.00R15",
"tireType":"DRIVE",
"tirePosition":"LFO",
"category":"Dealer Location Information",
"callerName":"ANN RENNER",
"callerPhone":{
"areaCode":null,
"phoneNumber":null
},
"caseNumber":"189354",
"caseCloseDate":null,
"poNumber":null,
"truckNumber":null,
"trailerNumber":null,
"tractorNumber":null,
"serviceDetailStatus":"CAN",
"refusalReason":"Response time"
},
{
"fleetName":"sycamore specialzed carriers",
"fleetNumber":"CF00002760",
"dealerName":null,
"dealerNumber":null,
"territoryName":null,
"territoryNumber":null,
"city":null,
"state":null,
"downTime":"3000-01-01",
"failureDescription":null,
"tireManufacturer":null,
"tireSize":null,
"tireType":null,
"tirePosition":null,
"category":"Dealer Location Information",
"callerName":"ANN RENNER",
"callerPhone":{
"areaCode":null,
"phoneNumber":null
},
"caseNumber":"189354",
"caseCloseDate":null,
"poNumber":null,
"truckNumber":null,
"trailerNumber":null,
"tractorNumber":null,
"serviceDetailStatus":"ACT",
"refusalReason":null
},
{
"fleetName":"sycamore specialzed carriers",
"fleetNumber":"CF00002760",
"dealerName":null,
"dealerNumber":null,
"territoryName":null,
"territoryNumber":null,
"city":null,
"state":null,
"downTime":"3000-01-01",
"failureDescription":null,
"tireManufacturer":"Continental Tire",
"tireSize":"295/75R22.5",
"tireType":"BIAS",
"tirePosition":"LMI",
"category":"Service Call",
"callerName":" ",
"callerPhone":{
"areaCode":null,
"phoneNumber":null
},
"caseNumber":"189240",
"caseCloseDate":null,
"poNumber":null,
"truckNumber":null,
"trailerNumber":null,
"tractorNumber":null,
"serviceDetailStatus":"CAN",
"refusalReason":"Other"
},
{
"fleetName":"sycamore specialzed carriers",
"fleetNumber":"CF00002760",
"dealerName":null,
"dealerNumber":null,
"territoryName":null,
"territoryNumber":null,
"city":null,
"state":null,
"downTime":"3000-01-01",
"failureDescription":null,
"tireManufacturer":"Continental Tire",
"tireSize":"295/75R22.5",
"tireType":"DRIVE",
"tirePosition":"LMI",
"category":"Service Call",
"callerName":" ",
"callerPhone":{
"areaCode":null,
"phoneNumber":null
},
"caseNumber":"189240",
"caseCloseDate":null,
"poNumber":null,
"truckNumber":null,
"trailerNumber":null,
"tractorNumber":null,
"serviceDetailStatus":"ACT",
"refusalReason":null
},
{
"fleetName":"TEST CUSTOMER",
"fleetNumber":"123ee22a",
"dealerName":null,
"dealerNumber":null,
"territoryName":null,
"territoryNumber":null,
"city":null,
"state":null,
"downTime":"3000-01-01",
"failureDescription":null,
"tireManufacturer":null,
"tireSize":null,
"tireType":null,
"tirePosition":null,
"category":"Service Call",
"callerName":"JASON MA",
"callerPhone":{
"areaCode":"123",
"phoneNumber":"222"
},
"caseNumber":"189328",
"caseCloseDate":"2012-01-03",
"poNumber":null,
"truckNumber":null,
"trailerNumber":null,
"tractorNumber":null,
"serviceDetailStatus":"ACT",
"refusalReason":"Other"
},
{
"fleetName":"TEST CUSTOMER",
"fleetNumber":"123ee22a",
"dealerName":null,
"dealerNumber":null,
"territoryName":null,
"territoryNumber":null,
"city":"ST LOUIS",
"state":"MO",
"downTime":"3000-01-01",
"failureDescription":"REPAIR IF POSSIBLE",
"tireManufacturer":"Continental Tire",
"tireSize":"11R22.5",
"tireType":"RADIAL",
"tirePosition":"LRI",
"category":"Service Call",
"callerName":"BJ TEST",
"callerPhone":{
"areaCode":"314",
"phoneNumber":"592-3129"
},
"caseNumber":"189341",
"caseCloseDate":"2012-06-19",
"poNumber":null,
"truckNumber":null,
"trailerNumber":"34",
"tractorNumber":"12",
"serviceDetailStatus":"CAN",
"refusalReason":"Product not available"
},
{
"fleetName":"TEST CUSTOMER",
"fleetNumber":"123ee22a",
"dealerName":null,
"dealerNumber":null,
"territoryName":null,
"territoryNumber":null,
"city":"ST LOUIS",
"state":"MO",
"downTime":"3000-01-01",
"failureDescription":"REPAIR IF POSSIBLE",
"tireManufacturer":"Continental Tire",
"tireSize":"11R22.5",
"tireType":"RADIAL",
"tirePosition":"LRI",
"category":"Service Call",
"callerName":"BJ TEST",
"callerPhone":{
"areaCode":"314",
"phoneNumber":"592-3129"
},
"caseNumber":"189341",
"caseCloseDate":"2012-06-19",
"poNumber":null,
"truckNumber":null,
"trailerNumber":"34",
"tractorNumber":"12",
"serviceDetailStatus":"ACT",
"refusalReason":null
}
]
As #bmargulies commented, the PhoneDeserializer calls (indirectly) the PhoneDeserializer. The line mapper.readValue(jp, Phone.class); will let Jackson resolve the deserializer for the Phone class, which happens to be PhoneDeserializer. You don't need the Phonedeserializer class at all, Jackson will handle your phone class properly.
Yeah, initially I didn't use the deserializer, and as #pgelinas said, it wasn't needed. That was where I got after playing around with things. It didn't work. The underlying problem was that I had to go back to using mapper.convertValue and give the Phone class a public no arg constructor. Basically, I'm an idiot and was just over-analyzing the simple.