当前位置: 首页 > 知识库问答 >
问题:

Spring Boot致动器漂亮打印JSON

邵华皓
2023-03-14

在我的Spring Boot1.3.3.Release应用程序执行器的健康endpoint返回以下JSON:

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

而不是一个字符串,我想返回漂亮的打印JSON。

我已将以下配置添加到application.yml中:

spring:
  jackson:
    serialization:
      INDENT_OUTPUT: true

已更新

这是我的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>
<?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>
<?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>

共有1个答案

巫马庆
2023-03-14

尽管前面提到了所有很好的答案,我希望它们对我有用,但是在spring-boot-starter-1.3.3.release中,唯一能让我的JSON打印得很漂亮的配置是Jenky在这里给出的答案:Jackson PrettyPrint for Spring4

为了方便起见,我从这篇文章中复制了两种可能的解决方案,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 );

            }

        }

    }

}
 类似资料:
  • 问题内容: 我正在创建一个JSON文件的脚本。现在,我只是使用(PHP 5.2.x)将数组编码为JSON输出。然后,我将返回值打印到文件中并保存。问题是客户端希望能够打开这些JSON文件以提高可读性,因此我想在其中添加换行符并“漂亮打印” JSON输出。有关如何执行此操作的任何想法?我唯一可以看到的替代方法是完全不使用,而只是手动写入文件内容,并为每行添加自己的换行符。 这是我得到的: 这就是我想

  • 下面是我的例子: 如何为JSONARRAY启用漂亮的打印? 我认为这足以解释我的问题,但是这里有一些bla bla bla bla bla bla因为stackoverflow问我更多的细节,让我发布问题。

  • 问题内容: 如果有人对JSON的打印非常了解,那么我将不胜感激! 我正在使用以下功能将JSON字符串移动到文件中,以将复杂的python字符串转换为JSON格式: 问题是我遇到了方括号的语法错误,因为这对我来说是个新话题,我不知道该如何解决。我需要的JSON格式如下: 我正在使用Google Visualization API,您可能对此比较熟悉,但是我需要动态图。上面的代码是API创建图形所需的

  • 问题内容: 我正在构建一个PHP脚本,该脚本将JSON数据提供给另一个脚本。我的脚本将数据构建到一个大的关联数组中,然后使用来输出数据。这是一个示例脚本: 上面的代码产生以下输出: 如果您的数据量很少,那就太好了,但是我希望遵循以下几点: 有没有办法在PHP中做到这一点而又没有丑陋的破解?似乎有人在Facebook上找到了答案。 问题答案: PHP 5.4提供了用于调用的选项。 http://ph

  • 问题内容: 我正在尝试使用javax.json API漂亮地打印json 我当前使用的代码如下: 我收到以下异常: 问题答案: 您应该使用JsonWriter而不是JsonGenerator。 替换这些行: 用这些:

  • 问题内容: 我使用以下代码将a漂亮地打印到JSON中: 输出: 这有点 太多了 (每个列表元素的换行!)。 我应该使用哪种语法: 代替? 问题答案: 几年后,我找到了带有内置模块的解决方案: 输出: