使用不同的 jackson ObjectMapper 进行单独的 RequestMapping
Using different jackson ObjectMappers for seperate RequestMapping
我有一个使用自定义 MIME 类型的 @RequestMapping
。该请求使用 @Configuration
中定义的 ObjectMapper
bean 来启用 JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER
.
此功能允许使用通常无效的 json(将反斜杠视为非特殊字符),这是此特定 @RequestMapping
允许 google 编码折线的要求直接解析。然而,这意味着这个 ObjectMapper 现在正用于我的 @RequestMapping
的 ALL,而实际上它只是一个要求。
有没有办法区分用于每个 @Controller
或 @RequestMapping
的 ObjectMapper?
对象映射器 Bean
@Bean
public ObjectMapper objectMapper() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.featuresToEnable(
JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER);
return builder.build();
}
请求映射接口方法
@ApiOperation(value = "Returns the toll cost breakdown for a journey", notes = "", response = TotalCost.class, tags={ "pricing", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "successful operation", response = TotalCost.class) })
@RequestMapping(value = "/pricing/journeyCost",
produces = { "application/json" },
consumes = { "application/vnd.toll-pricing+json" },
method = RequestMethod.POST)
ResponseEntity<TotalCost> getTollBreakdownFromEncodedPoly(@ApiParam(value = "Request object representing the journey" ,required=true ) @RequestBody Journey body);
如果您有自定义 MIME 类型,则可以注册一个自定义 HttpMessageConverter,它使用特殊的 ObjectMapper
作为您的 MIME 类型,并且 returns false
来自 canRead/canWrite
对于常规 MIME 类型。你像这样注册你的自定义HttpMessageConverter
:
@EnableWebMvc
@Configuration
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
messageConverters.add(myCustomMessageConverter());
super.configureMessageConverters(converters);
}
}
认为它与内容协商有关,与 URL 映射无关; URL 映射 (@RequestMapping
) 用于查找处理程序,而不是用于选择要使用的 marshaller/unmarshaller。
我在另一个用户链接到我的另一个 Whosebug 问题中找到了答案 -
我只需要将以下 @Bean
添加到我的 @Configuration
@Bean
public HttpMessageConverters customConverters() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.featuresToEnable(
JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER);
final AbstractJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(builder.build());
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.valueOf("application/vnd.toll-pricing+json")));
return new HttpMessageConverters(converter);
}
我有一个使用自定义 MIME 类型的 @RequestMapping
。该请求使用 @Configuration
中定义的 ObjectMapper
bean 来启用 JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER
.
此功能允许使用通常无效的 json(将反斜杠视为非特殊字符),这是此特定 @RequestMapping
允许 google 编码折线的要求直接解析。然而,这意味着这个 ObjectMapper 现在正用于我的 @RequestMapping
的 ALL,而实际上它只是一个要求。
有没有办法区分用于每个 @Controller
或 @RequestMapping
的 ObjectMapper?
对象映射器 Bean
@Bean
public ObjectMapper objectMapper() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.featuresToEnable(
JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER);
return builder.build();
}
请求映射接口方法
@ApiOperation(value = "Returns the toll cost breakdown for a journey", notes = "", response = TotalCost.class, tags={ "pricing", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "successful operation", response = TotalCost.class) })
@RequestMapping(value = "/pricing/journeyCost",
produces = { "application/json" },
consumes = { "application/vnd.toll-pricing+json" },
method = RequestMethod.POST)
ResponseEntity<TotalCost> getTollBreakdownFromEncodedPoly(@ApiParam(value = "Request object representing the journey" ,required=true ) @RequestBody Journey body);
如果您有自定义 MIME 类型,则可以注册一个自定义 HttpMessageConverter,它使用特殊的 ObjectMapper
作为您的 MIME 类型,并且 returns false
来自 canRead/canWrite
对于常规 MIME 类型。你像这样注册你的自定义HttpMessageConverter
:
@EnableWebMvc
@Configuration
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
messageConverters.add(myCustomMessageConverter());
super.configureMessageConverters(converters);
}
}
认为它与内容协商有关,与 URL 映射无关; URL 映射 (@RequestMapping
) 用于查找处理程序,而不是用于选择要使用的 marshaller/unmarshaller。
我在另一个用户链接到我的另一个 Whosebug 问题中找到了答案 -
我只需要将以下 @Bean
添加到我的 @Configuration
@Bean
public HttpMessageConverters customConverters() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.featuresToEnable(
JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER);
final AbstractJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(builder.build());
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.valueOf("application/vnd.toll-pricing+json")));
return new HttpMessageConverters(converter);
}