SpringBoot接口开发实战
1.pom中添加SpringBoot
org.springframework.boot spring-boot-starter-parent 2.5.6
2.配置swagger-ui、lombok所需要的依赖
2.9.2 11 11 io.springfox springfox-swagger2${springfox-swagger2-version} io.springfox springfox-swagger-ui${springfox-swagger2-version} com.github.xiaoymin swagger-bootstrap-ui1.9.6 //方便使用get、set方法 org.projectlombok lombok
3.创建application类
package Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication//目的是开启自动配置
@ComponentScan(value = {"getMethod","Config","postMethod"})
//设置扫描类
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
4.resources下新建application.properties,填写配置
server.port=${port:8888}
5.编写被扫描类及方法
a:get方法类
package getMethod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.cookieStore;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@RestController//表明是被扫描的类
@Api(value = "/")//设置访问该类的路径
public class getcookies {
@RequestMapping(value = {"/getcookies"})//设置方法路径
@ApiOperation(value = "獲取cookies get請求", httpMethod = "GET")//设置swagger接口文档中方法的描述,请求方式
public String getcookies(HttpServletResponse response) {
cookie cookie = new cookie("login", "true");
response.addcookie(cookie);
return "恭喜獲得cookies!";
}
@RequestMapping(value = {"/needcookies"})
@ApiOperation(value = "傳送cookies get請求", httpMethod = "GET")
public String needcookies(HttpServletRequest request) {
cookie[] cookies = request.getcookies();
if (Objects.isNull(cookies)) {
return "缺少cookies信息!";
}
for (cookie cookie1 : cookies) {
if ((cookie1.getName().equals("login")) && cookie1.getValue().equals("true")) {
return "携帶正確登錄信息,恭喜成功!";
}
}
return "cookie信息錯誤!";
}
//第一種需要蠶食URl方式 url+?key=value&
@RequestMapping(value = {"/needParams01"})
@ApiOperation(value = "傳遞參數 url+?key=value&get請求", httpMethod = "GET")
public Map needParams01(@RequestParam(value = "start", required = true) Integer start, @RequestParam(value = "end", required = true) Integer end) {
Map map = new HashMap<>();
map.put("可樂", 3);
map.put("薯條", 7);
map.put("漢堡", 10);
return map;
}
//第二種需要蠶食URl方式 url/value/value2
@RequestMapping(value = {"/needParams02/{start}/{end}"})
@ApiOperation(value = "傳遞參數 url/value/value2get請求", httpMethod = "GET")
public Map needParams02(@PathVariable Integer start, @PathVariable Integer end) {
Map map = new HashMap<>();
map.put("可樂", 3);
map.put("薯條", 7);
map.put("漢堡", 10);
return map;
}
}
b:post方法类
package postMethod;
import Unity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import javax.servlet.http.cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;
@RestController
@Api(value = "/")
public class returncookies {
@RequestMapping("/postReturncookies")
@ApiOperation(value = "登錄接口,回返token", httpMethod = "POST")
public String postReturncookies(HttpServletResponse response, @RequestParam(value = "UserName", required = true) String UserName, @RequestParam(value = "PassWord", required = true) String PassWord) {
if (UserName.equals("lh") && PassWord.equals("123456")) {
cookie cookie = new cookie("login", "true");
response.addcookie(cookie);
return "登錄成功";
}
return "登錄失敗,賬號或者密碼錯誤!";
}
@RequestMapping("/postReturnUserList")
@ApiOperation(value = "登錄接口,回返token", httpMethod = "POST")
public String postReturnUserList(HttpServletRequest request,HttpServletResponse response, @RequestBody User u) {
cookie[] cookies = request.getcookies();
if (Objects.isNull(cookies)) {
return "缺少cookies!";
}
for (cookie cookie : cookies) {
if (cookie.getName().equals("login") && cookie.getValue().equals("true")) {
if (u.getUserName().equals("lh")
&& u.getPassWord().equals("123456")) {
User c = new User();
c.setAge("18");
c.setSex("男");
c.setUserName("lh");
c.setPassWord("123456");
c.setName("六號");
return c.toString();
}
return "登錄失敗,賬號或者密碼錯誤!";
}
}
return "登錄失敗,cookies不正確!";
}
}
6.方法中用到的实体类
package Unity;
import lombok.Data;
@Data
public class User {
private String name;
private String age;
private String sex;
private String UserName;
private String PassWord;
}
7.运行Application类、浏览器打开localhost:8888/doc.html



