2019独角兽企业重金招聘Python工程师标准>>>
有如下几种方法
1、通过ObjectMapper设置
mapper.setPropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy.SNAKE_CASE); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
2、通过在application.properties增加如下配置
spring.jackson.property-naming-strategy=CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES
即可
注意事项,当开启@EnableSwagger2注解时候,会报jackjson异常,查看是swagger使用的api比较旧,不支持
java.lang.NoSuchMethodException: com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.(com.fasterxml.jackson.databind.PropertyName, com.fasterxml.jackson.databind.AnnotationIntrospector, boolean)
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getConstructor(Class.java:1825)
at springfox.documentation.schema.property.PojoPropertyBuilderFactory.constructorWithParams(PojoPropertyBuilderFactory.java:105)
at springfox.documentation.schema.property.PojoPropertyBuilderFactory.jackson26Instance(PojoPropertyBuilderFactory.java:89)
at springfox.documentation.schema.property.PojoPropertyBuilderFactory.create(PojoPropertyBuilderFactory.java:42)
at springfox.documentation.schema.property.BeanPropertyDefinitions.getName(BeanPropertyDefinitions.java:69)
at springfox.documentation.schema.property.BeanPropertyDefinitions.access$000(BeanPropertyDefinitions.java:28)
at springfox.documentation.schema.property.BeanPropertyDefinitions$2.apply(BeanPropertyDefinitions.java:58)
at springfox.documentation.schema.property.BeanPropertyDefinitions$2.apply(BeanPropertyDefinitions.java:55)
at com.google.common.base.Present.transform(Present.java:77)
at springfox.documentation.schema.property.ObjectMapperBeanPropertyNamingStrategy.nameForSerialization(ObjectMapperBeanPropertyNamingStrategy.java:56)
at springfox.documentation.schema.property.BeanPropertyDefinitions.name(BeanPropertyDefinitions.java:46)
at springfox.documentation.schema.property.OptimizedModelPropertiesProvider.beanModelProperty(OptimizedModelPropertiesProvider.java:281)
at springfox.documentation.schema.property.OptimizedModelPropertiesProvider.access$200(OptimizedModelPropertiesProvider.java:79)
at springfox.documentation.schema.property.OptimizedModelPropertiesProvider$2.apply(OptimizedModelPropertiesProvider.java:163)
at springfox.documentation.schema.property.OptimizedModelPropertiesProvider$2.apply(OptimizedModelPropertiesProvider.java:155)
类似问题:https://groups.google.com/forum/#!msg/swagger-swaggersocket/AUvwW1a03mQ/ezSxddEZAgAJ
3、采用在实体增加注解实现
//实现驼峰转下划线
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class BaseResultVO {
}
实体继承该类即可。如此不用每个类的字段都注明@jsonproperty注解
参考资料:Jackson 在 Spring Boot 中的使用小结 1