当使用 Spring MVC for REST 时,如何让 Jackson 漂亮地打印渲染的 JSON? [英] When using Spring MVC for REST, how do you enable Jackson to pretty-print rendered JSON?

查看:22
本文介绍了当使用 Spring MVC for REST 时,如何让 Jackson 漂亮地打印渲染的 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Spring MVC 开发 REST 服务时,我希望在开发中呈现 JSON '漂亮打印',但在生产中呈现正常(减少空格).

While developing REST services using Spring MVC, I would like render JSON 'pretty printed' in development but normal (reduced whitespace) in production.

推荐答案

如果您使用的是 Spring Boot 1.2 或更高版本,简单的解决方案是添加

If you are using Spring Boot 1.2 or later the simple solution is to add

spring.jackson.serialization.INDENT_OUTPUT=true

application.properties 文件.这假设您使用 Jackson 进行序列化.

to the application.properties file. This assumes that you are using Jackson for serialization.

如果您使用的是较早版本的 Spring Boot,则可以添加

If you are using an earlier version of Spring Boot then you can add

http.mappers.json-pretty-print=true

此解决方案仍然适用于 Spring Boot 1.2,但它已已弃用并且最终将被完全移除.您将在启动时在日志中收到弃用警告.

This solution still works with Spring Boot 1.2 but it is deprecated and will eventually be removed entirely. You will get a deprecation warning in the log at startup time.

(使用 spring-boot-starter-web 测试)

这篇关于当使用 Spring MVC for REST 时,如何让 Jackson 漂亮地打印渲染的 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆