如何在使用JSON和Java时映射嵌套对象和表单字段

时间:2013-02-16 21:55:40

标签: java javascript json parsing nested-attributes

错误消息

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "address" (Class entity.User), not marked as ignorable
 at [Source: org.apache.catalina.connector.CoyoteInputStream@1c6a59e0; line: 1, column: 67] (through reference chain: entity.User["address"])

HTML

<input type="text" class="" id="address" name="address" placeholder="Street"/>
<input type="text" id="postalCode" name="postalCode" placeholder="Zipcode"/></br>
<input type="text" id="city" class="" name="city" placeholder="City"/>
<input type="text" id="country" name="country" placeholder="Country"/>

地址对象有一个字段城市和邮政编码

private String address;
private City cityId;
private String postalCode;

城市对象包含国家/地区

private Country countryId;

Java脚本

return JSON.stringify({
      "address": $('#address').val(),
      "postalCode": $('#postalCode').val(),
      "city": $('#city').val()
   });
$.ajax({
   type: "POST",
   contentType: "application/json",
   url: "http://localhost:8080/testSoft/webresources/entity.user/",
   dataType: "json",
   data: formToJSON(),
   success: function(response)

用户对象

@Entity

@Table(name = "users")
@XmlRootElement
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "username")
    private String username;
...
    @JoinColumn(name = "address_address_id", referencedColumnName = "address_id")
    @ManyToOne
    private Address addressAddressId;

问题

将json-objects发布到我的Web服务时,如何处理嵌套对象?在我的例子中,Address是User-object中的一个对象,City是一个Object,City-object中有Country-object。

0 个答案:

没有答案