Linux 拨号vps windows公众号手机端

springboot怎么监听端口请求

lewis 7年前 (2018-02-03) 阅读数 9 #程序编程
文章标签 springboot

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

  1. 在pom.xml文件中添加Tomcat的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
  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!"的响应结果。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门