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
How do I obtain the Hymanson ObjectMapper in use by Spring 4.1?
提问by Adam Maass
Spring 4.1 instantiates a Hymanson 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 Hymanson, 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 实例化了一个 HymansonObjectMapper
实例。我有理由希望@Autowire
将该实例放入我的一个控制器中:控制器使用 Hymanson 对它自己进行一些次要的 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.
请注意,我不是在问如何自定义配置ObjectMapper
Spring 使用的;我对默认设置很满意。我只是想把 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 MappingHymanson2HttpMessageConverter
myself, 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 ObjectMapper
If you create a default Hymanson2ObjectMapperBuilder
@Bean
, you should be able to autowire that same ObjectMapper
instance 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 @Autowired
it 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 MappingHymanson2HttpMessageConverter
which 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;
如果您愿意,可以使用两步器;
At your
@SpringBootApplication
class, add:@Bean public ObjectMapper mapper() { return new ObjectMapper(); }
Anywhere you wish to use
ObjectMapper
:@Autowired ObjectMapper mapper;
在您的
@SpringBootApplication
课堂上,添加:@Bean public ObjectMapper mapper() { return new ObjectMapper(); }
您想使用的任何地方
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 ObjectMapper
is created by Hymanson2ObjectMapperBuilder
, and you can inject the builder using:
由ObjectMapper
创建Hymanson2ObjectMapperBuilder
,您可以使用以下方法注入构建器:
@Autowired
private Hymanson2ObjectMapperBuilder mapperBuilder;
Then use mapperBuilder.build()
to build an ObjectMapper
instance, 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 Hymanson2ObjectMapperBuilder
will 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 bootRun
we don't have this problem.
当我们bootRun
没有这个问题时。
The configuration is set in Hymanson2ObjectMapperBuilderCustomizerConfiguration#customize(Hymanson2ObjectMapperBuilder builder)
method.
配置在Hymanson2ObjectMapperBuilderCustomizerConfiguration#customize(Hymanson2ObjectMapperBuilder builder)
方法中设置。