1.引入依赖
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>1.5.3.RELEASE</version> 5 </parent> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-web</artifactId> 9 </dependency>
2.springboot启动类
1 import org.springframework.boot.SpringApplication; 2 import org.springframework.boot.autoconfigure.SpringBootApplication; 3 import org.springframework.context.annotation.ComponentScan; 4 //启动类 5 @SpringBootApplication 6 @ComponentScan("com.test") 7 public class Application { 8 public static void main(String[] args) { 9 SpringApplication.run(Application.class, args); 10 } 11 }
3.返回cookies信息的get接口开发
1 //@RestController加在类上面的注解,使得类里面的每个方法都将json/xml返回数据加返回到前台页面中 2 @RestController 3 public class MyGetMethod { 4 //@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写 5 @GetMapping("/getCookies") 6 public String getCookies(HttpServletResponse response) { 7 Cookie cookie = new Cookie("login","true"); 8 response.addCookie(cookie); 9 return "恭喜你获取cookies成功"; 10 } 11 }
4.要求携带cookies信息访问的get接口开发
1 //要求客户端携带cookies才能访问 2 @GetMapping("/getWithCookies") 3 public String getWithCookies(HttpServletRequest request) { 4 Cookie[] cookies = request.getCookies(); 5 if (cookies == null) { 6 return "必须携带cookie访问"; 7 } 8 for (Cookie c:cookies) { 9 if (c.getName().equals("login")&&c.getValue().equals("true")) { 10 return "恭喜你登录成功"; 11 } 12 } 13 return "必须携带cookie访问"; 14 }
5.携带参数的get请求两种开发方式
1 /** 2 * 开发一个需要携带参数才能访问的get请求。 3 * 第一种实现方式 url: key=value&key=value 4 * 我们来模拟获取商品列表 5 * 解决参数不一致可以用@RequestParam("start")Integer s 6 */ 7 @GetMapping("/getWithParam") 8 public Map<String, Double> getWithParam(Integer start,Integer end){ 9 Map<String , Double> myMap = new HashMap<String, Double>(); 10 myMap.put("苹果", 5.5); 11 myMap.put("香蕉", 2.8); 12 return myMap; 13 } 14 /** 15 * 第二种需要携带参数访问的get请求 16 * url:ip:port/get/with/param/10/20 17 */ 18 @GetMapping("/getWithParam/{start}/{end}") 19 public Map<String, Double> getWithParam2(@PathVariable Integer start,@PathVariable Integer end){ 20 Map<String , Double> myMap = new HashMap<String, Double>(); 21 myMap.put("苹果", 5.5); 22 myMap.put("香蕉", 2.8); 23 return myMap; 24 } 25
6.SpringBoot集成SwaggerUI
6.1引入依赖
1 <properties> 2 <swagger.version>2.6.1</swagger.version> 3 </properties> 4 <dependency> 5 <groupId>io.springfox</groupId> 6 <artifactId>springfox-swagger2</artifactId> 7 <version>${swagger.version}</version> 8 </dependency> 9 <dependency> 10 <groupId>io.springfox</groupId> 11 <artifactId>springfox-swagger-ui</artifactId> 12 <version>${swagger.version}</version> 13 </dependency>
6.2新建SwaggerConfig java配置类
1 package com.test.config; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.Configuration; 5 import springfox.documentation.builders.ApiInfoBuilder; 6 import springfox.documentation.builders.PathSelectors; 7 import springfox.documentation.service.ApiInfo; 8 import springfox.documentation.spi.DocumentationType; 9 import springfox.documentation.spring.web.plugins.Docket; 10 import springfox.documentation.swagger2.annotations.EnableSwagger2; 11 12 @Configuration 13 @EnableSwagger2 14 public class SwaggerConfig { 15 @Bean 16 public Docket api(){ 17 return new Docket(DocumentationType.SWAGGER_2) 18 .apiInfo(apiInfo()) 19 .pathMapping("/") 20 .select() 21 .paths(PathSelectors.regex("/.*")) 22 .build(); 23 } 24 25 private ApiInfo apiInfo() { 26 return new ApiInfoBuilder().title("我的接口文档") 27 .description("这是我的swaggerui生成的接口文档") 28 .version("1.0.0.0") 29 .build(); 30 } 31 }
6.3给方法增加注解
1 package com.test.server; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import javax.servlet.http.Cookie; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 import org.springframework.web.bind.annotation.GetMapping; 11 import org.springframework.web.bind.annotation.PathVariable; 12 import org.springframework.web.bind.annotation.RestController; 13 14 import io.swagger.annotations.Api; 15 import io.swagger.annotations.ApiOperation; 16 17 //@RestController加在类上面的注解,使得类里面的每个方法都将json/xml返回数据加返回到前台页面中 18 @RestController 19 @Api(value = "/",description = "这是我全部的get方法") 20 public class MyGetMethod { 21 //@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写 22 @GetMapping("/getCookies") 23 @ApiOperation(value = "通过这个方法可以获取到Cookies") 24 public String getCookies(HttpServletResponse response) { 25 Cookie cookie = new Cookie("login","true"); 26 response.addCookie(cookie); 27 return "恭喜你获取cookies成功"; 28 } 29 //要求客户端携带cookies才能访问 30 @GetMapping("/getWithCookies") 31 @ApiOperation(value = "要求客户端携带cookies访问") 32 public String getWithCookies(HttpServletRequest request) { 33 Cookie[] cookies = request.getCookies(); 34 if (cookies == null) { 35 return "必须携带cookie访问"; 36 } 37 for (Cookie c:cookies) { 38 if (c.getName().equals("login")&&c.getValue().equals("true")) { 39 return "恭喜你登录成功"; 40 } 41 } 42 return "必须携带cookie访问"; 43 } 44 /** 45 * 开发一个需要携带参数才能访问的get请求。 46 * 第一种实现方式 url: key=value&key=value 47 * 我们来模拟获取商品列表 48 * 解决参数不一致可以用@RequestParam("start")Integer s 49 */ 50 @GetMapping("/getWithParam") 51 @ApiOperation(value = "需求携带参数才能访问的get请求方法一") 52 public Map<String, Double> getWithParam(Integer start,Integer end){ 53 Map<String , Double> myMap = new HashMap<String, Double>(); 54 myMap.put("苹果", 5.5); 55 myMap.put("香蕉", 2.8); 56 return myMap; 57 } 58 /** 59 * 第二种需要携带参数访问的get请求 60 * url:ip:port/get/with/param/10/20 61 */ 62 @GetMapping("/getWithParam/{start}/{end}") 63 @ApiOperation(value = "需求携带参数才能访问的get请求方法二") 64 public Map<String, Double> getWithParam2(@PathVariable Integer start,@PathVariable Integer end){ 65 Map<String , Double> myMap = new HashMap<String, Double>(); 66 myMap.put("苹果", 5.5); 67 myMap.put("香蕉", 2.8); 68 return myMap; 69 } 70 }
7.返回cookies信息的post接口开发
1 @RestController 2 @Api(value = "/",description = "这是我全部的post方法") 3 public class MyPostMethod { 4 //这个变量是用来装我们cookies信息的 5 private static Cookie cookie; 6 @PostMapping("/login") 7 @ApiOperation(value = "登录成功,获取cookies信息") 8 public String login(@RequestParam(value = "username",required = true)String username,@RequestParam(value = "password",required = true)String password,HttpServletResponse response) { 9 if (username.equals("admin")&&password.equals("admin123")) { 10 cookie = new Cookie("login", "true"); 11 response.addCookie(cookie); 12 return "登录成功"; 13 } 14 return "用户名或者是密码错误!"; 15 } 16 17 }
8.Cookies验证和返回用户列表的post接口开发
8.1安装lombok插件
8.2引入lombok依赖,lombok通过添加注解的方式,不需要为类编写getter或eques方法,同时可以自动化日志变量
1 <dependency> 2 <groupId>org.projectlombok</groupId> 3 <artifactId>lombok</artifactId> 4 <version>1.16.14</version> 5 </dependency
8.3新建实体类
1 package com.test.pojo; 2 3 import lombok.Data; 4 5 @Data 6 public class User { 7 private String username; 8 private String password; 9 private String name; 10 private int age; 11 }
8.4Cookies验证和返回用户列表的post接口开发代码
1 @PostMapping("/getUsersList") 2 @ApiOperation(value = "获取用户列表") 3 public String getUsersList(HttpServletRequest request,@RequestBody User u){ 4 User user; 5 Cookie[] cookies = request.getCookies(); 6 if (cookies ==null) { 7 return "参数不合法"; 8 } 9 for(Cookie c:cookies) { 10 if (c.getName().equals("login")&&c.getValue().equals("true") 11 &&u.getUsername().equals("admin") 12 &&u.getPassword().equals("admin123")) { 13 user = new User(); 14 user.setName("李四"); 15 user.setAge(24); 16 user.setUsername("lisi"); 17 user.setPassword("admin123"); 18 return user.toString(); 19 } 20 } 21 return "参数不合法"; 22 }