Spring Boot Actuator 漂亮的打印 JSON
Posted
技术标签:
【中文标题】Spring Boot Actuator 漂亮的打印 JSON【英文标题】:Spring Boot Actuator pretty print JSON 【发布时间】:2016-07-07 07:14:13 【问题描述】:在我的Spring Boot 1.3.3.RELEASE
应用程序中,Actuator 的 Health 端点返回以下 JSON:
"status":"UP","diskSpace":"status":"UP","total":120031539200,"free":109277069312,"threshold":10485760
我想返回漂亮的打印 JSON,而不是单个字符串。
我在application.yml
中添加了以下配置:
spring:
jackson:
serialization:
INDENT_OUTPUT: true
但输出仍然返回相同的单个字符串。
如何正确配置我的应用程序以返回漂亮的 JSON?
更新
这是我的application.yml
server:
port: @server.port@
contextPath: /dashboard
management:
contextPath: /actuator
spring:
jackson:
serialization:
INDENT_OUTPUT: true
jmx:
enabled: false
aop:
proxy-target-class: @spring.aop.proxy-target-class@
security:
bcryptPasswordEncoder:
strength: @spring.security.bcryptPasswordEncoder.strength@
datasource:
driverClassName: @spring.datasource.driverClassName@
url: @spring.datasource.url@
username: @spring.datasource.username@
password: @spring.datasource.password@
jpa:
database: @spring.jpa.database@
database-platform: @spring.jpa.database-platform@
hibernate.ddl-auto: @spring.jpa.hibernate.ddl-auto@
show-sql: @spring.jpa.show-sql@
ok:
client:
clientSecret: @ok.client.clientSecret@
clientPublicKey: @ok.client.clientPublicKey@
这是父 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<name>Project Name</name>
<properties>
<springframework.boot.version>1.3.3.RELEASE</springframework.boot.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.source.version>1.8</java.source.version>
<java.target.version>1.8</java.target.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>$springframework.boot.version</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<modules>
<module>common</module>
<module>dashboard</module>
</modules>
</project>
这是常见的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>example</artifactId>
<groupId>com.example</groupId>
<version>0.0.1</version>
</parent>
<artifactId>common</artifactId>
<packaging>jar</packaging>
<properties>
<spring-boot.version>1.3.1.RELEASE</spring-boot.version>
<hibernate.version>5.1.0.Final</hibernate.version>
<hibernate-jpa.version>1.0.0.Final</hibernate-jpa.version>
<hibernate-validator>5.2.4.Final</hibernate-validator>
<querydsl.version>3.7.2</querydsl.version>
<commons-lang3.version>3.4</commons-lang3.version>
<togglz.version>2.2.0.Final</togglz.version>
<UserAgentUtils.version>1.19</UserAgentUtils.version>
</properties>
<dependencies>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>$hibernate.version</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>$hibernate-validator</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>$hibernate-jpa.version</version>
</dependency>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<!-- QueryDSL -->
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>$querydsl.version</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>$querydsl.version</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>$UserAgentUtils.version</version>
</dependency>
<!-- Togglz -->
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-web</artifactId>
<version>$togglz.version</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-security</artifactId>
<version>$togglz.version</version>
</dependency>
<!-- Joda time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>$commons-lang3.version</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>$project.basedir/src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>$project.build.directory/generated-resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>$java.source.version</source>
<target>$java.target.version</target>
<optimize>true</optimize>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<encoding>$project.build.sourceEncoding</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>$java.source.version</source>
<target>$java.target.version</target>
</configuration>
</plugin>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
这个主要的 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>example</artifactId>
<groupId>com.example</groupId>
<version>0.0.1</version>
</parent>
<artifactId>dashboard</artifactId>
<packaging>war</packaging>
<properties>
<com.example.common.version>0.0.1</com.example.common.version>
<!-- 3rdparty -->
<primefaces.version>5.3</primefaces.version>
<primefaces-themes.version>1.0.10</primefaces-themes.version>
<jsf.version>2.2.13</jsf.version>
<togglz.version>2.2.0.Final</togglz.version>
</properties>
<repositories>
<repository>
<id>prime-repo</id>
<name>PrimeFaces Maven Repository</name>
<url>http://repository.primefaces.org</url>
<layout>default</layout>
</repository>
</repositories>
<profiles>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>$com.example.common.version</version>
</dependency>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Togglz -->
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-servlet</artifactId>
<version>$togglz.version</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-jsf</artifactId>
<version>$togglz.version</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-console</artifactId>
<version>$togglz.version</version>
</dependency>
<!-- Apache Commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<!-- PostgreSQL -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!-- JSF and primefaces -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>$jsf.version</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>$jsf.version</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>$primefaces.version</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>all-themes</artifactId>
<version>$primefaces-themes.version</version>
</dependency>
<!-- Apache Tomcat embedded -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<scope>provided</scope>
</dependency>
<!-- Togglz -->
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-servlet</artifactId>
<version>$togglz.version</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-web</artifactId>
<version>$togglz.version</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-security</artifactId>
<version>$togglz.version</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-jsf</artifactId>
<version>$togglz.version</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-console</artifactId>
<version>$togglz.version</version>
</dependency>
</dependencies>
<build>
<outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>$project.basedir/src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>$project.build.directory/generated-resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>$java.source.version</source>
<target>$java.target.version</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
Chrome 屏幕截图:
【问题讨论】:
【参考方案1】:类紫杉醇。有两种方法可以美化输出:
application.yml
spring:
jackson:
serialization:
INDENT_OUTPUT: true
或
application.properties
spring.jackson.serialization.INDENT_OUTPUT=true
参考:https://github.com/lenicliu/eg-spring/tree/master/eg-spring-boot/eg-spring-boot-pretty-json
chrome 和 curl 都可以正常工作。
请检查application.yml的位置,你放在哪里?
src/main/resources/application.yml
更新
@SpringBootApplication
public class Application
public static void main(String[] args)
System.setProperty("spring.jackson.serialization.INDENT_OUTPUT", "true");
SpringApplication.run(Application.class, args);
【讨论】:
我在 src/main/resources/application.yml 中添加了 spring: jackson: serialization: INDENT_OUTPUT: true 另外,我还有许多其他可以正常工作的属性。我不知道为什么但是这个解决方案对我不起作用 我觉得很奇怪,能否请您提供整个 application.yml 和 pom.xml? 我添加了 application.yml 和 pom 文件 application.yml 和 pom.xml 对我来说看起来不错,尝试通过硬代码设置选项(在答案中更新),您是否自定义了 httpmessageconvertor 的 bean?注意空格和换行符。 我已经尝试使用硬代码选项,有/没有覆盖@Ceva24 下面的答案中的消息转换器 .. 仍然是相同的结果。我已将 Chrome 中的屏幕截图添加到问题正文中。【参考方案2】:尽管已经提到了所有很棒的答案,我希望它们对我有用,但是在spring-boot-starter-1.3.3.RELEASE
中,让我的 JSON 打印出来的唯一配置是Jenky's 在这里回答:Jackson PrettyPrint for Spring 4
为方便起见,我从该帖子中复制了两种可能的解决方案,XML 配置或代码配置。
选项 1:XML 配置
<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="objectMapper">
<array>
<bean class="com.yourproject.example.CustomObjectMapper"/>
</array>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
选项 2:代码配置
@Configuration
public class CustomWebMvcConfiguration extends WebMvcConfigurationSupport
@Override
protected void extendMessageConverters( List<HttpMessageConverter<?>> converters )
for ( HttpMessageConverter<?> converter : converters )
if ( converter instanceof MappingJackson2HttpMessageConverter )
MappingJackson2HttpMessageConverter jacksonConverter = (MappingJackson2HttpMessageConverter) converter;
jacksonConverter.setPrettyPrint( true );
【讨论】:
这对我的 Spring Boot 应用程序产生了一些副作用(某些 url 不可用)。请参阅@lenicliu 的答案 - 它更简单、更清洁。 此解决方案已确认可在 Spring 2.1.x 中正常工作。所有 JSON 内容类型都以漂亮的方式返回【参考方案3】:鉴于 Spring Boot 2 已经发布了将近一年,我想为此添加一个解决方案。是的,该问题针对 1.3.3,但已经有针对 1.5 的答案,并且由于标题错过了目标版本,我在谷歌上搜索了 2.1.1 版本时遇到了这个问题,我们遇到了同样的问题:
虽然修剪后的输出适用于机器,但您提供的屏幕截图暗示您希望在浏览器中读取端点(在本例中为health
)的输出。 this answer 结尾处描述的解决方案通过将原始端点替换为“包装”原始端点的自定义端点来允许两者(以及更多,如果需要)。通过这样做,您可以返回即漂亮打印的 JSON(注意 @ReadOperation
的 produces
属性)和/或例如以您想要的任何方式设置 html 样式。
【讨论】:
【参考方案4】:似乎 Spring boot 1.5.1.RELEASE 没有使用 MappingJackson2HttpMessageConverter
我找到了更改我自己的 ObjectMapper 对象并将其提供给 Jacksson2JsonMessageConverter 的方法,在同一个类中我定义了我添加了这个 @Bean 的 rabbit 模板,以及所有json 消息像 JSON 美化一样生成
@Bean
public Jackson2JsonMessageConverter producerJackson2MessageConverter()
Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
ObjectMapper jsonObjectMapper = new ObjectMapper();
jsonObjectMapper.enable(SerializationFeature.INDENT_OUTPUT);
converter.setJsonObjectMapper(jsonObjectMapper);
return converter;
【讨论】:
【参考方案5】:返回的输出只是 json,渲染将取决于您使用什么来查看数据。例如,在 chrome 中,有一些插件可用于渲染 json “漂亮”。我使用邮递员进行端点测试,它会格式化 json 响应。
【讨论】:
【参考方案6】:尝试覆盖消息转换器 bean 以使用自定义对象映射器,如下所示:
@Bean
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter()
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
converter.setObjectMapper(mapper);
return converter;
【讨论】:
不幸的是同样的结果 奇怪 - 我刚刚使用执行器创建了一个全新的 Spring Boot 1.3.3 应用程序,它对我有用。豆子肯定被使用了吗?它在一个用@Configuration 注释的类中吗?如果是这样,那么我只能猜测还有其他一些配置设置妨碍了。 你如何测试输出? 我仍在接收 Chrome 单行字符串 (以上是关于Spring Boot Actuator 漂亮的打印 JSON的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot 2.X(十六):应用监控之 Spring Boot Actuator 使用及配置
无法访问 Spring Boot Actuator“/actuator”端点