Long value is always null while using RestTemplate in Spring Boot - java

I am trying to parse this json
{
"total_count": 3,
"offset": 0,
"limit": 50,
"notifications": [
{
"isEdge": false,
"large_icon": "",
"priority": 10,
"queued_at": 1579590097,
"remaining": 0,
"send_after": 1579590097,
"completed_at": 1579590110,
"small_icon": "",
"successful": 1,
"received": 1,
"tags": null,
"filters": null,
"template_id": null,
"ttl": null,
"url": "",
"contents": {
"en": "MoMo House presents MoMo eating competition. Come join us on Jan 26th
for the biggest MoMo eating competition ever."
}
"web_url": null,
"app_url": null,
"web_buttons": null,
"web_push_topic": null,
"wp_sound": "",
"wp_wns_sound": "",
"ios_attachments": null
}
]
}
This is my code to parse it
public ResponseEntity<HashMap<String,List<Notification>>> getAllNotifications(){
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Authorization",Constants.ONESIGNAL_REST_KEY);
HttpEntity<String> entity = new HttpEntity<String>("body", httpHeaders);
ResponseEntity<OneSignalResponse> oneSignalResponseResponseEntity = restTemplate.exchange(Constants.ONESIGNAL_URL, HttpMethod.GET,entity,OneSignalResponse.class);
OneSignalResponse oneSignalResponse=oneSignalResponseResponseEntity.getBody();
System.out.println("Title "+oneSignalResponse.getNotifications().get(0).getHeadings().getEn());
List<Notification> notifications = new ArrayList<>();
for(NotificationsItem oneSig : oneSignalResponse.getNotifications()){
Notification notification = new Notification();
notification.setId(oneSig.getId());
notification.setTitle(oneSig.getHeadings().getEn());
notification.setMessage(oneSig.getContents().getEn());
System.out.println("value of sent time "+oneSig.getCompletedAt()); //this is null
Date date = new Date(sentTime);
notification.setDate(localDateTime.toString());
notifications.add(notification);
}
HashMap<String,List<Notification>> hashMap = new HashMap<>();
hashMap.put("notifications",notifications);
return new ResponseEntity<>(hashMap, HttpStatus.OK);
}
And this is my POJO
public class NotificationsItem{
#SerializedName("isEdge")
private boolean isEdge;
#SerializedName("delayed_option")
private String delayedOption;
#SerializedName("queued_at")
private int queuedAt;
#SerializedName("failed")
private int failed;
#SerializedName("priority")
private int priority;
#SerializedName("remaining")
private int remaining;
#SerializedName("tags")
private Object tags;
#SerializedName("canceled")
private boolean canceled;
#SerializedName("chrome_icon")
private String chromeIcon;
#SerializedName("completed_at")
private Object completedAt;
#SerializedName("isWP")
private boolean isWP;
#SerializedName("send_after")
private int sendAfter;
#SerializedName("adm_sound")
private String admSound;
#SerializedName("chrome_web_image")
private String chromeWebImage;
#SerializedName("contents")
private Contents contents;
#SerializedName("converted")
private int converted;
#SerializedName("android_visibility")
private int androidVisibility;
#SerializedName("headings")
private Headings headings;
#SerializedName("adm_group")
private String admGroup;
#SerializedName("chrome_web_badge")
private String chromeWebBadge;
#SerializedName("spoken_text")
private SpokenText spokenText;
#SerializedName("ios_badgeCount")
private int iosBadgeCount;
#SerializedName("isChrome")
private boolean isChrome;
#SerializedName("alexa_ssml")
private Object alexaSsml;
#SerializedName("alexa_display_title")
private Object alexaDisplayTitle;
#SerializedName("android_accent_color")
private String androidAccentColor;
#SerializedName("android_group")
private String androidGroup;
#SerializedName("include_external_user_ids")
private Object includeExternalUserIds;
#SerializedName("isAdm")
private boolean isAdm;
#SerializedName("successful")
private int successful;
#SerializedName("web_push_topic")
private Object webPushTopic;
#SerializedName("adm_large_icon")
private String admLargeIcon;
#SerializedName("content_available")
private boolean contentAvailable;
#SerializedName("adm_small_icon")
private String admSmallIcon;
#SerializedName("buttons")
private Object buttons;
#SerializedName("app_url")
private Object appUrl;
#SerializedName("ios_badgeType")
private String iosBadgeType;
#SerializedName("delivery_time_of_day")
private String deliveryTimeOfDay;
#SerializedName("large_icon")
private String largeIcon;
#SerializedName("big_picture")
private String bigPicture;
#SerializedName("filters")
private Object filters;
#SerializedName("ttl")
private Object ttl;
#SerializedName("url")
private String url;
#SerializedName("include_player_ids")
private Object includePlayerIds;
#SerializedName("android_led_color")
private String androidLedColor;
#SerializedName("isWP_WNS")
private boolean isWPWNS;
#SerializedName("web_url")
private Object webUrl;
#SerializedName("adm_group_message")
private AdmGroupMessage admGroupMessage;
#SerializedName("adm_big_picture")
private String admBigPicture;
#SerializedName("small_icon")
private String smallIcon;
#SerializedName("template_id")
private Object templateId;
#SerializedName("web_buttons")
private Object webButtons;
#SerializedName("isAndroid")
private boolean isAndroid;
#SerializedName("isIos")
private boolean isIos;
#SerializedName("errored")
private int errored;
//getters setters
}
As the json is large I have included only those that matters and the POJO might not be accordingly here.
So when I call the method, I get the value of contents but I get null when I try to get completedAt this is shown as comment in the code too. So I am able to parse some parts perfectly but I am not able to parse some. I don't know what I am doing wrong so any help would be highly appreciated. Thanks

#SerializedName is a Gson annotation. Spring normally uses Jackson. The Jackson annotation for this would be #JsonProperty("name").

You have to change the datatype for the variable completedAt
#SerializedName("completed_at")
private Object completedAt;
To
#SerializedName("completed_at")
private Long completedAt;

Related

how to split different json properties not wrapped in json object into multiple model java classes

I have a big payload with different key property values. I tried creating model separate model classes based on the object on the portal. But there is no separate json object wrapped for such properties. My question is how to split the big json object into separate java classes.
Payload:
{
"documentType": "BDFFIIJCDSAAA",
"documentGroup": "394EIIJCDSAAA",
"ply": "0",
"numberOfCopies": "1",
"tpDocNumbers": [
"507317219"
],
"product": {
"vessel": null,
"voyage": null,
"countryOfReceipt": null,
"includeCountryOfReceipt": "true",
"countryOfLoading": null,
"includeCountryOfLoading": "true",
"countryOfDischarge": null,
"includeCountryOfDischarge": "true",
"iountryOfDelivery": null,
"includeCountryOfDelivery": "true",
"tradeLane": null,
"includeTradeLane": "true",
"placeOfReceipt": null,
"includePlaceOfReceipt": "true",
"loadPort": null,
"includeLoadPort": "true",
"dischargePort": null,
"includeDischargePort": "true",
"placeOfDelivery": null,
"includePlaceOfDelivery": "true"
},
"isTaskStatusBlank": "true",
"isTaskStatusOpen": "true",
"isTaskStatusClosed": "true",
"recepient": "Direct",
"recepientPort": null,
"communication": "Email",
"contact": "avinash.m#maersk.com",
"subject": "latest123",
"docBrokerDomain": "",
"printerName": "",
"templateDomain": "",
"priority": "",
"freeText": ""
}
Java Model classes -
public class A {
#JsonProperty("DocumentType")
private String documentType;
#JsonProperty("DocumentGroup")
private String documentGroup;
private Ply ply;
#JsonProperty("PartyRoles")
private String partyRoles;
#JsonProperty("CustomerId")
private String customerId;
#JsonProperty("PartyRoleTypeId")
private String partyRoleTypeId;
#JsonProperty("PartyRefNo")
private String partyRefNo;
#JsonProperty("TPDocNumbers")
private List<String> tpDocNumbers;
#JsonProperty("Product")
private Product product;
private Cargo cargoInfo;
private TaskCriteria taskCriteria;
private Recipient recipientInfo;
#JsonProperty("DocBrokerDomain")
private String docBrokerDomain;
#JsonProperty("PrinterName")
private String printerName;
#JsonProperty("TemplateDomain")
private String templateDomain;
#JsonProperty("Priority")
private String priority;
#JsonProperty("RequestType")
private String requestType;
#JsonProperty("FreeText")
private String freeText;
}
public class Ply {
#JsonProperty("Ply")
private int plyNumber;// plySelection
#JsonProperty("NumberOfCopies")
private int copies;// copySelection
#JsonProperty("freightSelection")
private String freightSelection;
}
This doesn't work and I get the error - "no String-argument constructor/factory method to deserialize from String value ('0')\n at" when it encounters the Ply Object class.
Since in your JSON you have Ply properties in the same level as all the other A class properties, you need to use #JsonUnwrapped in the private Ply ply; property. Additionally, given that Jackson ObjectMapper is case sensitive while deserializing you also need to adjust the #JsonProperty annotations (if the property name match JSON property you don't even need the annotation):
public class A {
private String documentType;
private String documentGroup;
#JsonUnwrapped
private Ply ply;
private String partyRoles;
private String customerId;
private String partyRoleTypeId;
private String partyRefNo;
private List<String> tpDocNumbers;
private Product product;
private Cargo cargoInfo;
private TaskCriteria taskCriteria;
private Recipient recipientInfo;
private String docBrokerDomain;
private String printerName;
private String templateDomain;
private String priority;
private String requestType;
private String freeText;
}
You also need to adjust Ply to match JSON properties:
public class Ply {
#JsonProperty("ply")
private int plyNumber;
#JsonProperty("numberOfCopies")
private int copies;
private String freightSelection;
}
The problem you are facing is because your JSON has ply as a string value and your POJO is using Ply as an Object of Ply class.
Solution:
change your POJO from Ply object to ply integer value.
change your Json from ply string to Object of Ply with Ply number as a property value.
You can also use ObjectMapper readTree to read JSON as a tree and create your mapping

How can I read data and put into model in java?

I need to get some specific data from rest to my model.
Data I get from rest :
[{"id":595,"name":"TransXChange20210805113332","prosystem_file_id":null,"dataset":16,"creator":113,"editor":113,"created":"2021-08-05T09:45:21.444949Z","edited":"2021-08-05T09:45:27.542152Z","update_from_url":false,"description":"Rozkłady jazdy komunikacji miejskiej ważne od 08.08.2021","file":"https://otwartedane.erzeszow.pl/media/resources/transxchange20210805113332.xml","link":null,"extension":"XML","data_type":"[]","file_name":"transxchange20210805113332.xml","chart_info":null,"map_info":null,"score":4,"public":true,"licence":"other-open","connection_dict":null,"selection_string":null,"db_type":"POSTGRESQL","type":"file","dbview_cache_minutes":1,"preview_base64":null,"gpkg_display_info":null,"archive_to_csv":false},{"id":538,"name":"TransXChange20210513082611","prosystem_file_id":null,"dataset":16,"creator":113,"editor":113,"created":"2021-05-13T06:28:50.233464Z","edited":"2021-07-28T08:52:06.695966Z","update_from_url":false,"description":"Rozkłady jazdy komunikacji miejskiej ważne od 15.05.2021","file":"https://otwartedane.erzeszow.pl/media/resources/transxchange20210513082611.xml","link":null,"extension":"XML","data_type":"[]","file_name":"transxchange20210513082611.xml","chart_info":null,"map_info":null,"score":4,"public":true,"licence":"other-open","connection_dict":null,"selection_string":null,"db_type":"POSTGRESQL","type":"file","dbview_cache_minutes":1,"preview_base64":null,"gpkg_display_info":null,"archive_to_csv":false},{"id":544,"name":"TransXChange20210526143716","prosystem_file_id":null,"dataset":16,"creator":113,"editor":113,"created":"2021-05-26T12:40:42.587492Z","edited":"2021-07-28T08:52:04.417450Z","update_from_url":false,"description":"Rozkłady jazdy komunikacji miejskiej ważne od 01.06.2021","file":"https://otwartedane.erzeszow.pl/media/resources/transxchange20210526143716.xml","link":null,"extension":"XML","data_type":"[]","file_name":"transxchange20210526143716.xml","chart_info":null,"map_info":null,"score":4,"public":true,"licence":"other-open","connection_dict":null,"selection_string":null,"db_type":"POSTGRESQL","type":"file","dbview_cache_minutes":1,"preview_base64":null,"gpkg_display_info":null,"archive_to_csv":false}]
i got it in single line
my code in java :
RestTemplateBuilder builder = new RestTemplateBuilder();
String soc = builder.build().getForObject("https://otwartedane.erzeszow.pl/v1/datasets/16/resources/", String.class);
assert soc != null;
System.out.println(soc);
And the problem is I need to put them into model
My model:
public class Resource {
private long id;
private String name;
private long prosystem_file_id;
private int dataset;
private int creator;
private int editor;
private LocalDateTime created;
private LocalDateTime edited;
private boolean update_from_url;
private String description;
private String file;
private String link;
private String extension;
private String data_type;
private String file_name;
private String chart_info;
private String map_info;
private int score;
private boolean isPublic;
private String licence;
private String connection_dict;
private String selection_string;
private String db_type;
private String type;
private int dbview_cache_minutes;
private String preview_base64;
private String gpkg_display_info;
private boolean archive_to_csv;
All getters and setters are generated.
How can I put them into model for example List?
The problem is I get data like [{resource},{resource},{resource}].
As it states from the RestTemplate documentation
you can call to the getForObject method passing as an argument the desired deserialization class.
Something like:
RestTemplateBuilder builder = new RestTemplateBuilder();
Resource soc = builder.build().getForObject("https://otwartedane.erzeszow.pl/v1/datasets/16/resources/", Resource.class);
assert soc != null;
System.out.println(soc);
I have found answer to my question.
I need to assign my call to array of model.
Resource[] soc =builder.build().getForObject( URL , Resource[].class );

Unmarshal JSON-Array to object while ignoring null values

I get a JSON-Object as an array form an external source which looks like this:
[13823108,"Text1","Text2",null,null,1585730520000,1585753742000,null,null,"COMPLETED",null]
The array will always have null values at those positions, which is why I would like to ignore those.
My Java object is suppose to look like this:
#JsonFormat(shape = JsonFormat.Shape.ARRAY)
public class MyJsonObject {
private final String id;
private final String textField1;
private final String textField2;
private final Date started;
private final Date updated;
private final String status;
#JsonCreator
public MyJsonObject(
#JsonProperty("id") final String id,
#JsonProperty("textField1") final String textField1,
#JsonProperty("textField2") final String textField2,
#JsonProperty("started") final Date started,
#JsonProperty("updated") final Date updated,
#JsonProperty("status") final String status) {
this.id = id;
this.textField1 = textField1;
this.textField2 = textField2;
this.started = started;
this.updated = updated;
this.status = status;
}
[...]
}
When I try to unmarshal the Json the null values are used, which courses the additional values to be ignored:
MyJsonObject{id='13105603', textField1='Text1', textField2='Text2', started=null, updated=null, status='1569348774000'}
Is there a way to tell the ObjectMapper to ignore null values of the array? Can I achieve this without writing a specific Deserializer?
I already tried #JsonInclude(JsonInclude.Include.NON_NULL) with no effect.

JSON/GSON parsing returns null value for certain attributes [duplicate]

This question already has answers here:
Gson deserialisation generates NULL value
(2 answers)
Closed 6 years ago.
I've been trying to parse a JSON file using GSON to display the content. For attributes like ship name I am getting the appropriate results but for many of the others such as typeOfShipCargo, AISversion etc. I keep getting null values.
I have given the bulk of the code below. Please let me know if anything else needs to be added or not. Cheers.
JSON File
[
{
"idmessage": "27301",
"idsession": "362",
"time_stamp_system": "2017-01-20 14:51:14",
"NMEA_string": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"processed": "1",
"MMSI": "0000000001",
"AIS_version": "0",
"IMO_number": "xxxxxxxxxx",
"callSign": "ODLK1",
"name": "ODLXJ KWW",
"type_of_ship_and_cargo": "0",
"bow_to_possition_unit": "212",
"stern_to_possition_unit": "71",
"port_to_possition_unit": "22",
"starboard_to_possitio_unit": "22",
"type_of_position_fixing_divice": "1",
"ETA": "Test",
"destination": "",
"last_static_draught": "0",
"DTE": "127"
}
]
Parent class
public class Ship {
private String idmsg;
private String idsession;
private String systemTime;
private String NMEAstring;
private String processed;
private String MMSI;
public Ship(){}
public Ship(String idmsg, String idsession, String systemTime, String NMEAstring, String processed, String MMSI) {
this.idmsg = idmsg;
this.idsession = idsession;
this.systemTime = systemTime;
this.NMEAstring = NMEAstring;
this.processed = processed;
this.MMSI = MMSI;
}
//Getters and setters
}
Child class
public class ShipDetails extends Ship {
private String AISversion;
private String IMOnumber;
private String callSign;
private String name;
private String typeOfShipCargo;
private String bowToPositionUnit;
private String sternToPositionUnit;
private String portToPositionUnit;
private String starboardToPositionUnit;
private String typeOfPositionFixingDevice;
private String eta;
private String destination;
private String lastStaticDraught;
private String dte;
public ShipDetails(String idmsg, String idsession, String systemTime, String NMEAstring, String processed,
String MMSI, String AISversion, String IMOnumber, String callSign, String name, The rest...) {
super(idmsg, idsession, systemTime, NMEAstring, processed, MMSI);
this.AISversion = AISversion;
this.IMOnumber = IMOnumber;
this.callSign = callSign;
this.name = name;
this.typeOfShipCargo = typeOfShipCargo;
this.bowToPositionUnit = bowToPositionUnit;
this.sternToPositionUnit = sternToPositionUnit;
this.portToPositionUnit = portToPositionUnit;
this.starboardToPositionUnit = starboardToPositionUnit;
this.typeOfPositionFixingDevice = typeOfPositionFixingDevice;
this.eta = eta;
this.destination = destination;
this.lastStaticDraught = lastStaticDraught;
this.dte = dte;
}
//getters and setters
}
System class
public class ShipController {
private static ArrayList<ShipDetails> shipDet = new ArrayList<ShipDetails>();
public static void main(String[] args) throws IOException{
File inStream = new File("details.json");
Scanner read = new Scanner(inStream);
BufferedReader bf = new BufferedReader(new FileReader("positions.json"));
String json = "";
while (read.hasNext())
{
json += read.nextLine();
}
Gson gson = new Gson();
ShipDetails[] tempShip = gson.fromJson(json, ShipDetails[].class);
if(tempShip == null)
System.out.println("Null");
shipDet.addAll(Arrays.asList(tempShip));
shipPos.addAll(Arrays.asList(tempDetails));
read.close();
//Printing the attribute values. For AIS version it retuns null while for name it doesn't.
for (ShipDetails d : shipDet)
{
System.out.println(d.getAISversion());
}
}
The names of those fields don't match the field names in Java class (or their corresponding getters and setters). You can use SerializedName annotation to use non-default name for the field:
#SerializedName("type_of_ship_and_cargo")
//#SerializedName(alternate = "type_of_ship_and_cargo")
private String typeOfShipCargo;
Sometimes alternate part of the annotation is sufficient to only make deserialization flexible but use the default naming for serialization.

Gson to POJO, how to read the inner JSON Objects as part of the same POJO?

I have a JSON like below and would like to convert it using Gson to one POJO.
I am trying to figure out how to basically cut down the inner nested objects like desc to be treated as part of the same Java object, instead of creating a new POJO named Desc.
Is there a way to do this with Serialized Names to look into nested JSON objects?
Thanks in advance!
JSON to be converted to POJO
{
'name': name,
'desc': {
'country': country,
'city': city,
'postal': postal,
'street': street,
'substreet': substreet,
'year': year,
'sqm': sqm
},
'owner': [owner],
'manager': [manager],
'lease': {
'leasee': [
{
'userId': leaseeId,
'start': leaseeStart,
'end': leaseeeEnd
}
],
'expire': leaseExpire,
'percentIncrease': leasePercentIncrease,
'dueDate': dueDate
},
'deposit': {
'bank': 'Sample Bank',
'description': 'This is a bank'
}
}
Custom POJO
public class Asset {
private String mId;
#SerializedName("name")
private String mName;
private String mCountry;
private String mCity;
private String mPostal;
private String mStreet;
private String mSubstreet;
private int mYear;
private int mSqm;
#SerializedName("owner")
private List<String> mOwners;
#SerializedName("manager")
private List<String> mManagers;
private List<Leasee> mLeasees;
private DateTime mLeaseExpiration;
private int mPercentIncrease;
private int mDueDate;
private String mDepositBank;
private String mDepositDescription;
}

Categories

Resources