Jackson

Spring 为 Jackson JSON 库提供支持。

Spring offers support for the Jackson JSON library.

Jackson-based JSON MVC Views

MappingJackson2JsonView 使用 Jackson 库的 ObjectMapper 将响应内容作为 JSON 呈现。默认情况下,模型映射的全部内容(框架特定类除外)均以 JSON 编码。对于需要对映射内容进行筛选的情况,您可以使用 modelKeys 属性指定要编码的特定模型属性集。还可以使用 extractValueFromSingleKeyModel 属性,将单键模型中的值提取出来直接序列化,而不是作为模型属性的映射序列化。

The MappingJackson2JsonView uses the Jackson library’s ObjectMapper to render the response content as JSON. By default, the entire contents of the model map (with the exception of framework-specific classes) are encoded as JSON. For cases where the contents of the map need to be filtered, you can specify a specific set of model attributes to encode by using the modelKeys property. You can also use the extractValueFromSingleKeyModel property to have the value in single-key models extracted and serialized directly rather than as a map of model attributes.

您可以按照需要,使用 Jackson 提供的注释对 JSON 映射进行定制。在需要进一步控制时,您可以通过 ObjectMapper 属性注入自定义 ObjectMapper,以针对特定类型提供自定义 JSON 序列化器和反序列化器。

You can customize JSON mapping as needed by using Jackson’s provided annotations. When you need further control, you can inject a custom ObjectMapper through the ObjectMapper property, for cases where you need to provide custom JSON serializers and deserializers for specific types.

Jackson-based XML Views

`MappingJackson2XmlView`使用 Jackson XML 扩展的 `XmlMapper`将响应内容渲染为 XML。如果模型包含多个条目,则应该显式使用 `modelKey`Bean 属性设置需要序列化的对象。如果模型包含单个条目,将自动执行序列化。

MappingJackson2XmlView uses the Jackson XML extension’s XmlMapper to render the response content as XML. If the model contains multiple entries, you should explicitly set the object to be serialized by using the modelKey bean property. If the model contains a single entry, it is serialized automatically.

您可以按照需要,使用 JAXB 或 Jackson 提供的注释对 XML 映射进行定制。在需要进一步控制时,您可以通过 ObjectMapper 属性注入自定义 XmlMapper,以针对特定类型提供自定义 JSON 序列化器和反序列化器。

You can customize XML mapping as needed by using JAXB or Jackson’s provided annotations. When you need further control, you can inject a custom XmlMapper through the ObjectMapper property, for cases where custom XML you need to provide serializers and deserializers for specific types.