Spring REST consuming JSON uppercase vs lowercase

ε祈祈猫儿з 提交于 2019-12-11 02:59:42

问题


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.


回答1:


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

@JsonProperty("phone")
public String PHONE;


来源:https://stackoverflow.com/questions/26890398/spring-rest-consuming-json-uppercase-vs-lowercase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!