I have snake case representation of the json data '{"first_name":"Michael", "last_name":"Jordan", "address_loc":{"city_name":"Mumbai", "street":"galino1"}}' . I want to consume this data by jersey + moxy based post method resource in form of pojo representation. Please suggest.Please also note that normal camel case representation of json is working using given following code snippet but I have the requirement of using snake case.
@XmlRootElement
public class Person {
private String firstName;
private String lastName;
private AddressLoc addressLoc = new AddressLoc();
public Person(){};
public Address getAddressLoc() {
return addressLoc ;
}
public void setAddressLoc(Address address) {
this.addressLoc = address;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Child/nested class
public class AddressLoc {
private String cityName;
private String street;
public AddressLoc(){};
public String getCityName() {
return cityName;
}
public void setCityName(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
Resource Class
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.nabisoft.tutorials.jerseymoxy.model.Person;
@Path("/person")
public class PersonResource {
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.TEXT_PLAIN})
@Path("/post")
public String postPerson(Person pers) throws Exception{
System.out.println("First Name = "+pers.getFirstName());
System.out.println("Last Name = "+pers.getLastName());
System.out.println("Last Name = "+pers.getAddress().getCity());
System.out.println("Last Name = "+pers.getAddress().getStreet());
return "ok";
}
}
Client code
<script src="<%=request.getContextPath() %>/js/jquery-1.11.2.min.js"></script>
<script>
var ctxPath = "<%=request.getContextPath() %>";
$(function(){
$("#postPerson, #postMessage").on("click", function(){
$.ajax({
url: $(this).attr("id") === "postMessage" ? ctxPath+"/service/message/post" : ctxPath+"/service/personwa/post",
type: "POST",
data: '{"first_name":"Michael", "last_name":"Jordan", "address_loc":{"city_name":"Mumbai", "street":"galino1"}}',
contentType: "application/json",
cache: false,
dataType: "json"
});
});
});
</script>
Apart from above listed codebase I also have "ApplicationConfig.java" and "JsonMoxyConfigurationContextResolver.java" without any fancy implementaion in it. Thanks.
You want to add javax.xml.bind.annotation.XmlElement annotation and provide another (snake case) name.
@XmlRootElement
public class Person {
@XmlElement(name="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.