Java 如何获取 Spring 4.1 使用的 Jackson ObjectMapper?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30060006/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:03:36  来源:igfitidea点击:

How do I obtain the Hymanson ObjectMapper in use by Spring 4.1?

javajsonspringspring-mvcHymanson

提问by Adam Maass

Spring 4.1 instantiates a Hymanson ObjectMapperinstance. I have reason to want to @Autowirethat instance into one of my controllers: The controller does some minor JSON parsing of its own using Hymanson, but the ObjectMapperit uses should be the one and same instance that Spring itself is using. How do I go about accomplishing that?

Spring 4.1 实例化了一个 HymansonObjectMapper实例。我有理由希望@Autowire将该实例放入我的一个控制器中:控制器使用 Hymanson 对它自己进行一些次要的 JSON 解析,但ObjectMapper它使用的应该是 Spring 本身正在使用的同一个实例。我该怎么做呢?

Note that I'm not asking how to custom configure the ObjectMapperin use by Spring; I'm happy with the defaults. I just want to fish the instance used by Spring out so that I can re-use the existing instance in my own code.

请注意,我不是在问如何自定义配置ObjectMapperSpring 使用的;我对默认设置很满意。我只是想把 Spring 使用的实例捞出来,这样我就可以在我自己的代码中重新使用现有的实例。

回答by Emerson Farrugia

If you take a look at MappingHymanson2HttpMessageConverter, you'll see that it creates a new ObjectMapper, but doesn't expose it as a bean. There's a getter, but the only way I've fished it out in the past is when I created the MappingHymanson2HttpMessageConvertermyself, e.g.

如果您查看MappingHymanson2HttpMessageConverter,您会看到它创建了一个 new ObjectMapper,但没有将其公开为 bean。有一个吸气剂,但我过去唯一能找到它的方法是我MappingHymanson2HttpMessageConverter自己创建的,例如

public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

        MappingHymanson2HttpMessageConverter HymansonMessageConverter = new MappingHymanson2HttpMessageConverter();
        ObjectMapper objectMapper = HymansonMessageConverter.getObjectMapper();

        objectMapper.registerModule(new JodaModule());
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);

        converters.add(HymansonMessageConverter);
    }
}

If you're working with Spring Boot, there's a sectionin the manual dedicated to working with the ObjectMapperIf you create a default Hymanson2ObjectMapperBuilder@Bean, you should be able to autowire that same ObjectMapperinstance in your controller.

如果您正在使用 Spring Boot,手册中有一个部分专门用于使用ObjectMapper如果您创建一个 default Hymanson2ObjectMapperBuilder@Bean,您应该能够ObjectMapper在您的控制器中自动装配同一个实例。

回答by Tuno

If you're using Spring Boot with Hymanson on your classpath and default implementation for JSON parsing in your REST controller, then this should work:

如果您在类路径上使用 Spring Boot 和 Hymanson,并且在 REST 控制器中使用默认的 JSON 解析实现,那么这应该可以工作:

@Autowired
private ObjectMapper HymansonObjectMapper;

回答by netmikey

As was said by others, you can't @Autowiredit in directly into your controller.

正如其他人所说,您不能将@Autowired其直接放入控制器中。

@Emerson Farrugia's suggestion to create a new instance using

@Emerson Farrugia 建议使用创建一个新实例

Hymanson2ObjectMapperBuilder.json().build()

also didn't work for me because the obtained instance was not following the spring.Hymanson.*configuration properties, which I needed it to.

也对我不起作用,因为获得的实例没有遵循spring.Hymanson.*我需要的配置属性。



The solution I found was to obtain the ObjectMapper from Spring's MappingHymanson2HttpMessageConverterwhich is injectable.

我找到的解决方案是从 Spring 中获取MappingHymanson2HttpMessageConverter可注入的 ObjectMapper 。

So I autowired it:

所以我自动装配它:

@Autowired
private MappingHymanson2HttpMessageConverter springMvcHymansonConverter;

and then get the ObjectMapper from it like this:

然后像这样从中获取 ObjectMapper:

ObjectMapper objectMapper = springMvcHymansonConverter.getObjectMapper();

This instance behaves exactly as Spring MVC's own message conversion - it probably isthe same instance anyway.

这个实例的行为与 Spring MVC 自己的消息转换完全一样——无论如何它可能同一个实例。

回答by Sharan Arumugam

A two-stepper if you will;

如果您愿意,可以使用两步器;

  1. At your @SpringBootApplicationclass, add:

    @Bean
    public ObjectMapper mapper() {
      return new ObjectMapper();
    }
    
  2. Anywhere you wish to use ObjectMapper:

    @Autowired
    ObjectMapper mapper;
    
  1. 在您的@SpringBootApplication课堂上,添加:

    @Bean
    public ObjectMapper mapper() {
      return new ObjectMapper();
    }
    
  2. 您想使用的任何地方ObjectMapper

    @Autowired
    ObjectMapper mapper;
    

Peace!

和平!

回答by AngelVel

When you try to @Autowire the MappingHymanson2HttpMessageConverter it gives you: No qualifying bean of type 'org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter' available: expected single matching bean but found 4: mappingHymanson2HttpMessageConverter,HymansonHttpMessageConverter,halHymansonHttpMessageConverter,alpsJsonHttpMessageConverter.

当您尝试@Autowire的MappingHymanson2HttpMessageConverter它给你: No qualifying bean of type 'org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter' available: expected single matching bean but found 4: mappingHymanson2HttpMessageConverter,HymansonHttpMessageConverter,halHymansonHttpMessageConverter,alpsJsonHttpMessageConverter

This is not a big issue, you can just change your variable name to one of the above to get that instance: @Autowired private MappingHymanson2HttpMessageConverter halHymansonHttpMessageConverter;

这不是一个大问题,您只需将变量名称更改为上述名称之一即可获得该实例: @Autowired private MappingHymanson2HttpMessageConverter halHymansonHttpMessageConverter;

回答by Lym Zoy

The ObjectMapperis created by Hymanson2ObjectMapperBuilder, and you can inject the builder using:

ObjectMapper创建Hymanson2ObjectMapperBuilder,您可以使用以下方法注入构建器:

@Autowired
private Hymanson2ObjectMapperBuilder mapperBuilder;

Then use mapperBuilder.build()to build an ObjectMapperinstance, and this instance can use configurations in application.properties. Official doc here.

然后使用mapperBuilder.build()构建一个ObjectMapper实例,这个实例可以使用application.properties. 官方文档在这里。

回答by WesternGun

I have debugged into source code of Spring Boot and found that only when we launch the whole context Hymanson2ObjectMapperBuilderwill contain the config we put in application.yml.

我调试了 Spring Boot 的源代码,发现只有当我们启动整个上下文时,Hymanson2ObjectMapperBuilder才会包含我们放入的配置application.yml

This means, if we want to generate an ObjectMapper in a Spring Boot test, with JUnit 5, we have to:

这意味着,如果我们想在 Spring Boot 测试中生成一个 ObjectMapper,使用 JUnit 5,我们必须:

@ExtendWith(SpringExtension.class)
@SpringBootTest
class SomeTest {
    @Autowired
    private Hymanson2ObjectMapperBuilder builder;
    ...

    @Test
    void testObjectMapper() {
        ObjectMapper mapper = builder.build();


    }

We cannot only make @SpringBootTest(classes = Hymanson2ObjectMapperBuilder.class)to generate this builder.

我们不能只@SpringBootTest(classes = Hymanson2ObjectMapperBuilder.class)生成这个生成器。

When we bootRunwe don't have this problem.

当我们bootRun没有这个问题时。

The configuration is set in Hymanson2ObjectMapperBuilderCustomizerConfiguration#customize(Hymanson2ObjectMapperBuilder builder)method.

配置在Hymanson2ObjectMapperBuilderCustomizerConfiguration#customize(Hymanson2ObjectMapperBuilder builder)方法中设置。

enter image description here

在此处输入图片说明