Java – Spring REST consuming JSON uppercase vs lowercase

javajsonresttemplatespringspring-mvc

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.

Best Answer

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

@JsonProperty("phone")
public String PHONE;