我有一个简单的Spring Boot / Spring Data Rest测试应用程序,它有一个实体“Contact”和一个CRUDRepository。没有其他代码。
当数据库为空时,请求/ contact上的api + json元数据正常工作:
$ curl -s 'http://localhost:8080/contacts' \
-H 'Accept: application/vnd.api+json'\
-H 'Content-Type: application/hal+json'
...生产:
{
"links" : [ {
"rel" : "self",
"href" : "http://localhost:8080/contacts"
}, {
"rel" : "profile",
"href" : "http://localhost:8080/profile/contacts"
} ],
"content" : [ {
"relTargetType" : "sdr.Contact",
"collectionValue" : true,
"rel" : null,
"value" : [ ]
} ]
}
但是,当我在数据库中放置一个联系人时,同一个请求会产生大量的递归信息转储:
{
"links" : [ {
"rel" : "self",
"href" : "http://localhost:8080/contacts"
}, {
"rel" : "profile",
"href" : "http://localhost:8080/profile/contacts"
} ],
"content" : [ {
"id" : 2,
"firstName" : "Test",
"lastName" : "Contact",
"embeddeds" : {
"content" : [ ]
},
"propertyAccessor" : {
"bean" : {
"id" : 2,
"firstName" : "Test",
"lastName" : "Contact"
}
},
"persistentEntity" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
"type" : "java.lang.Long",
"modifiers" : 2,
"annotations" : [ ],
"declaredAnnotations" : [ ],
"synthetic" : false,
"declaringClass" : "sdr.Contact",
"enumConstant" : false,
"genericType" : "java.lang.Long",
"annotatedType" : {
"type" : "java.lang.Long",
"annotations" : [ ],
"declaredAnnotations" : [ ]
},
"accessible" : true
},
"association" : false,
"owner" : {
"idProperty" : {
"name" : "id",
"rawType" : "java.lang.Long",
"field" : {
"name" : "id",
***SNIP, THIS GOES ON FOREVER ***
为什么会返回此递归结果?这是预期的行为吗?
其他一切正常。测试应用程序中没有JPA关系或任何其他复杂因素。我可以使用HAL浏览器添加和删除记录等。
我正在使用Spring Boot 1.3.2和Spring Data Rest 2.4.2。
这是应用程序中唯一的实体:
@Entity
public class Contact {
private Long id;
private String firstName;
private String lastName;
public Contact() {
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Basic
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Basic
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
存储库:
@Repository
@RepositoryRestResource
public interface ContactRepository extends CrudRepository<Contact, Long> {
}
这是应用程序中的全部代码。
更新:这似乎是Spring Data REST中的一个错误。有关测试应用程序,请参阅DATAREST-767。也可能与DATAREST-733和DATAREST-734
相关答案 0 :(得分:0)
Spring Data REST for JSON API不支持,你得到的是为+json
呈现的默认表示。