简体   繁体   中英

Rest Web Service - Object Mapper

I am working on Rest based application where I am creating the rest client. The problem is while sending the post request the object is expected to be 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. 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. Appreciate your help on this.

You should use @JsonProperty() in your User class: Example:

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

Annotate your fields with @JsonProperty , eg

@JsonProperty("first_Name")
private String firstName;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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