【发布时间】:2017-05-10 22:56:05
【问题描述】:
我正在使用 spring-boot-starter-parent 1.3.3 和 jackson-core-asl:jar:1.9.2。我无法创建与其中关联的人的对象(组),因为该组是使用人名创建的。响应喜欢以下内容..
例如 请求:
{
"name": "Students"
"person": {"id": 1, "name: "John"}
}
回应:
{
"id" : 1,
"name" : "John",
"content" : [ ],
"links" : [ {
"rel" : "self",
"href" : "http://localhost/Group/1"
}, {
"rel" : "person",
"href" : "http://localhost/Group/1/person"
} ]
}
群组(名称:“Students”)是在上述回复中使用人名“John”创建的。
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
如果我将@JsonProperty() 放在 Group.java 中,一切正常。
例如
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 创建。
得到以下错误..
{ “原因”:{ “原因”:空, "message": "无法从 String 值 ('http://localhost:8080/persons/1') 实例化类型 [简单类型,类 xx.xxx.Person] 的值; [来源:没有单字符串构造函数/工厂方法\n org.apache.catalina.connector.CoyoteInputStream@64bf4540;线:18, column: 19] (通过引用链: xx.xxx.Group[\"person\"])" }, “消息”:“无法读取文档:无法实例化类型的值 [简单类型,类 xx.xxx.Person] 来自 String 值 ('http://localhost:8080/persons/1');没有单串 构造函数/工厂方法\n 在 [Source: org.apache.catalina.connector.CoyoteInputStream@64bf4540;线:18, 栏:19](通过参考链: xx.xxx.[\"人\"]);嵌套的 例外是 com.fasterxml.jackson.databind.JsonMappingException: Can 不从 [simple type, class xx.xx.Person] 实例化值 字符串值('http://localhost:8080/persons/1');没有单串 构造函数/工厂方法\n 在 [Source: org.apache.catalina.connector.CoyoteInputStream@64bf4540;线:18, 列:19](通过引用链:xx.xxx.Group[\"person\"])" }
请提供您的想法。
【问题讨论】:
标签: java spring-boot jackson spring-data-rest mockmvc