Spring REST消耗JSON大写与小写 [英] Spring REST consuming JSON uppercase vs lowercase

查看:707
本文介绍了Spring REST消耗JSON大写与小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建简单的Web服务,即从URL读取JSON并将其返回.我关注了spring.io教程.我可能缺少有关命名约定的内容?

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没有很好的命名约定.有些值是大写的,有些是小写的.我知道要与restTemplate正确匹配,我需要遵循这些名称.

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.

我的对象结构:

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
}

这是我使用代码的部分

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

我缺少什么?看起来使用了私有必需的经典lowerUpper约定,但是如果我进行更改,它将无法与JSON正确匹配.我能以春季的方式命名吗?

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;

非常感谢.

推荐答案

您可以使用@JsonProperty批注覆盖变量名称.

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

@JsonProperty("phone")
public String PHONE;

这篇关于Spring REST消耗JSON大写与小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆