Domain Object Representations (Object Mapping)
Spring Data REST 返回在 HTTP 请求中指定的 “Accept” 类型对应的域对象的表述。
Spring Data REST returns a representation of a domain object that corresponds to the Accept
type specified in the HTTP request.
当前,仅支持 JSON 表述。其他表述类型可以通过添加适当的转换器并将控制器方法更新为适当的内容类型在将来得到支持。
Currently, only JSON representations are supported. Other representation types can be supported in the future by adding an appropriate converter and updating the controller methods with the appropriate content-type.
有时,Spring Data REST “ObjectMapper” 的行为(已专门配置为使用可以将域对象变为链接再变回来的智能序列化器)可能无法正确处理你的域模型。你可以组织数据的方式有很多种,你可能会发现自己的域模型无法正确翻译为 JSON。有时候在这些情况下尝试用一般的方式支持一个复杂的域模型也不实际。有时,根据复杂度,甚至无法提供一般解决方案。
Sometimes, the behavior of the Spring Data REST ObjectMapper
(which has been specially configured to use intelligent serializers that can turn domain objects into links and back again) may not handle your domain model correctly. There are so many ways you can structure your data that you may find your own domain model is not translated to JSON correctly. It is also sometimes not practical in these cases to try and support a complex domain model in a generic way. Sometimes, depending on the complexity, it is not even possible to offer a generic solution.
Adding Custom Serializers and Deserializers to Jackson’s ObjectMapper
为了适应大多数用例,Spring Data REST 非常努力地正确呈现你的对象图。它尝试将非托管 Bean 序列化为正常的 POJO,并且在需要时尝试创建到托管 Bean 的链接。然而,如果你的域模型本身不适合于读取或写入纯 JSON,你可以使用自己的自定义映射、序列化器和反序列化器对 Jackson 的 ObjectMapper 进行配置。
To accommodate the largest percentage of use cases, Spring Data REST tries very hard to render your object graph correctly. It tries to serialize unmanaged beans as normal POJOs, and it tries to create links to managed beans where necessary. However, if your domain model does not easily lend itself to reading or writing plain JSON, you may want to configure Jackson’s ObjectMapper with your own custom mappings, serializers, and deserializers.
Abstract Class Registration
你可能需要与之连接的一个关键配置点是在域模型中使用抽象类(或接口)时。默认情况下,Jackson 不知道为一个接口创建什么实现。考虑以下示例:
One key configuration point you might need to hook into is when you use an abstract class (or an interface) in your domain model. By default, Jackson does not know what implementation to create for an interface. Consider the following example:
@Entity
public class MyEntity {
@OneToMany
private List<MyInterface> interfaces;
}
在默认配置中,当向导出器发布新数据时,Jackson 不知道实例化哪个类。这是你需要通过注释或(更干净地)使用 “Module” 注册类型映射来告诉 Jackson 的。
In a default configuration, Jackson has no idea what class to instantiate when POSTing new data to the exporter. This is something you need to tell Jackson either through an annotation, or (more cleanly) by registering a type mapping by using a Module
.
要将你自己的 Jackson 配置添加到 Spring Data REST 使用的 “ObjectMapper”,请覆盖 “configureJacksonObjectMapper” 方法。该方法会传递一个 “ObjectMapper” 实例,该实例有一个特殊模块来处理序列化和反序列化 “PersistentEntity” 对象。你也可以注册你自己的模块,如下例所示:
To add your own Jackson configuration to the ObjectMapper
used by Spring Data REST, override the configureJacksonObjectMapper
method. That method is passed an ObjectMapper
instance that has a special module to handle serializing and deserializing PersistentEntity
objects. You can register your own modules as well, as the following example shows:
@Override
protected void configureJacksonObjectMapper(ObjectMapper objectMapper) {
objectMapper.registerModule(new SimpleModule("MyCustomModule") {
@Override
public void setupModule(SetupContext context) {
context.addAbstractTypeResolver(
new SimpleAbstractTypeResolver()
.addMapping(MyInterface.class, MyInterfaceImpl.class));
}
});
}
一旦您在 Module
中访问了 SetupContext
对象,您就可以执行各种很酷的操作来配置 Jackson 的 JSON 映射。您可以在 Jackson’s wiki 上阅读有关 Module
实例如何工作的更多信息。
Once you have access to the SetupContext
object in your Module
, you can do all sorts of cool things to configure Jackson’s JSON mapping. You can read more about how Module
instances work on Jackson’s wiki.
Adding Custom Serializers for Domain Types
如果你想以一种特殊的方式序列化或反序列化域类型,你可以向 Jackson 的 “ObjectMapper” 注册你自己的实现,Spring Data REST 导出器会透明地正确处理那些域对象。要从你的 “setupModule” 方法实现中添加序列化器,你可以执行类似以下操作:
If you want to serialize or deserialize a domain type in a special way, you can register your own implementations with Jackson’s ObjectMapper
, and the Spring Data REST exporter transparently handles those domain objects correctly. To add serializers from your setupModule
method implementation, you can do something like the following:
@Override
public void setupModule(SetupContext context) {
SimpleSerializers serializers = new SimpleSerializers();
SimpleDeserializers deserializers = new SimpleDeserializers();
serializers.addSerializer(MyEntity.class, new MyEntitySerializer());
deserializers.addDeserializer(MyEntity.class, new MyEntityDeserializer());
context.addSerializers(serializers);
context.addDeserializers(deserializers);
}