Java springboot中的本地主机连接被拒绝
我对springboot不熟悉。我有一个控制器类,如下所示Java springboot中的本地主机连接被拒绝,java,spring-boot,httprequest,spring-restcontroller,request-mapping,Java,Spring Boot,Httprequest,Spring Restcontroller,Request Mapping,我对springboot不熟悉。我有一个控制器类,如下所示 package com.example.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class Controller { @RequestMappin
package com.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
@RequestMapping("/hello")
public String getHi() {
return "Hi";
}
}
运行该项目后,我尝试在浏览器中打开“localhost:8080/hello”,但本地主机连接被拒绝。在控制台中,我可以看到,Tomcat服务器是用8080端口启动的。
提前谢谢
更新
我通过在我的主课下面添加一行来解决我的问题
@Autowired
Controller controller;
尝试访问127.0.0.1:8080/hello以确保环回有效在
@RequestMapping
@randytan上添加@RequestMapping(value=“/hello”,method=RequestMethod.GET)
但不更改检查端口8080
是否未被其他应用程序使用。或者添加server.port=8082
以使用以下url在另一个端口上测试您的应用程序:localhost:8082/hello
@HarryCoder我尝试使用8082端口。这一次我得到了“白标错误页面”,该链接可以回答您的问题:
Starting service [Tomcat]
Starting Servlet engine: [Apache Tomcat/9.0.46]
Initializing Spring embedded WebApplicationContextRoot WebApplicationContext: initialization completed in 626 ms
Tomcat started on port(s): 8080 (http) with context path ''
@Autowired
Controller controller;