Spring Boot:请求和响应的不同ObjectMapper实例 [英] Spring Boot : Different ObjectMapper instances for Request and Response

查看:804
本文介绍了Spring Boot:请求和响应的不同ObjectMapper实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的春季启动应用程序中有以下控制器:

I have following controller in my spring boot application:

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<ResponseDto<MyClass> process(@RequestBody RequestDto<MyClass> request){
    return null;
}

MyClass 有一个字段,让我们说'myField',我想要不同的 NamingStrategy 配置这个字段的请求和响应(这是因为我不想只为一个创建一个新类领域)。我已经配置了 ObjectMapper 实例,如下所示:

MyClass has a field, let's say 'myField' and I want different NamingStrategyconfiguration for request and response for this field (this is because I don't want to create a new class just for one field). I have configured ObjectMapper instance as below:

@Bean
public ObjectMapper objectMapper(){
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setPropertyNamingStrategy(namingStrategy);
    return objectMapper;
}

这将用于请求和响应(即反序列化和序列化),在spring boot中有什么方法可以指示控制器使用不同的 ObjectMapper 实例?

This will be used both for Request and Response (i.e. deserialization and serialization), is there any way in spring boot by which I can instruct the controller to use different ObjectMapper instances?

推荐答案

您可以使用 内容协商解决此问题。首先,定义您的自定义 HttpMessageConverter 。在下面的示例中,我定义了一个自定义转换器,当请求 Content-Type 标头设置为 application / test + json

You can solve it with content negotiation. Firstly, define your custom HttpMessageConverter. In following example I have defined a custom converter that is applied when the request Content-Type header is set to application/test+json:

@Bean
public HttpMessageConverters customConverters() {
    final AbstractJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(new ObjectMapper());
    converter.setSupportedMediaTypes(Collections.singletonList(MediaType.valueOf("application/test+json")));

    return new HttpMessageConverters(true, Collections.singletonList(converter));
}

为了简化这个例子,我使用了新创建的 ObjectMapper - 在你的情况下,你必须在这里传递先前配置的对象。

For simplicity of this example I've used newly created ObjectMapper - in your case you will have to pass here previously configured object.

接下来就是告诉你的行动只接受 appliction / test + json 请求(请记住,从现在起它需要 Content-Type:application / test + json 在此端点的每个请求中显示的标头):

Next thing is to tell your action to accept only appliction/test+json requests (keep in mind, that from now on it requires to Content-Type:application/test+json header to present in every request to this endpoint):

@RequestMapping(method = RequestMethod.POST, consumes = "application/test+json")
public ResponseEntity<ResponseDto<MyClass> process(@RequestBody RequestDto<MyClass> request){
    return null;
}

最后一件事是确保在调用此端点时 Content-Type:application / test + json 标头已设置。当然,您可以使用任何其他名称来获得所需的内容类型,显示的名称只是一个示例。

Last thing is to make sure that when you call this endpoint, Content-Type:application/test+json header is set. Of course you can use any other name for desired content type, presented name is just an example.

这篇关于Spring Boot:请求和响应的不同ObjectMapper实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆