jackson 无法序列化json中的大写字段

以下为springboot配置代码:

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

@Configuration
public class JacksonConfig {

    @Bean
    public Jackson2ObjectMapperBuilder jacksonBuilder() {
        Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder = new Jackson2ObjectMapperBuilder();
        jackson2ObjectMapperBuilder.visibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
        jackson2ObjectMapperBuilder.visibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        return jackson2ObjectMapperBuilder;
    }

}

重启就可以了!!!!

下面为我在stackoverflow原版代码:

https://stackoverflow.com/questions/15303110/jackson-json-field-mapping-capitalization

 @Getter
    @Setter
    static class UserInfo {
        //@JsonProperty("UUID")
        private String UUID = "11";
        private String UserName = "22";
        private String userName = "33";
        private String user_Name = "44";
        private String user_name = "55";
        private String User_name = "66";
        private boolean HasDeleted=true;
        private boolean hasDeleted=true;
        private boolean has_Deleted=true;
        private boolean has_deleted=true;
        private boolean HAS_DELETED=true;
    }

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
        objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

        String s = objectMapper.writeValueAsString(new UserInfo());
        System.out.println(s);


        UserInfo userInfo = objectMapper.readValue(s, UserInfo.class);
        System.out.println(objectMapper.writeValueAsString(userInfo));
    }

输出json结果:

{"UUID":"11","UserName":"22","userName":"33","user_Name":"44","user_name":"55","User_name":"66","HasDeleted":true,"hasDeleted":true,"has_Deleted":true,"has_deleted":true,"HAS_DELETED":true}

你可能感兴趣的:(jackson 无法序列化json中的大写字段)