0
votes

Actually I try to invoke a get request with the restTemplate in Spring. Debbuging my application clearly shows that the JSON is downloaded but the automatic mapping does not work. My List of domain object includes only 0 values and null values. When I invoke the get request from the browser, I get the following response as JSON (I copied here the first two record out of the 3 192):

[{"OrderId":77862,"DateAdded":"2016-04-30T02:25:40.263","OrderItemCorpusId":"HUBW","OrderItemCorpusOriginalId":null,"OrderItemCurrency":"HUF","OrderItemExchangeRate":1.00000,"OrderItemOriginalLocation":"HU","OrderItemBuyLocation":"HU","OrderItemPrice":1337.80314,"OrderItemDiscountId":0,"OrderItemDiscountValue":"","DocumentId":25140,"Title":"Romana Gold 10. kötet","PublisherOriginalName":"Harlequin Kiadó","ISBN":"9789634073369"},{"OrderId":77864,"DateAdded":"2016-04-30T15:49:22.61","OrderItemCorpusId":"HUBW","OrderItemCorpusOriginalId":null,"OrderItemCurrency":"HUF","OrderItemExchangeRate":1.00000,"OrderItemOriginalLocation":"HU","OrderItemBuyLocation":"HU","OrderItemPrice":2748.03149,"OrderItemDiscountId":0,"OrderItemDiscountValue":"","DocumentId":25252,"Title":"Az eltűnt lány","PublisherOriginalName":"Harlequin Kiadó","ISBN":"9789634072423"}]

My POJO domain object which should keep the converted data from JSON:

@JsonIgnoreProperties(ignoreUnknown = true)
public class BandWTransaction {
private long OrderId;
private Date DateAdded;
private String OrderItemCurrency;
private double OrderItemExchangeRate;
private String OrderItemBuyLocation;

private double OrderItemPrice;

private String OrderItemDiscountValue;

private long DocumentId;
private String Title;
private String PublisherOriginalName;

private String ISBN;
//getters and setters

Finally the code snippet I use for the rest get request:

String startDate = new SimpleDateFormat("yyyy-MM-dd").format(start.getTime());
String endDate = new SimpleDateFormat("yyyy-MM-dd").format(end.getTime());
    UriComponents uri = UriComponentsBuilder.newInstance().scheme("http").host("www.bookandwalk.hu")
            .path("/api/AdminTransactionList").queryParam("password", "XXX")
            .queryParam("begindate", startDate).queryParam("enddate", endDate).queryParam("corpusid", "HUBW")
            .build().encode();

    LOG.log(Level.INFO, "{0} were called as a rest call", uri.toString());

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.set("User-Agent", "Anything");
    HttpEntity<String> entity = new HttpEntity<String>(headers);

    ResponseEntity<List<BandWTransaction>> transResponse = restTemplate.exchange(uri.toString(), HttpMethod.GET,
            entity, new ParameterizedTypeReference<List<BandWTransaction>>() {
            });

    List<BandWTransaction> transactions = transResponse.getBody();

When I debug the app I realized that the transactions list includes objects with full of null and 0 values. More precisely, there is no and objcet within the list having other values as 0 and null in the properties. I have also checked that spring boot automatically registered in the restTemplate.messageConverters ArrayList 9 HttpMessageConverter. The 7th element of this ArrayList is the org.springframework.http.converter.json.MappingJackson2HttpMessageConverter which supports the application/json and application/+json media types. Any idea is appreciated to solve this problem as I am newbie in spring and in JSON mapping in general.

2

2 Answers

3
votes

It seems you have a naming convention issue due to your field variables starts with a uppercase. When Jackson finds a pair getTitle/setTitleasumes that the name of this variable is title (starting with lowercase). Of course, if you change the capitalization of your variables, json properties and java variables has different names, so mapping still fails. The best solution is change your names to meet Java conventions, and use Jackson annotations to define your mappings.

@JsonProperty(value="OrderId")
private int orderId;
@JsonProperty(value="DateAdded")
private Date dateAdded;

Hope it helps.

1
votes

I can suggest you to write a test and check how fasterxml ObjectMapper read a json and unmarshall json to your object:

ObjectMapper objectMapper = new ObjectMapper();
String somestring = objectMapper.readValue("somestring", String.class);

just replace String with your class and "somestring" with your json. So you check if there is problem with it.

And try to use @JsonPropery cause all this capital letters fields start with looks messy:

@JsonIgnoreProperties(ignoreUnknown = true)
public class BandWTransaction {
    @JsonProperty("OrderId")
    private long OrderId;
    [...]

With this stuff I read json correct. You can come in from other side remove ignoring unknown properties:

@JsonIgnoreProperties(ignoreUnknown = true) //remove it and run test public class BandWTransaction {

and you get :

(11 known properties: "dateAdded", "orderItemExchangeRate", "documentId", "orderItemPrice", "orderId", "orderItemBuyLocation", "orderItemDiscountValue", "orderItemCurrency", "isbn", "title", "publisherOriginalName"])

So problem in variables naming and you can fix it with @JsonProperty