如何更改ResponseEntity的消息字段格式?

问题描述 投票:0回答:1

在我的Spring-Boot应用程序中,我的API使用org.springframework.http.ResponseEntity返回响应正文>

Example:
    {
      "timestamp": "Oct 2, 2019 3:24:32 PM",
      "status": 200,
      "error": "OK",
      "message": "Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool",
      "path": "/a/b/c/d/init"
    }

我需要将消息字段格式更改为短格式。

我试图通过网络找到它,但发现了其他第三方库的引用,而不是春天的引用。

在我的应用程序中,我获得了此配置Bean:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.DateFormat;

@Configuration
public class JacksonConfiguration {

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
        objectMapper.setDateFormat(DateFormat.getDateTimeInstance());

        return objectMapper;
    }
}

有没有一种格式化消息字段的方法?

[在我的Spring-Boot应用程序中,我的API使用org.springframework.http.ResponseEntity返回响应正文,例如:{“ timestamp”:“ Oct 2,2019 3:24:32 PM”,“ status”:200,“错误“:” OK“,...

java spring format httpresponse
1个回答
0
投票

您可以在jackson中编写一个自定义json序列化程序,并通过@JsonSerialize(using=SerializerClassName.class)的注释bean属性来决定应对特定的字符串值执行什么操作>

© www.soinside.com 2019 - 2024. All rights reserved.