如何使注入的ObjectMapper输出特定于区域设置的内容

huangapple go评论100阅读模式
英文:

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()));
     ...
 }

huangapple
  • 本文由 发表于 2020年4月9日 14:43:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61115370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定