[英]RepositoryRestMvcConfiguration's ObjectMapper vs. Spring Boot default ObjectMapper?
I'm using RepositoryRestMvcConfiguration
for fine-tuning of the rest repository behaviour: 我正在使用
RepositoryRestMvcConfiguration
来微调其余存储库的行为:
@Configuration
public class WebConfig extends RepositoryRestMvcConfiguration {
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setReturnBodyOnCreate(true);
}
The downside is the extended class brings in its own ObjectMapper bean, causing conficts described here . 缺点是扩展的类带来了自己的ObjectMapper豆,造成描述conficts 这里 。 The recommended workaround is to use the extending class to mark the ObjectMapper bean as
@Primary
, however the bean from RepositoryRestMvcConfiguration
has different behaviour while serializing nested entities. 推荐的解决方法是使用扩展类将ObjectMapper Bean标记为
@Primary
,但是序列化嵌套实体时,来自RepositoryRestMvcConfiguration
的Bean具有不同的行为。
Let's assume the following enitites: 让我们假设以下实体:
@Entity class Parent {
@Id Long id;
@OneToMany @JsonManagedReference List<Child> children;
// usual getters and setters for fields...
}
@Entity class Child {
@Id Long id;
@ManyToOne @JsonBackReference Parent parent;
@ManyToOne @JsonBackReference School school;
public getSchooldId() { return school.getId(); }
// usual getters and setters for fields...
}
@Entity class School {
@Id Long id;
@OneToMany @JsonManagedReference List<Child> children;
// usual getters and setters for fields...
}
Using the default Spring Boot ObjectMapper gives the expected result (nested entities are rendered): 使用默认的Spring Boot ObjectMapper可以得到预期的结果(呈现嵌套实体):
{"id": 1, "children":[{"id":2, "schoolId":7},{"id":3, "schooldId":8}]}
However the ObjectMapper from RepositoryRestMvcConfiguration
ignores the child entities: 但是,来自
RepositoryRestMvcConfiguration
的ObjectMapper会忽略子实体:
{"id": 1}
What is the correct way of configuring the RepositoryRestMvcConfiguration
ObjectMapper to achieve the same behaviour as the Spring Boot default? 配置
RepositoryRestMvcConfiguration
ObjectMapper以实现与Spring Boot默认设置相同的行为的正确方法是什么?
RepositoryRestMvcConfiguration
creates two objectMapper objects. RepositoryRestMvcConfiguration
创建两个objectMapper对象。
Resources
and Links. Resources
和链接。 You could try to achieve the desired result by autowiring the objectMapper using a qualifier : 您可以尝试通过使用限定符自动装配objectMapper来获得所需的结果:
@Qualifier('_halObjectMapper')
EDIT: For rendering associations/nested properties 编辑:用于渲染关联/嵌套的属性
Spring data-rest does not render by default associations( reference ) because they are available under the HATEOAS Specification(the _link
part of your json). Spring data-rest默认情况下不呈现关联( reference ),因为它们在HATEOAS规范(json的
_link
部分)下可用。 If you want to render associations you just need to use Projections . 如果要渲染关联,则只需要使用Projections即可 。
This Person has several attributes:id is the primary key, firstName and lastName are data attributes,address is a link to another domain object
此Person具有几个属性:id是主键,firstName和lastName是数据属性,address是到另一个域对象的链接
@Entity
public class Person {
@Id @GeneratedValue
private Long id;
private String firstName, lastName;
@OneToOne
private Address address;
…
}
Will be rendered: 将呈现:
{
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/persons/1"
},
"address" : {
"href" : "http://localhost:8080/persons/1/address"
}
}
}
By default, Spring Data REST will export this domain object including all of its attributes.
默认情况下,Spring Data REST将导出此域对象,包括其所有属性。 firstName and lastName will be exported as the plain data objects that they are.
firstName和lastName将作为原始数据对象导出。 There are two options regarding the address attribute.
关于地址属性有两个选项。 One option is to also define a repository for Address.
一种选择是还定义地址的存储库。
There is another route.
还有另一条路线。 If the Address domain object does not have it's own repository definition, Spring Data REST will inline the data fields right inside the Person resource.
如果Address域对象没有其自己的存储库定义,则Spring Data REST将在Person资源内部内联数据字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.