【问题标题】:Springboot-why i cannot set url localhost:8080/helloworld to get text "hello world"?Springboot-为什么我不能设置 url localhost:8080/helloworld 来获取文本“hello world”?
【发布时间】:2019-10-24 06:24:42
【问题描述】:

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

我只想,每次人们访问“localhost:8080/helloworld”时 文本“Hello World!”应该显示出来。

但是,我收到错误“应用程序无法启动”,请查看屏幕截图和错误消息。

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

有谁知道,有什么问题吗?谢谢!

第 1 部分:myApplication.java

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);
    }

}

第 2 部分:myController.java

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!";
    }

}

第三部分:pom.xml

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

第 4 部分:错误消息

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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'

【问题讨论】:

    标签: java spring-boot


    【解决方案1】:
    Caused by: java.net.BindException: Address already in use: bind
    

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

    故障排除步骤。

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

    lsof -i :8080
    

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

    【讨论】:

    • 在 Windows 上,运行 netstat -ano | findstr 8080。最后一个值是进程 ID。
    • 谢谢!我试着点击红色按钮终止运行进度,然后按绿色按钮运行,它工作了..
    • 但是非常感谢您提供的故障排除方法!
    • 如果您觉得这个答案有帮助,请接受。另一个答案也很满意,随便选一个吧:)
    【解决方案2】:

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

    根据上述异常信息,

    引起:java.net.BindException:地址已在使用:绑定 在 sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_92] 在 sun.nio.ch.Net.bind(Unknown Source) ~[na:1.8.0_92]

    表示spring boot默认的8080端口已经被占用。这意味着另一个应用程序正在同一端口 8080 上运行。要解决此问题,您可以参考以下方法之一。

    1. 您可以在spring boot应用程序的application.properties文件中配置server.port=8090。您可以根据方便提及端口号。
    2. 在 eclipse 或 intellij Idea 的 VM 参数中仅配置 -Dserver.port=8090
    3. 如果你有spring boot fat jar,可以使用java -jar springboot.jar --server.port=8090命令运行。我给的名字是springboot.jar,你可以为spring boot应用取自己的名字。

    【讨论】:

      【解决方案3】:

      我注意到了两件事。

      1. 您的地址已被使用。

      解决办法:

      通过在application.properties 中将端口指定为server.port=8081 或任何其他空闲端口来更改端口,或者像克里斯托弗所说的那样停止当前进程。

      1. 您正在从控制器方法返回字符串。

      如果您希望页面在浏览器上显示Hello world,请使用@ResponseBody 作为方法。

      【讨论】:

        【解决方案4】:

        以上所有答案都很好。还有一个原因:可能是您已经启动了一次应用程序,但没有停止它,更改了某些内容并尝试再次启动它。检查是否有 Java 进程正在运行,如果您看到之前启动的应用程序,请将其终止。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-09-12
          • 2010-11-11
          • 1970-01-01
          • 1970-01-01
          • 2015-05-07
          • 1970-01-01
          • 2014-11-22
          • 2019-02-04
          相关资源
          最近更新 更多