java 如何在 Spring Boot 1.4 中自定义 Jackson
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39263553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to customise Hymanson in Spring Boot 1.4
提问by Reece
I've been unable to find examples of how to use Hymanson2ObjectMapperBuilderCustomizer.javain spring boot 1.4 to customise the features of Hymanson.
我一直无法找到有关如何在 spring boot 1.4 中使用Hymanson2ObjectMapperBuilderCustomizer.java来自定义 Hymanson 功能的示例。
The doco for customising Hymanson in boot 1.4 - http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-customize-the-Hymanson-
在 boot 1.4 中自定义 Hymanson 的 doco - http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-customize-the-Hymanson-
My configuration works, although I am unsure if this is the correct way to customise the object mapper using Hymanson2ObjectMapperBuilderCustomizer.java
我的配置有效,但我不确定这是否是使用Hymanson2ObjectMapperBuilderCustomizer.java自定义对象映射器的正确方法
@Configuration
public class HymansonAutoConfiguration {
@Autowired
private Environment env;
@Bean
public Hymanson2ObjectMapperBuilder HymansonObjectMapperBuilder(
List<Hymanson2ObjectMapperBuilderCustomizer> customizers) {
Hymanson2ObjectMapperBuilder builder = configureObjectMapper();
customize(builder, customizers);
return builder;
}
private void customize(Hymanson2ObjectMapperBuilder builder,
List<Hymanson2ObjectMapperBuilderCustomizer> customizers) {
for (Hymanson2ObjectMapperBuilderCustomizer customizer : customizers) {
customizer.customize(builder);
}
}
private Hymanson2ObjectMapperBuilder configureObjectMapper() {
Hymanson2ObjectMapperBuilder builder = new Hymanson2ObjectMapperBuilder();
List<String> activeProfiles = asList(env.getActiveProfiles());
if (activeProfiles.contains(SPRING_PROFILE_DEVELOPMENT)) {
builder.featuresToEnable(SerializationFeature.INDENT_OUTPUT);
}
return builder;
}
}
To provide some context, this class sits in my own spring starter project for REST services that just auto configures a number of things, like ControllerAdvice and some trivial features like the above.
为了提供一些上下文,这个类位于我自己的 REST 服务的 spring 启动项目中,它只是自动配置一些东西,比如 ControllerAdvice 和一些像上面这样的琐碎功能。
So my goal is to extend the Hymanson configuration rather than to override any configuration provided by boot or other packages.
所以我的目标是扩展 Hymanson 配置,而不是覆盖 boot 或其他包提供的任何配置。
回答by Mark
To customize the Hymanson ObjectMapper
that's already pre-configured by Spring Boot, I was able to do this (the example here is to add a custom deserializer).
为了自定义ObjectMapper
已经由 Spring Boot 预先配置的 Hymanson ,我能够做到这一点(这里的示例是添加自定义反序列化器)。
Configuration class:
配置类:
@SpringBootConfiguration
public class Application {
@Autowired
private BigDecimalDeserializer bigDecimalDeserializer;
...
@Bean
public Hymanson2ObjectMapperBuilderCustomizer addCustomBigDecimalDeserialization() {
return new Hymanson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(Hymanson2ObjectMapperBuilder HymansonObjectMapperBuilder) {
HymansonObjectMapperBuilder.deserializerByType(BigDecimal.class, bigDecimalDeserializer);
}
};
}
...
}
And my custom deserializer, to show how it's picked up by Spring:
还有我的自定义解串器,以展示它是如何被 Spring 接收的:
@Component
public class BigDecimalDeserializer extends StdDeserializer<BigDecimal> {
public BigDecimalDeserializer() {
super(BigDecimal.class);
}
@Override
public BigDecimal deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
...
}
...
}
回答by Andy Wilkinson
It depends on what you're trying to do.
这取决于你想要做什么。
If you want to make some customisations in addition to those that are performed by default then you should create your own Hymanson2ObjectMapperBuilderCustomizer
implementation and expose it as a bean. What you currently have is a more complex version of this. Rather than having the customisers injected and then calling them yourself, you can just create your own customiser bean and Boot will call it for you.
如果您想在默认情况下执行的自定义之外进行一些自定义,那么您应该创建自己的Hymanson2ObjectMapperBuilderCustomizer
实现并将其公开为 bean。您目前拥有的是一个更复杂的版本。无需注入定制器然后自己调用它们,您只需创建自己的定制器 bean,Boot 就会为您调用它。
If you want to take complete control and switch off all of Boot's customisations then create a Hymanson2ObjectMapperBuilder
or ObjectMapper
bean and configure it as required. The builder approach is preferred as this builder is then also used to configure ObjectMappers created by other components such as Spring Data REST.
如果您想完全控制并关闭所有 Boot 的自定义,则创建一个Hymanson2ObjectMapperBuilder
或ObjectMapper
bean 并根据需要对其进行配置。首选构建器方法,因为此构建器还用于配置由其他组件(如 Spring Data REST)创建的 ObjectMappers。
Looking at your code and taking a step back, you could configure things far more simply by using a profile-specific configuration file (something like application-dev.properties
) to enable indenting of Hymanson's output. You can read more about that here.
查看您的代码并退后一步,您可以通过使用特定于配置文件的配置文件(类似于application-dev.properties
)来启用 Hymanson 输出的缩进,从而更简单地进行配置。您可以在此处阅读更多相关信息。
回答by Ulises
just create an ObjectMapper
bean:
只需创建一个ObjectMapper
bean:
@Bean
ObjectMapper objectMapper() {
return Hymanson2ObjectMapperBuilder
.json()
.featuresToEnable(MapperFeature.DEFAULT_VIEW_INCLUSION)
.build();
}