[英]Jersey Moxy based rest service not consuming nested json post request payload
我有json數據'{“ first_name”:“ Michael”,“ last_name”:“ Jordan”,“ address_loc”:{“ city_name”:“孟買”,“ street”:“ galino1”}}'的蛇形表示形式 。 我想通過基於pojo表示形式的基於jersey + moxy的post方法資源來消耗此數據。 請建議。還請注意,使用給定的以下代碼片段可以正常使用json的駝峰式案例表示形式,但我有使用蛇形案例的要求。
@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;
}
}
兒童/嵌套班
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;
}
}
資源類別
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";
}
}
客戶代碼
<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>
除了上面列出的代碼庫,我還具有“ ApplicationConfig.java”和“ JsonMoxyConfigurationContextResolver.java”,其中沒有任何奇特的實現。 謝謝。
您要添加javax.xml.bind.annotation.XmlElement批注並提供另一個(蛇形)名稱。
@XmlRootElement
public class Person {
@XmlElement(name="first_name")
private String firstName;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.