租用问题

质量为本、客户为根、勇于拼搏、务实创新

< 返回租用问题列表

springboot怎么监听端口请求,springboot配置监听

发布时间:2023-10-11 17:34:17

springboot怎样监听端口要求

在Spring Boot中,可使用内置的Tomcat服务器来监听端口要求。以下是一种常见的方式:

  1. 在pom.xml文件中添加Tomcat的依赖:

org.springframework.boot
spring-boot-starter-web
  1. 创建一个Spring Boot的启动类,并添加@SpringBootApplication注解:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
  1. 在启动类中定义一个Controller类,用于处理要求:
@RestController
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
}
  1. 在application.properties或application.yml文件中配置监听的端口号:
server.port=8080
  1. 运行MyApplication类的main方法,启动Spring Boot利用。

现在,你可以通过访问http://localhost:8080/hello来测试要求,并获得到"Hello, Spring Boot!"的响应结果。