首页 > 解决方案 > Springboot:显示注册的映射

问题描述

我使用 spring initilzr 创建了简单的 spring boot 应用程序。POM 是:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <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>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

然后我添加了返回字符串'hello'的简单控制器。端点工作,我很高兴,但我希望在启动应用程序期间记录所有已注册的映射。如何做到这一点?

标签: spring-boot

解决方案


一种方法是使用Spring Boot Actuator来监视应用程序映射。
然后,您将能够使用 JMX 或 HTTP 监视数据。

如何 :

  1. 添加 Spring Boot 执行器(请参阅https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-enabling

添加此依赖项:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  1. 公开映射(参见https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-endpoints-exposing-endpoints

2a。对于 HTTP:

将此行添加到application.properties

management.endpoints.web.exposure.include=info, health, mappings

然后检查此端点:http://localhost:8080/actuator/mappings(JSON 数据)

2b。对于 JMX:

将此行添加到application.properties

spring.jmx.enabled=true

然后检查以 为前缀的 MBean ,例如org.springframework.boot使用VisualVM 。


推荐阅读