首页 文章

Spring-data-rest POST使用Jackson Deserialization在嵌套对象中具有相同的属性名称

提问于
浏览
0

我使用的是spring-boot-starter-parent 1.3.3和jackson-core-asl:jar:1.9.2 . 我无法创建与其关联的人的对象(组),因为该组是使用人名创建的 . 响应喜欢以下..

例如 Request:

{
"name": "Students"
"person": {"id": 1, "name: "John"}
}

Response:

{
  "id" : 1,
  "name" : "John",
  "content" : [ ],
  "links" : [ {
    "rel" : "self",
    "href" : "http://localhost/Group/1"
  }, {
    "rel" : "person",
    "href" : "http://localhost/Group/1/person"
  } ]
}

在上述回复中,使用人名“John”创建了组(名称:“Students”) .

Person.java

@Table(name = "person")
public class Person implements Serializable {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private int id;
    private String name;


    //getter & setter

Group.java

@Table(name = "group")
public class Group implements Serializable {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private int id;
    private String name;
    @ManyToOne
    @JoinColumn(name = "person_id")
    private Person person;


    //getter & setter

如果我在Group.java中放置@JsonProperty(),一切正常 .

例如

Group.java

@Table(name = "group")
public class Group implements Serializable {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private int id;
    private String name;
    @ManyToOne
    @JoinColumn(name = "person_id")
    @JsonProperty("person") // why this is needed??
    private Person person;

默认的json属性与字段名称相同,那么为什么需要这个@JsonProperty注释呢?

对象映射器中是否存在任何配置来解决此问题?

而且如果我把@RestResource(exported = false)而不是“@JsonProperty”,它在测试用例中工作正常,但无法通过swaggerUI创建 .

得到以下错误..

{“cause”:{“cause”:null,“message”:“无法从字符串值('http:// localhost:8080 / persons /)实例化类型[simple type,class xx.xxx.Person]的值1');没有单字符串构造函数/工厂方法\ n在[来源:org.apache.catalina.connector.CoyoteInputStream@64bf4540;第18行,第19列](通过引用链:xx.xxx.Group [\ “person \”])“},”message“:”无法读取文档:无法从字符串值('http:// localhost:8080 / persons)实例化类型[simple type,class xx.xxx.Person]的值/ 1');没有单字符串构造函数/工厂方法\ n在[来源:org.apache.catalina.connector.CoyoteInputStream@64bf4540;第18行,第19列](通过引用链:xx.xxx . [\ “person \”]);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无法从String值('http:// localhost:8080 /)实例化类型[simple type,class xx.xx.Person]的值persons / 1');没有单字符串构造函数/工厂方法\ n在[来源:org.apache.catalina.connector.CoyoteInp utStream @ 64bf4540; line:18,column:19](通过引用链:xx.xxx.Group [\“person \”])“}

请提供您的想法 .

1 回答

  • 0

    检查Person.java中的类名,类名是Group而不是Person

    public class Group实现Serializable {

相关问题