Spring Boot Actuator 精美打印 JSON

Spring Boot Actuator pretty print JSON

在我的 Spring Boot 1.3.3.RELEASE 应用程序执行器的健康端点 return 之后 JSON:

{"status":"UP","diskSpace":{"status":"UP","total":120031539200,"free":109277069312,"threshold":10485760}}

我想要 return 漂亮的打印而不是单个字符串 JSON.

我在 application.yml 中添加了以下配置:

spring:
  jackson:
    serialization:
      INDENT_OUTPUT: true

但输出仍然是 return 相同的单个字符串。

如何正确配置我的应用程序以便 return pretty-print 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@

这是parentpom.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 截图:

返回的输出只是 json,渲染将取决于您使用什么来查看数据。例如,在 chrome 中,有可用于呈现 json "pretty" 的插件。我使用邮递员进行端点测试,它会格式化 json 响应。

尝试覆盖消息转换器 bean 以使用自定义对象映射器,如下所示:

@Bean
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {

    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.INDENT_OUTPUT, true);

    converter.setObjectMapper(mapper);

    return converter;
}

亚历山大。有两种方法可以美化输出:

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);
    }
}

尽管已经提到了所有很棒的答案,但我希望它们对我有用,但在 spring-boot-starter-1.3.3.RELEASE 中,唯一让我的 JSON 漂亮打印的配置是 Jenky's answer here:

为方便起见,我从 post 复制了两个可能的解决方案,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 1.5.1.RELEASE 没有使用 MappingJackson2HttpMessageConverter

我找到了将我自己的 ObjectMapper 对象更改并提供给 Jacksson2JsonMessageConverter 的方法,在同一个 class 我定义了我添加这个@Bean 的兔子模板,并且所有 json 消息都像 JSON 美化

一样生成
@Bean
public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
    Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
    ObjectMapper jsonObjectMapper = new ObjectMapper();
    jsonObjectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    converter.setJsonObjectMapper(jsonObjectMapper);
    return converter;
}

鉴于 Spring Boot 2 已推出将近一年,我想为此添加一个解决方案。是的,这个问题针对的是 1.3.3,但是已经有 1.5 的答案了,而且由于标题没有找到目标版本,我在谷歌搜索 2.1.1 版本时遇到了这个问题,我们遇到了同样的问题:

虽然修剪后的输出对于机器来说很好,但您提供的屏幕截图暗示您想要在浏览器中读取端点(在本例中为 health)的输出。 this answer 末尾描述的解决方案通过将原始端点替换为 "wraps" 原始端点的自定义端点来允许两者(如果需要,还可以更多)。通过这样做,您可以 return 即漂亮的打印 JSON (注意 @ReadOperationproduces 属性) and/or 例如HTML 样式随心所欲。