引入web依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
编写controller
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/index")
public String index() {
return "hello world!";
}
}
其中@RestController相当于@Controller并给每一个接口添加@ResponseBody注解
application.properties配置
#端口号,不填默认8080
server.port=9999
#项目名称,不填默认空
server.servlet.context-path=/demo
右键启动DemoApplication.java,地址栏输入http://127.0.0.1:9999/demo/hello/index查看结果