Spring Boot 为什么我不能设置url localhost:8080/helloworld来获取文本“hello world”?

cgfeq70w  于 2023-11-17  发布在  Spring
关注(0)|答案(5)|浏览(190)

我构建的只是一个非常简单的springboot项目,只有一个类用于application,一个类用于controller。
我只是想,每次当人们访问“localhost:8080/helloworld”的文本“你好,世界!”应该显示。
然而,我得到了一个错误“应用程序FASTINGTO START”,请参阅截图和错误信息。
如果我只是删除这个controller.java,它将被编译好。所以问题一定是编译器。java
有没有人知道,怎么了?谢谢!
第一部分:myApplication.java

  1. package com.zi.sbprojects;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class ApiApp {
  6. public static void main(String[] args) {
  7. SpringApplication.run(ApiApp.class, args);
  8. }
  9. }

字符串
第二部分:myController.java

  1. package com.zi.sbprojects.helloworld;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class ControllerHelloWorld {
  6. @RequestMapping("/helloworld")
  7. public String sayHelloWorld() {
  8. return "Hello World!";
  9. }
  10. }


第3部分:pom.xml

  1. <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">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.zi.sbprojects</groupId>
  4. <artifactId>sb-firstproject</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <name>My First Spring Boot Project</name>
  7. <parent>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-parent</artifactId>
  10. <version>2.1.0.RELEASE</version>
  11. </parent>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-web</artifactId>
  16. </dependency>
  17. </dependencies>
  18. <properties>
  19. <java.version>1.8</java.version>
  20. </properties>
  21. </project>


第4部分:错误信息

  1. . ____ _ __ _ _
  2. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  3. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  4. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  5. ' |____| .__|_| |_|_| |_\__, | / / / /
  6. =========|_|==============|___/=/_/_/_/
  7. :: Spring Boot :: (v2.1.0.RELEASE)
  8. 2019-06-09 18:25:03.150 INFO 7148 --- [ main] com.zi.sbprojects.ApiApp : Starting ApiApp on N0474010 with PID 7148 (started by SangZi in C:\Spring Tools\sb-firstproject)
  9. 2019-06-09 18:25:03.155 INFO 7148 --- [ main] com.zi.sbprojects.ApiApp : No active profile set, falling back to default profiles: default
  10. 2019-06-09 18:25:05.887 INFO 7148 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
  11. 2019-06-09 18:25:05.916 INFO 7148 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  12. 2019-06-09 18:25:05.916 INFO 7148 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.12
  13. 2019-06-09 18:25:05.930 INFO 7148 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_92\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_92/bin/server;C:/Program Files/Java/jre1.8.0_92/bin;C:/Program Files/Java/jre1.8.0_92/lib/amd64;C:\Program Files\Java\jre1.8.0_92\bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files\Docker\Docker\Resources\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\BMC Software\ARSystem\dataimporttool;C:\Program Files\Git\cmd;C:\Program Files\TortoiseSVN\bin;C:\Program Files\nodejs\;.;C:\Program Files (x86)\Java\jdk1.8.0_92\bin;C:\Program Files (x86)\Java\jre1.8.0_92\bin;C:\Program Files\Maven\apache-maven-3.5.3-bin\apache-maven-3.5.3\bin;C:\Users\sangzi\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\sangzi\AppData\Roaming\npm;C:\Users\sangzi\AppData\Local\GitHubDesktop\bin;C:\Program Files\JetBrains\WebStorm 2019.1.2\bin;;C:\Users\sangzi\Downloads\spring-tool-suite-3.9.8.RELEASE-e4.11.0-win32-x86_64\sts-bundle\sts-3.9.8.RELEASE;;.]
  14. 2019-06-09 18:25:06.073 INFO 7148 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  15. 2019-06-09 18:25:06.073 INFO 7148 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2815 ms
  16. 2019-06-09 18:25:06.116 INFO 7148 --- [ main] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
  17. 2019-06-09 18:25:06.131 INFO 7148 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
  18. 2019-06-09 18:25:06.132 INFO 7148 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
  19. 2019-06-09 18:25:06.140 INFO 7148 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'formContentFilter' to: [/*]
  20. 2019-06-09 18:25:06.141 INFO 7148 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
  21. 2019-06-09 18:25:06.477 INFO 7148 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
  22. 2019-06-09 18:25:06.804 ERROR 7148 --- [ main] org.apache.catalina.util.LifecycleBase : Failed to start component [Connector[HTTP/1.1-8080]]
  23. org.apache.catalina.LifecycleException: Protocol handler start failed
  24. at org.apache.catalina.connector.Connector.startInternal(Connector.java:960) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
  25. at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
  26. at org.apache.catalina.core.StandardService.addConnector(StandardService.java:225) [tomcat-embed-core-9.0.12.jar:9.0.12]
  27. at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.addPreviouslyRemovedConnectors(TomcatWebServer.java:259) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
  28. at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:197) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
  29. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.java:300) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
  30. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:162) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
  31. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) [spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
  32. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
  33. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
  34. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
  35. at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
  36. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
  37. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
  38. at com.zi.sbprojects.ApiApp.main(ApiApp.java:10) [classes/:na]
  39. Caused by: java.net.BindException: Address already in use: bind
  40. at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_92]
  41. at sun.nio.ch.Net.bind(Unknown Source) ~[na:1.8.0_92]
  42. at sun.nio.ch.Net.bind(Unknown Source) ~[na:1.8.0_92]
  43. at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source) ~[na:1.8.0_92]
  44. at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source) ~[na:1.8.0_92]
  45. at org.apache.tomcat.util.net.NioEndpoint.initServerSocket(NioEndpoint.java:236) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
  46. at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:210) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
  47. at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1108) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
  48. at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:550) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
  49. at org.apache.catalina.connector.Connector.startInternal(Connector.java:957) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
  50. ... 14 common frames omitted
  51. 2019-06-09 18:25:06.812 INFO 7148 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
  52. 2019-06-09 18:25:06.834 INFO 7148 --- [ main] ConditionEvaluationReportLoggingListener :
  53. Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
  54. 2019-06-09 18:25:06.837 ERROR 7148 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
  55. ***************************
  56. APPLICATION FAILED TO START
  57. ***************************
  58. Description:
  59. The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured.
  60. Action:
  61. Verify the connector's configuration, identify and stop any process that's listening on port 8080, or configure this application to listen on another port.
  62. 2019-06-09 18:25:06.841 INFO 7148 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'

6jygbczu

6jygbczu1#

  1. Caused by: java.net.BindException: Address already in use: bind

字符串
这个错误有你需要的一切。你已经有一个应用程序运行在与你的spring Boot 应用程序相同的端口上。

启动步骤。

在OSX上,您可以运行以下命令:

  1. lsof -i :8080


这将给予您当前正在运行的绑定到端口8080的进程列表。

sg24os4d

sg24os4d2#

我提供下面几行,可能对你有帮助。
根据上述异常消息,
原因:java.net.BindException:地址已在用途:bind at sun.nio.ch.Net.bind0(本地方法)~[na:1.8.0_92] at sun.nio.ch.Net.bind(未知源)~[na:1.8.0_92]
这意味着Sping Boot 中默认的8080端口被占用了。这意味着另一个应用程序正在同一个8080端口上运行。要解决此问题,您可以参考以下方法之一。
1.您可以在spring Boot 应用程序中的application.properties文件中配置server.port=8090。您可以根据方便程度指定端口号。
1.在eclipse或intellij Idea中仅在VM参数中配置-Dserver.port=8090
1.如果你有spring Boot fat jar,你可以使用命令java -jar springboot.jar --server.port=8090运行。我给它起了一个名字springboot.jar,你可以为spring Boot boot应用程序起自己的名字。

ua4mk5z4

ua4mk5z43#

上面的所有答案都很好。还有一个原因:可能你已经启动了一次应用程序,没有停止它,改变了一些东西,并试图重新启动它。检查是否有Java进程正在运行,如果你看到你以前启动的应用程序,杀死它。

pbgvytdp

pbgvytdp4#

我注意到两件事。
1.您的地址已被使用。
解决方法:
通过在application.properties中将其指定为server.port=8081或任何其他空闲端口来更改端口,或者按照christopher提到的那样停止当前进程。
1.您正在从控制器方法返回String。
如果您希望页面在浏览器上显示Hello world,则使用@ResponseBody作为方法。

pokxtpni

pokxtpni5#

我也创造了同样的东西,他没有,我的项目控制台也看起来很好。
但是我的应用程序仍然没有在localhost上运行:8080/hello

相关问题