简体   繁体   English

REST Web服务-对象映射器

[英]Rest Web Service - Object Mapper

I am working on Rest based application where I am creating the rest client. 我正在基于Rest的应用程序中创建我的Rest客户端。 The problem is while sending the post request the object is expected to be JSON. 问题是在发送发布请求时,该对象应为JSON。

Class User{ String first_Name; String last_Name; //getters & setters }

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); String json = ow.writeValueAsString(object);

The above code returns with correct JSON format, however the underscore of the class attributes are getting eliminated. 上面的代码以正确的JSON格式返回,但是消除了类属性的下划线。 Eg. 例如。 I am expected the result to be like 我希望结果像

{"first_Name":"Joseph","last_Name":"Thomas"}

but the actual result is 但实际结果是

{"firstName":"Joseph","lastName":"Thomas"}. 

Can someone help me how to get the json with underscore. 有人可以帮助我如何使用下划线获取json。 Appreciate your help on this. 感谢您的帮助。

You should use @JsonProperty() in your User class: Example: 您应该在User类中使用@JsonProperty() :示例:

@JsonProperty("first_Name")
String first_Name; 
@JsonProperty("last_Name")
String first_Name;

Annotate your fields with @JsonProperty , eg @JsonProperty注释您的字段,例如

@JsonProperty("first_Name")
private String firstName;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM