Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring REST consuming JSON uppercase vs lowercase

I am trying to create simple webservice, that is reading JSON from URL and gives it back. I followed spring.io tutorial about this. I am probably missing something about naming conventions?

JSON I use doesn't have nice naming convention. Some values are in uppercase, some lowercase other are mixed. What I understood for proper matching with restTemplate I need to follow these names.

My object structure:

public class Page {
private String name; //works
private String about; // works
private String PHONE; //does not work
private String Website; //does not work

//getters and setters
}

If I change them to public, they start to work.

public class Page {
private String name; //works
private String about; // works
public String PHONE; //works
public String Website; //works

//getters and setters
}

This is the part of code where I use that

@RequestMapping(value = "/Test")
public Bubble getBubbleInfo(){
RestTemplate restTemplate = new RestTemplate();
Page page= restTemplate.getForObject("myURL", Page.class);
    return page;
}

What I am missing? It looks that using private required classical lowerUpper convention but if I change that it won't be properly matched with the JSON. Can I name it somehow for spring?

//spring, this is PHONE
public String phone;

Thanks a lot.

like image 625
Elis.jane Avatar asked May 13 '26 00:05

Elis.jane


1 Answers

You can use @JsonProperty annotation to override the variable name.

@JsonProperty("phone")
public String PHONE;
like image 107
Mithun Avatar answered May 15 '26 13:05

Mithun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!