一、springboot官方demo开发
- 依赖包和父:pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.14</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.14</version>
</dependency>
</dependencies>
- 新建 SampleController.java
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home(){
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(SampleController.class,args);
}
}
- 运行结果
说明:内置了web服务器
二、使用SpringBoot开发get方法接口
返回cookie信息的get接口开发
- 新建Application.java 入口
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.course.server")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
- com.course.server 新建MyGetMethod.java
@RestController
public class MyGetMethod{
@RequestMapping(value="/getCookies",method=RequestMethod.GET)
public String getCookies(){
return "恭喜你获得cookies信息成功";
}
}
- Resource下新建文件:application.properties
server.port=${por