abraham
abraham

Reputation: 1

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.

Upvotes: 0

Views: 1036

Answers (2)

codeaholicguy
codeaholicguy

Reputation: 1691

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

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

Upvotes: 1

Robby Cornelissen
Robby Cornelissen

Reputation: 97152

Annotate your fields with @JsonProperty, e.g.

@JsonProperty("first_Name")
private String firstName;

Upvotes: 0

Related Questions