Webclient + Jackson:如何设置反序列化以将 snake_case 转换为 camelCase?
Webclient + Jackson: how to setup deserialization to convert snake_case into camelCase?
我想避免在我的属性前加上 @JsonProperty("property_name")
前缀,而只是设置 Spring WebClient 生成器以将所有 snake_cases 转换为驼峰式。
这可能吗?
您可以为您的模型添加注释class:
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Model {
String camelCase; //will be camel_case in json
}
您可以按照 并只为一个模型应用配置。
或者您可以通过适当的 属性:
进行全局设置
spring:
jackson:
property-naming-strategy: SNAKE_CASE
阅读 9.4.3. Customize the Jackson ObjectMapper and 10.A.4. JSON properties 以了解我们可以从配置文件中定义多少选项。在您的情况下,您需要设置:
spring.jackson.property-naming-strategy=SNAKE_CASE
如果您只想更改反序列化的配置,您需要自定义 WebClient
的创建方式。
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class WebClientConfiguration {
@Bean
public WebClient webClient(ObjectMapper baseConfig) {
ObjectMapper newMapper = baseConfig.copy();
newMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder()
.codecs(configurer ->
configurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(newMapper)))
.build();
return WebClient.builder()
.baseUrl("http://localhost:8080")
.exchangeStrategies(exchangeStrategies)
.build();
}
}
参见:
- How to customize SpringWebFlux WebClient JSON deserialization?
我想避免在我的属性前加上 @JsonProperty("property_name")
前缀,而只是设置 Spring WebClient 生成器以将所有 snake_cases 转换为驼峰式。
这可能吗?
您可以为您的模型添加注释class:
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Model {
String camelCase; //will be camel_case in json
}
您可以按照
或者您可以通过适当的 属性:
进行全局设置spring:
jackson:
property-naming-strategy: SNAKE_CASE
阅读 9.4.3. Customize the Jackson ObjectMapper and 10.A.4. JSON properties 以了解我们可以从配置文件中定义多少选项。在您的情况下,您需要设置:
spring.jackson.property-naming-strategy=SNAKE_CASE
如果您只想更改反序列化的配置,您需要自定义 WebClient
的创建方式。
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class WebClientConfiguration {
@Bean
public WebClient webClient(ObjectMapper baseConfig) {
ObjectMapper newMapper = baseConfig.copy();
newMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder()
.codecs(configurer ->
configurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(newMapper)))
.build();
return WebClient.builder()
.baseUrl("http://localhost:8080")
.exchangeStrategies(exchangeStrategies)
.build();
}
}
参见:
- How to customize SpringWebFlux WebClient JSON deserialization?