英文:
How to make injected objectmapper output locale-specific
问题
我有一个带有方法的Spring Boot REST控制器
@GetMapping(value = "/validate", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
@ResponseBody
public Result validate(
@ApiParam(value = "http://example.org/test", required = true) @RequestParam String iri,
@Context HttpServletRequest request
) {
return service.validate(iri);
}
...
并且配置了一个自定义对象映射器(`Result`类是第三方库的一部分)
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
SimpleModule module = new SimpleModule();
module.addSerializer(Result.class, new ResultSerializer());
mapper.registerModule(module);
return mapper;
}
在`ResultSerializer`(我的自定义实现)内,我需要基于`Accept-language`头(例如来自绑定的HttpServletRequest)来自定义序列化输出。我目前只能通过不使用Spring注入的方式来解决`ObjectMapper`的问题,在我的控制器中显式创建一个ObjectMapper实例,执行它并返回输出。所以:
**在作为Spring bean注入的ObjectMapper实例中是否可以获取`Accept-language`头?**
英文:
I have a Spring Boot REST controller with the method
@GetMapping(value = "/validate", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
@ResponseBody
public Result validate(
@ApiParam(value = "http://example.org/test", required = true) @RequestParam String iri,
@Context HttpServletRequest request
) {
return service.validate(iri);
}
...
And a custom object mapper configured as (Result
class is part of a third-party library)
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
SimpleModule module = new SimpleModule();
module.addSerializer(Result.class, new ResultSerializer());
mapper.registerModule(module);
return mapper;
}
Inside ResultSerializer
(my custom implementation) I need to customize the serialization output based on the Accept-language
header (e.g. from the bounding HttpServletRequest). I am currently only able to solve by not using Spring injection for the ObjectMapper
, explicitly creating an ObjectMapper instance in my controller, executing it and returning the output. So:
Can an Accept-language
header be obtained in the ObjectMapper
instance injected as a Spring bean?
答案1
得分: 0
一个可能性(不确切地知道您想要自定义什么):您可以尝试将您的ResultSerializer设置为request-scoped
。
@Bean
@RequestScope
public ResultSerializer resultSerializer() {
return new ResultSerializer();
}
然后您可以简单地将HttpServletRequest注入到ResultSerializer中。
public class ResultSerializer {
@Autowired
private HttpServletRequest request;
}
英文:
A possibility (not knowing exactly what you are trying to customize): You could try making your ResultSerializer request-scoped
.
@Bean
@RequestScope
public ResultSerializer resultSerializer() {
return new ResultSerializer();
}
Then you can simply inject your HttpServletRequest into the ResultSerializer.
public class ResultSerializer {
@Autowired
private HttpServletRequest request;
}
答案2
得分: 0
由于Marco的帮助,我成功地对他的解决方案进行了轻微调整,以便在使用以下代码时使其工作:
@Bean
@RequestScope
public ObjectMapper objectMapper(@Autowired HttpServletRequest request) {
...
module.addSerializer(Result.class, new ResultSerializer(request.getLocale().toLanguageTag()));
...
}
英文:
Thanks to Marco, I managed to adjust slightly his solution to get it working using:
@Bean
@RequestScope
public ObjectMapper objectMapper(@Autowired HttpServletRequest request) {
...
module.addSerializer(Result.class, new ResultSerializer(request.getLocale().toLanguageTag()));
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论