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

cgfeq70w  于 7个月前  发布在  Spring
关注(0)|答案(5)|浏览(80)

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

}

字符串
第二部分: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!";
    }

}


第3部分: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'

6jygbczu

6jygbczu1#

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

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

启动步骤。

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

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

相关问题