首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Springboot-为什么我不能设置url localhost:8080/helloworld来获取文本"hello world"?

Springboot-为什么我不能设置url localhost:8080/helloworld来获取文本"hello world"?
EN

Stack Overflow用户
提问于 2019-06-09 16:37:31
回答 4查看 3.3K关注 0票数 0

我构建的只是一个非常简单的springboot项目,只有一个类用于应用程序,一个类用于控制器。

我只想,每次人们访问"localhost:8080/helloworld“时,都会看到”你好世界!“应该显示出来。

但是,我收到了一个错误“应用程序启动失败”,请查看屏幕截图和错误消息。

如果我只删除这个controller.java,它就会编译得很好。所以问题一定是controller.java

有人知道吗,出什么事了?谢谢!

Part1: myApplication.java

代码语言:javascript
运行
复制
package com.zi.sbprojects;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ApiApp {

    public static void main(String[] args) {
        SpringApplication.run(ApiApp.class, args);
    }

}

Part2:myController.java

代码语言:javascript
运行
复制
package com.zi.sbprojects.helloworld;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ControllerHelloWorld {

    @RequestMapping("/helloworld")
    public String sayHelloWorld() {
        return "Hello World!";
    }

}

Part3: pom.xml

代码语言:javascript
运行
复制
<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zi.sbprojects</groupId>
  <artifactId>sb-firstproject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>My First Spring Boot Project</name>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
  </parent>

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

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

</project>

part4:错误消息

代码语言:javascript
运行
复制
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.0.RELEASE)

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)
2019-06-09 18:25:03.155  INFO 7148 --- [           main] com.zi.sbprojects.ApiApp                 : No active profile set, falling back to default profiles: default
2019-06-09 18:25:05.887  INFO 7148 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-06-09 18:25:05.916  INFO 7148 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-06-09 18:25:05.916  INFO 7148 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.12
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;;.]
2019-06-09 18:25:06.073  INFO 7148 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-06-09 18:25:06.073  INFO 7148 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2815 ms
2019-06-09 18:25:06.116  INFO 7148 --- [           main] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2019-06-09 18:25:06.131  INFO 7148 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-06-09 18:25:06.132  INFO 7148 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-06-09 18:25:06.140  INFO 7148 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'formContentFilter' to: [/*]
2019-06-09 18:25:06.141  INFO 7148 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2019-06-09 18:25:06.477  INFO 7148 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-06-09 18:25:06.804 ERROR 7148 --- [           main] org.apache.catalina.util.LifecycleBase   : Failed to start component [Connector[HTTP/1.1-8080]]

org.apache.catalina.LifecycleException: Protocol handler start failed
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:960) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
    at org.apache.catalina.core.StandardService.addConnector(StandardService.java:225) [tomcat-embed-core-9.0.12.jar:9.0.12]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.addPreviouslyRemovedConnectors(TomcatWebServer.java:259) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:197) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.java:300) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:162) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) [spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at com.zi.sbprojects.ApiApp.main(ApiApp.java:10) [classes/:na]
Caused by: java.net.BindException: Address already in use: bind
    at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_92]
    at sun.nio.ch.Net.bind(Unknown Source) ~[na:1.8.0_92]
    at sun.nio.ch.Net.bind(Unknown Source) ~[na:1.8.0_92]
    at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source) ~[na:1.8.0_92]
    at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source) ~[na:1.8.0_92]
    at org.apache.tomcat.util.net.NioEndpoint.initServerSocket(NioEndpoint.java:236) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
    at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:210) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
    at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1108) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
    at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:550) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:957) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
    ... 14 common frames omitted

2019-06-09 18:25:06.812  INFO 7148 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-06-09 18:25:06.834  INFO 7148 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-06-09 18:25:06.837 ERROR 7148 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

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.

Action:

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.

2019-06-09 18:25:06.841  INFO 7148 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-06-09 18:15:17

上面所有的答案都是好的。还有一个原因:您可能已经启动了一次应用程序,但是没有停止,更改了一些内容,并试图再次启动它。检查是否有Java进程正在运行,如果您看到您先前启动的应用程序,则关闭它。

票数 1
EN

Stack Overflow用户

发布于 2019-06-09 16:38:51

代码语言:javascript
运行
复制
Caused by: java.net.BindException: Address already in use: bind

此错误包含您所需的所有内容。您已经有一个应用程序运行在与spring引导应用程序相同的端口上。

排除故障的步骤。

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

代码语言:javascript
运行
复制
lsof -i :8080

这将给出当前正在运行的进程的列表,这些进程绑定到端口8080

票数 4
EN

Stack Overflow用户

发布于 2019-06-09 16:55:12

我提供以下几行,这可能对你有帮助。

根据上面的异常消息,

由: java.net.BindException:已在使用的地址:绑定在sun.nio.ch.Net.bind0(原生方法) ~na:1.8.0_92 at sun.nio.ch.Net.bind(未知源) ~na:1.8.0_92

这意味着,在春季引导中默认的端口8080已被占用。这意味着另一个应用程序运行在同一个端口8080中。要解决这个问题,您可以参考以下方法之一。

  1. 您可以在spring引导应用程序中的server.port=8090文件中配置application.properties。您可以在方便的基础上提及端口号。
  2. 在eclipse或intellij中仅在VM参数中配置-Dserver.port=8090
  3. 如果您有spring引导fat jar,则可以使用命令java -jar springboot.jar --server.port=8090运行。我已经将名称命名为springboot.jar,您可以为spring引导应用程序使用自己的名称。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56516446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档