为每个 REST API 响应美化或返回漂亮的 JSON 响应

Posted

技术标签:

【中文标题】为每个 REST API 响应美化或返回漂亮的 JSON 响应【英文标题】:Beautify or return pretty JSON response for every REST API response 【发布时间】:2021-08-26 01:45:21 【问题描述】:

我的 REST API 响应如下所示:

"business_id": "vcNAWiLM4dR7D2nwwJ7nCA", "full_address": "4840 E Indian School Rd\nSte 101\nPhoenix, AZ 85018", "hours": "Tuesday": "close": "17:00", "open": "08:00", "Friday": "close": "17:00", "open": "08:00", "Monday": "close": "17:00", "open": "08:00", "Wednesday": "close": "17:00", "open": "08:00", "Thursday": "close": "17:00", "open": "08:00", "open": true, "categories": ["Doctors", "Health & Medical"], "city": "Phoenix", "review_count": 7, "name": "Eric Goldberg, MD", "neighborhoods": [], "longitude": -111.98375799999999, "state": "AZ", "stars": 3.5, "latitude": 33.499313000000001, "attributes": "By Appointment Only": true, "type": "business"

是否有任何 HTTP 标头或类似的通用标头可以美化此 JSON 响应?

这是一个现有项目,所以我不能真正使用任何 3rd 方库在每个服务或控制器层中更改它。我需要一些适用于每个响应的东西。

预期响应:


  "business_id": "vcNAWiLM4dR7D2nwwJ7nCA",
  "full_address": "4840 E Indian School Rd\nSte 101\nPhoenix, AZ 85018",
  "hours": 
    "Tuesday": 
      "close": "17:00",
      "open": "08:00"
    ,
    "Friday": 
      "close": "17:00",
      "open": "08:00"
    ,
    "Monday": 
      "close": "17:00",
      "open": "08:00"
    ,
    "Wednesday": 
      "close": "17:00",
      "open": "08:00"
    ,
    "Thursday": 
      "close": "17:00",
      "open": "08:00"
    
  ,
  "open": true,
  "categories": [
    "Doctors",
    "Health & Medical"
  ],
  "city": "Phoenix",
  "review_count": 7,
  "name": "Eric Goldberg, MD",
  "neighborhoods": [],
  "longitude": -111.983758,
  "state": "AZ",
  "stars": 3.5,
  "latitude": 33.499313,
  "attributes": 
    "By Appointment Only": true
  ,
  "type": "business"

我正在寻找类似“Content-type”标头之类的东西来决定响应的格式。

【问题讨论】:

【参考方案1】:

Spring 使用ObjectMapper 类来序列化/反序列化 json 对象。您可以通过提供自定义配置来覆盖它:

@Configuration
public class ObjectMapperConfig 
    @Bean
    public ObjectMapper objectMapper() 
        return new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
    

这样,您应用程序中的任何序列化 json(不仅是 http 响应)都会“漂亮”。

但是,如果您只想美化 http 响应。您可以通过以下方式覆盖AbstractHttpMessageConverter 类:

@Component
public class PrettyJsonMessageConverter extends AbstractHttpMessageConverter<Object> 

    @Autowired
    private ObjectMapper objectMapper;

    public PrettyJsonMessageConverter() 
        super(MediaType.APPLICATION_JSON);
    

    @Override
    protected boolean supports(Class<?> clazz) 
        return true;
    

    @Override
    protected Object readInternal(Class<? extends Object> clazz,
                                  HttpInputMessage inputMessage) throws HttpMessageNotReadableException, IOException 
        return objectMapper.readValue(inputMessage.getBody(), clazz);
    

    @Override
    protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException 
        outputMessage.getBody().write(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(o));
    

【讨论】:

同样,这适用于单个响应或端点。我正在寻找类似“Content-type”标头的东西来决定响应的格式。 @ASAkram 没有自动更改此设置的标头。 @ASAkram 一旦您配置了 objectMapper 或 AbstractHttpMessageConverter,它将适用于所有响应,您无需采取进一步行动。

以上是关于为每个 REST API 响应美化或返回漂亮的 JSON 响应的主要内容,如果未能解决你的问题,请参考以下文章

WordPress REST API - 有没有办法在帖子响应中返回类别和父类别名称?

Spring boot rest api - 我可以在不为响应对象创建任何 java 类(DTO 或实体)的情况下获得响应吗?

有没有办法在 FastAPI 中漂亮地打印/美化 JSON 响应?

(二)Django REST实践:最简单的REST API实现

api网关中的响应模板不以美化格式返回json

Spring Boot REST API 返回 XML 作为响应