SpringBoot接口开发实战
1.pom中添加SpringBoot
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
</parent>
2.配置swagger-ui、lombok所需要的依赖
<properties>
<springfox-swagger2-version>2.9.2</springfox-swagger2-version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger2-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger2-version}</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>//方便使用get、set方法
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
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<String, Integer> needParams01(@RequestParam(value = "start", required = true) Integer start, @RequestParam(value = "end", required = true) Integer end) {
Map<String, Integer> 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<String, Integer> needParams02(@PathVariable Integer start, @PathVariable Integer end) {
Map<String, Integer> 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