【问题标题】:Spring Boot Actuator Shutdown Endpoint with Spring RestTemplate Client: Error 415 Unsupported Media TypeSpring Boot Actuator Shutdown Endpoint with Spring RestTemplate Client:错误 415 不支持的媒体类型
【发布时间】:2020-11-03 08:49:46
【问题描述】:

我试图运行 Spring Boot 微服务测试,本文对此进行了解释: https://blog.codecentric.de/en/2017/02/integration-testing-strategies-spring-boot-microservices-part-2/

在这些测试中,Spring Boot 应用程序在每次测试之前和之后以编程方式启动和停止,使用 Spring RestTemplate Client 和 Spring Boot Actuator“关闭”端点。

很遗憾,此代码在 Spring Boot 2.3.1 中不起作用并返回“错误 415 不支持的媒体类型”

ResponseEntity<JSONObject> response = template
                        .postForEntity(managementUrl + "/shutdown", "", JSONObject.class);

测试后必须在管理控制台中手动终止应用程序。

完整的源代码可以在 GitLab 上找到: https://gitlab.com/dfeingol/springboot-testing-tips/-/tree/master/atdd

这是一个非常有趣的测试策略,也是在测试中使用 Spring Boot Docker 映像的绝佳替代方案。

可惜文章和源码都很老了,用的是Spring Boot 1.4.0

有谁知道如何使用 Spring Boot Actuator “shutdown”端点和 Spring RestTemplate Client 正确关闭 Spring Boot 2.3.1 应用程序?

【问题讨论】:

    标签: spring-boot microservices integration-testing spring-boot-actuator spring-resttemplate


    【解决方案1】:

    你缺少HttpHeader,请看答案:

    POST request via RestTemplate in JSON

    您还需要启用端点并通过 HTTP 公开它:

    management.endpoints.web.expose=*
    management.endpoint.shutdown.enabled=true
    

    【讨论】:

      【解决方案2】:

      感谢您的帮助,Umesh Sanwal!

      以下代码对我有用:

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<String>(null, headers);
        ResponseEntity<String> response = template.postForEntity(managementUrl + "/shutdown", entity, String.class);
      
      

      我能够将文章中的代码更新到最新的 Spring Boot (2.3.1) 和 Cucumber (6.2.2) 并修复所有测试:

      参见 Spring Boot 微服务测试策略一文: https://blog.codecentric.de/en/2017/02/integration-testing-strategies-spring-boot-microservices-part-2/

      在我的 GitHub 上查看完整更新的代码: https://github.com/skyglass/skyglass-composer/tree/master/springboot-testing-tips

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-26
        • 2021-10-12
        • 2016-09-10
        • 2015-06-18
        • 1970-01-01
        • 2017-06-21
        • 2013-01-13
        相关资源
        最近更新 更多