前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unregistering JMX-exposed beans on shutdown

Unregistering JMX-exposed beans on shutdown

作者头像
猫头虎
发布2024-04-08 12:21:07
700
发布2024-04-08 12:21:07
举报

🌷🍁 博主猫头虎 带您 Go to New World.✨🍁 🦄 博客首页——猫头虎的博客🎐 🐳《面试题大全专栏》 文章图文并茂🦕生动形象🦖简单易学!欢迎大家来踩踩~🌺 🌊 《IDEA开发秘籍专栏》学会IDEA常用操作,工作效率翻倍~💐 🌊 《100天精通Golang(基础入门篇)》学会Golang语言,畅玩云原生,走遍大小厂~💐

🪁🍁 希望本文能够给您带来一定的帮助🌸文章粗浅,敬请批评指正!🍁🐥

解决Spring Boot应用启动及404问题

在开发和部署Spring Boot应用程序时,遇到问题是很常见的。本篇博客将为您介绍一种常见的启动问题以及对应的解决方案。我们将首先通过错误信息来了解问题,然后逐步引入解决方案,并对其进行详细的讲解。

摘要

本文将探讨在启动Spring Boot应用程序时出现的两个常见问题:一是启动成功但无法访问接口,返回404错误;二是应用启动失败。我们将通过具体的错误信息和解决方案来说明如何解决这些问题,以及如何正确配置应用以避免类似问题。

引言

Spring Boot作为一种快速开发框架,大大提高了Java应用程序的开发效率。然而,在开发和部署过程中,您可能会遇到各种问题。这些问题可能涉及应用的配置、依赖项、服务器问题等。本篇博客将关注两个常见的问题:启动成功但返回404错误,以及应用启动失败。我们将逐步介绍这些问题的背景、原因和解决方案。

错误信息:

代码语言:javascript
复制
D:\Java\corretto-1.8.0_342\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -javaagent:D:\IDEA\apps\IDEA-U\ch-0\222.4345.14\lib\idea_rt.jar=54850 

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.7.2)

2022-11-01 09:19:00  [INFO]  org.springframework.boot.StartupInfoLogger:48  : Starting ApplicationKt on OAK with PID 20788 (E:\Project-Code\OutsideApi\build\classes\kotlin\main started by DELL in E:\Project-Code\OutsideApi) 
2022-11-01 09:19:00  [INFO]  org.springframework.boot.SpringApplication:593  : No active profile set, falling back to default profiles: default 
2022-11-01 09:19:00  [INFO]  org.springframework.context.support.AbstractApplicationContext:583  : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7ee955a8: startup date [Tue Nov 01 09:19:00 CST 2022]; root of context hierarchy 
2022-11-01 09:19:01  [INFO]  org.springframework.jmx.export.MBeanExporter:431  : Registering beans for JMX exposure on startup 
2022-11-01 09:19:01  [INFO]  org.springframework.boot.StartupInfoLogger:57  : Started JoySpaceApplicationKt in 1.018 seconds (JVM running for 1.513) 
2022-11-01 09:19:01  [INFO]  org.springframework.context.support.AbstractApplicationContext:984  : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7ee955a8: startup date [Tue Nov 01 09:19:00 CST 2022]; root of context hierarchy 
2022-11-01 09:19:01  [INFO]  org.springframework.jmx.export.MBeanExporter:449  : Unregistering JMX-exposed beans on shutdown 

Process finished with exit code 0

网上的解决方案:

引入Tomcat

代码语言:javascript
复制
	implementation("org.springframework.boot:spring-boot-starter-tomcat:2.7.2")

启动成功

,结果又会出现另一个问题,访问所有接口都变成404

代码语言:javascript
复制
HTTP/1.1 404 
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 1086
Date: Tue, 01 Nov 2022 01:28:17 GMT

<!doctype html>
<html lang="en">
<head><title>HTTP Status 404 – Not Found</title>
    <style type="text/css">h1 {
        font-family: Tahoma, Arial, sans-serif;
        color: white;
        background-color: #525D76;
        font-size: 22px;
    }

    h2 {
        font-family: Tahoma, Arial, sans-serif;
        color: white;
        background-color: #525D76;
        font-size: 16px;
    }

    h3 {
        font-family: Tahoma, Arial, sans-serif;
        color: white;
        background-color: #525D76;
        font-size: 14px;
    }

    body {
        font-family: Tahoma, Arial, sans-serif;
        color: black;
        background-color: white;
    }

    b {
        font-family: Tahoma, Arial, sans-serif;
        color: white;
        background-color: #525D76;
    }

    p {
        font-family: Tahoma, Arial, sans-serif;
        background: white;
        color: black;
        font-size: 12px;
    }

    a {
        color: black;
    }

    a.name {
        color: black;
    }

    .line {
        height: 1px;
        background-color: #525D76;
        border: none;
    }</style>
</head>
<body><h1>HTTP Status 404 – Not Found</h1>
<hr class="line"/>
<p><b>Type</b> Status Report</p>
<p><b>Message</b> /photo/takeCutPic</p>
<p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing
    to disclose that one exists.</p>
<hr class="line"/>
<h3>Apache Tomcat/8.5.23</h3></body>
</html>
Response file saved.
> 2022-11-01T092817.404.html

Response code: 404; Time: 42ms (42 ms); Content length: 1082 bytes (1.08 kB)

完美解决方案:

缺失: org.springframework.boot:spring-boot-starter-web:2.7.2 依赖

代码语言:javascript
复制
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
代码语言:javascript
复制
	implementation("org.springframework.boot:spring-boot-starter-web:2.7.2")
问题一:启动成功但返回404错误
错误信息
404 Error
404 Error
分析

当您启动应用程序后,您可能会遇到一种情况,即应用程序启动成功,但当尝试访问接口时,会返回404错误。这意味着服务器找不到请求的资源,导致访问失败。

解决方案

要解决这个问题,您需要检查应用程序的依赖项配置。很可能是因为缺少了spring-boot-starter-web依赖,导致应用程序无法正确处理HTTP请求。您可以通过在pom.xml中添加以下依赖项来解决这个问题:

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

或者,如果您使用的是Gradle,可以通过以下方式引入依赖:

代码语言:javascript
复制
implementation("org.springframework.boot:spring-boot-starter-web:2.7.2")

这将确保应用程序具备处理HTTP请求的能力,从而解决404错误问题。

问题二:应用启动失败
错误信息
应用启动失败
应用启动失败
分析

在启动Spring Boot应用程序时,您可能会遇到应用程序无法启动的情况。这可能涉及到多种问题,如缺少依赖、配置错误、端口被占用等。

解决方案

如果应用程序启动失败,首先要检查日志中的错误信息,以了解具体的问题。在这个例子中,似乎应用程序缺少了Tomcat容器。为了解决这个问题,您需要引入spring-boot-starter-tomcat依赖:

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

或者,在Gradle中:

代码语言:javascript
复制
implementation("org.springframework.boot:spring-boot-starter-tomcat:2.7.2")

这将引入Tomcat容器,并使应用程序能够在其中运行。如果有其他依赖关系或配置问题,您可以根据错误信息逐步排查并解决。

总结

在开发和部署Spring Boot应用程序时,遇到问题是正常的。本篇博客介绍了两个常见的问题:启动成功但返回404错误,以及应用启动失败。对于第一个问题,问题可能是缺少spring-boot-starter-web依赖,解决方案是引入该依赖。对于第二个问题,问题可能是缺少Tomcat容器,解决方案是引入spring-boot-starter-tomcat依赖。通过理解这些问题的背景、原因和解决方案,您将能够更好地处理类似问题,并确保您的Spring Boot应用程序正常运行。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-08-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 解决Spring Boot应用启动及404问题
    • 摘要
      • 引言
      • 错误信息:
      • 网上的解决方案:
      • 完美解决方案:
        • 问题一:启动成功但返回404错误
          • 问题二:应用启动失败
            • 总结
            相关产品与服务
            容器服务
            腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档