greengold
greengold

Reputation: 1333

Make Actuator output JSON instead of XML

I am using XmlMapper widely in the Spring Boot application. I have declared a mapper config like:

@Configuration
@RequiredArgsConstructor
public class JacksonMapperConfig {

    @Bean
    public SimpleModule confidentialFieldJacksonModule() {
        SimpleModule simpleModule = new SimpleModule();
        return simpleModule;
    }

    @Bean
    public XmlMapper getObjectMapper(SimpleModule confidentialFieldJacksonModule) {
        XmlMapper xmlMapper = new XmlMapper();

        xmlMapper.registerModule(confidentialFieldJacksonModule);

        return xmlMapper;
    }

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer(SimpleModule confidentialFieldJacksonModule) {
        return builder -> builder.modules()
                .modules(confidentialFieldJacksonModule)
                .build();
    }
}

I also have the Actuator on the classpath, and I want to see it's output as JSON.

Instead I have an unreadable xml:

<Map><_links><self><href>https://localhost:9001/actuator</href><templated>false</templated></self><beans><href>https://localhost:9001/actuator/beans</href><templated>false</templated></beans><caches-cache><href>https://localhost:9001/actuator/caches/{cache}</href><temp.....

I tried to declare ObjectMapper as a Bean like:

    @Bean
    @Primary
    public ObjectMapper getObjectMapper() {
        return new ObjectMapper();
    }

but application won't start:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.frequentis.nrdbsimulator.service.CallerInfoTypeViewService required a bean of type 'com.fasterxml.jackson.dataformat.xml.XmlMapper' that could not be found.


Action:

Consider defining a bean of type 'com.fasterxml.jackson.dataformat.xml.XmlMapper' in your configuration.

it is the WebService-enabled application with SOAP endpoints, therefore I need XmlMapper.

How can I make Actuator output JSON?

Thanks

EDIT: adding pom parts:

    <properties>
        <java.version>11</java.version>
        <mapstruct.version>1.5.5.Final</mapstruct.version>
        <vaadin.version>23.4.0</vaadin.version>
        <vaadin.toggle.version>1.0.2</vaadin.toggle.version>
        <jackson.xml.version>2.15.4</jackson.xml.version>
        <jaxws-maven-plugin.version>4.0.2</jaxws-maven-plugin.version>
        <version.maven.assembly.plugin>3.6.0</version.maven.assembly.plugin>
    </properties>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-lumo-theme</artifactId>
            <version>${vaadin.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.cache</groupId>
            <artifactId>cache-api</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.9.2</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jcache</artifactId>
            <version>5.6.15.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>5.6.15.Final</version>
        </dependency>

        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>grid-renderers-collection-addon</artifactId>
            <version>2.7.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>

        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>1.13.0</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>2.2.224</version>
            <scope>runtime</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-envers -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-envers</artifactId>
            <version>5.6.15.Final</version>
        </dependency>

        <dependency>
            <groupId>org.logback-extensions</groupId>
            <artifactId>logback-ext-spring</artifactId>
            <version>0.1.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.17.2</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>

        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
        </dependency>

<!--        <dependency>-->
<!--            <groupId>jakarta.xml.bind</groupId>-->
<!--            <artifactId>jakarta.xml.bind-api</artifactId>-->
<!--            <version>3.0.1</version>-->
<!--        </dependency>-->
        <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>

        <dependency>
            <groupId>javax.jws</groupId>
            <artifactId>javax.jws-api</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.12</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
        </dependency>

        <dependency>
            <groupId>org.vaadin.olli</groupId>
            <artifactId>file-download-wrapper</artifactId>
            <version>6.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.14.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>com.jayway.jsonpath</groupId>
                    <artifactId>json-path</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.ws.xmlschema</groupId>
            <artifactId>xmlschema-core</artifactId>
            <version>2.2.4</version>
        </dependency>

        <dependency>
            <groupId>org.instancio</groupId>
            <artifactId>instancio-core</artifactId>
            <version>4.5.0</version>
        </dependency>

        <dependency>
            <groupId>org.instancio</groupId>
            <artifactId>instancio-junit</artifactId>
            <version>4.5.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-test</artifactId>
            <version>3.1.8</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

weird is, that I checked some time ago and it was outputing JSON as expected. Now I have tried it and got xml...

spring-boot-starter-parent: v2.7.18

EDIT after migration to Spring-boot 3:

so in a different branch (b/c I can't use Java 17) out of curiosity I migrated the project to Spring 3.

Now it's mixed. When I go to http://localhost:9001/actuator/ I get:

<Map>
<timestamp>2024-05-28T17:15:30.620+00:00</timestamp>
<status>404</status>
<error>Not Found</error>
<path>/actuator/</path>
</Map>

when I go to: http://localhost:9001/actuator/info I get proper json back:

{"build":{"artifact":"nrdbsimulator","name":"NRDB simulator","time":"2024-05-28T16:56:45.491Z","version":"1.0.1","group":"com.frequentis"}}

my management-related properties are:

management.server.address=127.0.0.1
management.server.port=9001
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=loggers
management.server.ssl.enabled=false

and I can not visit http://localhost:9001/actuator/prometheus too. I get not found in XML format back.

I have micrometer dependency in my pom:

        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>

Note, I have tried with Spring Boot Parent v3.3.0 and v3.2.6 too.

And when I try to observe metric with histogram I get class not found error back, which completely doesn't make sense to me:

java.lang.ClassNotFoundException: io.prometheus.metrics.tracer.initializer.SpanContextSupplier

Any help on how to debug this appreciated

Upvotes: 1

Views: 71

Answers (0)

Related Questions