Spring Boot反序列化蛇案到骆驼案失败.无法反序列化"some_value"到"someValue" [英] Spring Boot deserialization snake case to camel case fails. Can't deserialize "some_value" to "someValue"

查看:50
本文介绍了Spring Boot反序列化蛇案到骆驼案失败.无法反序列化"some_value"到"someValue"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个Spring Boot应用程序,该应用程序必须向参数为someValue和someOtherValue的对象发出参数为"some_value = 1500& some_other_value = 50000"的GET请求.

So I have this Spring Boot app that has to take a GET request with parameters "some_value=1500&some_other_value=50000" to an object with the attributes someValue and someOtherValue.

我已经尝试过@JsonProperty("some_value"),但是它没有用.我已经在我的application.properties文件中添加了"spring.jackson.property-naming-strategy = SNAKE_CASE",但它仍然无法正常工作.

I've tried @JsonProperty("some_value") and it didn't work. I've added "spring.jackson.property-naming-strategy=SNAKE_CASE" to my application.properties file and it still doesn't work.

重要的细节:当我尝试序列化一个对象时,它的确打开了someValue => some_value和someOtherValue => some_other_value.所以我知道配置是精细的",但是它拒绝将蛇形情况下的请求参数映射到我需要的骆驼形情况.(而且,不...我无法控制请求格式.我在蛇形套中得到了参数,必须将它们映射到骆驼套中)

Important detail: when I try to serialize an object it does turn someValue => some_value and someOtherValue => some_other_value. So I know the config is "fine" but it refuses to map the request param in snake case to the camel case that I need. (And no... I have no control over the request format. I get the params in snake case and I have to map them to camel case)

请帮助.谢谢!

推荐答案

我认为您在配置中存在一些错误,在我的情况下,原因是@EnableWebMvc批注,如果您使用此批注,Spring Boot的默认自动配置将不会工作.

I think u have some mistake in configuration , in my case the cause is @EnableWebMvc annotation, if u use this annotation , the default autoconfig from spring boot will not work.

为了将属性从camelCase转换为snake_case,您只需要覆盖bean jackson2ObjectMapperBuilder ,spring使用此bean来更改jackson的snake_case设置,请尝试以下这些代码将其添加到ur @Configuration中文件

in order to convert attribute from camelCase to snake_case , u only need to override bean jackson2ObjectMapperBuilder ,spring use this bean to change the snake_case setting for jackson , try these code below add them to ur @Configuration file

@Bean
    @Primary
    public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
        Jackson2ObjectMapperBuilder jsonBuilderConfig = new Jackson2ObjectMapperBuilder();
        jsonBuilderConfig.propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        return jsonBuilderConfig;
    }

如果您读取了 AbstractJackson2HttpMessageConverter 类下的 readJavaType()方法,则可以看到jackson的工作原理,则使用了 objectMapper 如果PropertyNamingStrategy没有snake_case设置转换无法正常工作,则将json.track转换为该对象映射器

if u read readJavaType() method which is under AbstractJackson2HttpMessageConverter class,u can see how jackson works , there is an objectMapper there used to convert json.track this objectMapper,if PropertyNamingStrategy doesn`t have snake_case setting the convertion cant work

这篇关于Spring Boot反序列化蛇案到骆驼案失败.无法反序列化"some_value"到"someValue"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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