如何在Spring 4.1之前获得Jackson ObjectMapper ?

时间:2022-10-11 18:04:26

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

Spring 4.1实例化一个Jackson ObjectMapper实例。我有理由希望将该实例@Autowire连接到我的一个控制器中:控制器使用Jackson进行一些小型的JSON解析,但它使用的ObjectMapper应该与Spring本身使用的实例相同。我该如何去实现它呢?

Note that I'm not asking how to custom configure the ObjectMapper in 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.

注意,我不是问如何自定义配置在Spring中使用的ObjectMapper;我对默认值很满意。我只是想提取Spring使用的实例,以便在自己的代码中重用现有实例。

5 个解决方案

#1


27  

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

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

@Autowired
private ObjectMapper jacksonObjectMapper;

#2


10  

If you take a look at MappingJackson2HttpMessageConverter, 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 MappingJackson2HttpMessageConverter myself, e.g.

如果查看MappingJackson2HttpMessageConverter,您将看到它创建了一个新的ObjectMapper,但不将其作为bean公开。这里有一个getter,但是我过去获取它的唯一方法是我自己创建MappingJackson2HttpMessageConverter,例如。

public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

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

        MappingJackson2HttpMessageConverter jacksonMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = jacksonMessageConverter.getObjectMapper();

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

        converters.add(jacksonMessageConverter);
    }
}

If you're working with Spring Boot, there's a section in the manual dedicated to working with the ObjectMapper If you create a default Jackson2ObjectMapperBuilder @Bean, you should be able to autowire that same ObjectMapper instance in your controller.

如果使用Spring Boot,手册中有一节专门用于处理ObjectMapper,如果您创建一个默认的Jackson2ObjectMapperBuilder @Bean,您应该能够在控制器中自动连接相同的ObjectMapper实例。

#3


7  

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

正如其他人所说,您不能将它@Autowired直接连接到您的控制器中。

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

@Emerson Farrugia关于创建新实例的建议

Jackson2ObjectMapperBuilder.json().build()

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

我也没有工作,因为获得的实例没有遵循春天。杰克逊。*配置属性,我需要它。


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

我找到的解决方案是从Spring的MappingJackson2HttpMessageConverter获取ObjectMapper,它是可注入的。

So I autowired it:

所以我autowired的:

@Autowired
private MappingJackson2HttpMessageConverter springMvcJacksonConverter;

and then get the ObjectMapper from it like this:

然后像这样获取ObjectMapper:

ObjectMapper objectMapper = springMvcJacksonConverter.getObjectMapper();

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

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

#4


0  

A two-stepper if you will;

  1. At your @SpringBootApplication class, add:

    在您的@SpringBootApplication类中,添加:

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

    任何您希望使用ObjectMapper的地方:

    @Autowired
    ObjectMapper mapper;
    

Peace!

和平!

#5


0  

When you try to @Autowire the MappingJackson2HttpMessageConverter it gives you: No qualifying bean of type 'org.springframework.http.converter.json.MappingJackson2HttpMessageConverter' available: expected single matching bean but found 4: mappingJackson2HttpMessageConverter,jacksonHttpMessageConverter,halJacksonHttpMessageConverter,alpsJsonHttpMessageConverter.

当您尝试@Autowire时,它会给您提供:没有类型“org.springframework.http.converter.json”的限定bean。MappingJackson2HttpMessageConverter'可用:预期的单个匹配bean,但是找到了4:MappingJackson2HttpMessageConverter,jacksonHttpMessageConverter,halJacksonHttpMessageConverter,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 MappingJackson2HttpMessageConverter halJacksonHttpMessageConverter;

这不是什么大问题,您只需将变量名更改为上面的一个,就可以获得该实例:@Autowired private MappingJackson2HttpMessageConverter;

#1


27  

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

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

@Autowired
private ObjectMapper jacksonObjectMapper;

#2


10  

If you take a look at MappingJackson2HttpMessageConverter, 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 MappingJackson2HttpMessageConverter myself, e.g.

如果查看MappingJackson2HttpMessageConverter,您将看到它创建了一个新的ObjectMapper,但不将其作为bean公开。这里有一个getter,但是我过去获取它的唯一方法是我自己创建MappingJackson2HttpMessageConverter,例如。

public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

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

        MappingJackson2HttpMessageConverter jacksonMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = jacksonMessageConverter.getObjectMapper();

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

        converters.add(jacksonMessageConverter);
    }
}

If you're working with Spring Boot, there's a section in the manual dedicated to working with the ObjectMapper If you create a default Jackson2ObjectMapperBuilder @Bean, you should be able to autowire that same ObjectMapper instance in your controller.

如果使用Spring Boot,手册中有一节专门用于处理ObjectMapper,如果您创建一个默认的Jackson2ObjectMapperBuilder @Bean,您应该能够在控制器中自动连接相同的ObjectMapper实例。

#3


7  

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

正如其他人所说,您不能将它@Autowired直接连接到您的控制器中。

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

@Emerson Farrugia关于创建新实例的建议

Jackson2ObjectMapperBuilder.json().build()

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

我也没有工作,因为获得的实例没有遵循春天。杰克逊。*配置属性,我需要它。


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

我找到的解决方案是从Spring的MappingJackson2HttpMessageConverter获取ObjectMapper,它是可注入的。

So I autowired it:

所以我autowired的:

@Autowired
private MappingJackson2HttpMessageConverter springMvcJacksonConverter;

and then get the ObjectMapper from it like this:

然后像这样获取ObjectMapper:

ObjectMapper objectMapper = springMvcJacksonConverter.getObjectMapper();

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

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

#4


0  

A two-stepper if you will;

  1. At your @SpringBootApplication class, add:

    在您的@SpringBootApplication类中,添加:

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

    任何您希望使用ObjectMapper的地方:

    @Autowired
    ObjectMapper mapper;
    

Peace!

和平!

#5


0  

When you try to @Autowire the MappingJackson2HttpMessageConverter it gives you: No qualifying bean of type 'org.springframework.http.converter.json.MappingJackson2HttpMessageConverter' available: expected single matching bean but found 4: mappingJackson2HttpMessageConverter,jacksonHttpMessageConverter,halJacksonHttpMessageConverter,alpsJsonHttpMessageConverter.

当您尝试@Autowire时,它会给您提供:没有类型“org.springframework.http.converter.json”的限定bean。MappingJackson2HttpMessageConverter'可用:预期的单个匹配bean,但是找到了4:MappingJackson2HttpMessageConverter,jacksonHttpMessageConverter,halJacksonHttpMessageConverter,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 MappingJackson2HttpMessageConverter halJacksonHttpMessageConverter;

这不是什么大问题,您只需将变量名更改为上面的一个,就可以获得该实例:@Autowired private MappingJackson2HttpMessageConverter;