Spring Boot Controller 返回 JSON 统一处理 key 字符串大小写

JSON Spring Boot Jackson About 3,821 words

需求

需要对返回的JSON字符串中的key,全部转为首字母大写。

相关配置

返回值

@Setter
@Getter
@Data
public class Response {

    private int codeInfo;

    private Object data;

    @JsonProperty("msgHint")
    private String msgHint = "success";

}

实现全局Advice

@Slf4j
@RestControllerAdvice
public class MyResponseBodyAdvice implements ResponseBodyAdvice {

    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType,
                                  MediaType selectedContentType, Class selectedConverterType,
                                  ServerHttpRequest request, ServerHttpResponse response) {
        Response res = new Response();
        res.setData(body);
        return res;
    }
}

DTO

@Setter
@Getter
@ToString
public class User {

    private String nickname;

    private String idCard;

}

Controller

@RestController
public class TestController {

    @GetMapping("/testObject")
    public User testObject() {
        User user = new User();
        user.setNickname("nickname");
        user.setIdCard("idcard");
        return user;
    }

}

key 命名策略

UpperCamelCaseStrategy

首字母大写的驼峰方式,@JsonProperty优先级更高(msgHint没有受UpperCamelCaseStrategy影响)

{
    "CodeInfo": 0,
    "Data":
    {
        "Nickname": "nickname",
        "IdCard": "idcard"
    },
    "msgHint": "success"
}

LowerCamelCaseStrategy

首字母小写的驼峰方式

{
    "codeInfo": 0,
    "data":
    {
        "nickname": "nickname",
        "idCard": "idcard"
    },
    "msgHint": "success"
}

UpperSnakeCaseStrategy

大写及下划线拼接

{
    "CODE_INFO": 0,
    "DATA":
    {
        "NICKNAME": "nickname",
        "ID_CARD": "idcard"
    },
    "msgHint": "success"
}

SnakeCaseStrategy

小写及下划线拼接

{
    "code_info": 0,
    "data":
    {
        "nickname": "nickname",
        "id_card": "idcard"
    },
    "msgHint": "success"
}

LowerCaseStrategy

小写

{
    "codeinfo": 0,
    "data":
    {
        "nickname": "nickname",
        "idcard": "idcard"
    },
    "msgHint": "success"
}

KebabCaseStrategy

中划线拼接

{
    "code-info": 0,
    "data":
    {
        "nickname": "nickname",
        "id-card": "idcard"
    },
    "msgHint": "success"
}

LowerDotCaseStrategy

小写及.拼接

{
    "code.info": 0,
    "data":
    {
        "nickname": "nickname",
        "id.card": "idcard"
    },
    "msgHint": "success"
}

YAML 配置

spring:
  jackson:
    property-naming-strategy: com.fasterxml.jackson.databind.PropertyNamingStrategies.UpperCamelCaseStrategy
#    property-naming-strategy: com.fasterxml.jackson.databind.PropertyNamingStrategies.LowerCamelCaseStrategy
#    property-naming-strategy: com.fasterxml.jackson.databind.PropertyNamingStrategies.UpperSnakeCaseStrategy
#    property-naming-strategy: com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy
#    property-naming-strategy: com.fasterxml.jackson.databind.PropertyNamingStrategies.LowerCaseStrategy
#    property-naming-strategy: com.fasterxml.jackson.databind.PropertyNamingStrategies.KebabCaseStrategy
#    property-naming-strategy: com.fasterxml.jackson.databind.PropertyNamingStrategies.LowerDotCaseStrategy

Java 配置

@Configuration
public class ObjectMapperConfig {

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.UpperCamelCaseStrategy.INSTANCE);
        return objectMapper;
    }

}

单独配置

使用@JsonNaming注解指定命名策略。

@Setter
@Getter
@Data
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
public class Response {

    private int codeInfo;

    private Object data;

    @JsonProperty("msgHint")
    private String msgHint = "success";

}

注意

PropertyNamingStrategy不适用于Map集合。

PropertyNamingStrategy not work on Map #2559

Views: 139 · Posted: 2024-09-18

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh