目录
- 1、springboot 的Demo
- 2、实例类
- 3、服务类 get请求方法
- 4、服务类的post请求方法
- 5、swaggerConfig 接口文档生成
- 配置依赖
@SpringBootApplication
@ComponentScan("com.course")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
1、springboot 的Demo
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
2、实例类
@Data
public class User {
private String userName;
private String password;
private String name;
private String age;
private String sex;
}
3、服务类 get请求方法
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.util.HashMap;
import java.util.Map;
import java.util.Objects;
@RestController
@Api(value = "/",tags = "这是我全部的get方法")
public class MyGetMethod {
@RequestMapping(value = "/getCookies",method = RequestMethod.GET)
@ApiOperation(value = "通过这个方法可以获取到Cookies",httpMethod = "GET")
public String getCookies(HttpServletResponse response){
//HttpServerletRequest 装请求信息的类
//HttpServerletResponse 装响应信息的类
Cookie cookie = new Cookie("login","true");
response.addCookie(cookie);
return "恭喜你获得cookies信息成功";
}
/**
* 要求客户端携带cookies访问
* 这是一个需要携带cookies信息才能访问的get请求
*/
@RequestMapping(value = "/get/with/cookies",method = RequestMethod.GET)
@ApiOperation(value = "要求客户端携带cookies访问",httpMethod = "GET")
public String getWithCookies(HttpServletRequest request){
Cookie[] cookies = request.getCookies();
if(Objects.isNull(cookies)){
return "你必须携带cookies信息来";
}
for(Cookie cookie : cookies){
if(cookie.getName().equals("login") &&
cookie.getValue().equals("true")){
return "这是一个需要携带cookies信息才能访问的get请求!";
}
}
return "你必须携带cookies信息来";
}
/**
* 开发一个需要携带参数才能访问的get请求。
* 第一种实现方式 url: key=value&key=value
* 我们来模拟获取商品列表
*/
@RequestMapping(value = "/get/with/param",method = RequestMethod.GET)
@ApiOperation(value = "需求携带参数才能访问的get请求方法一",httpMethod = "GET")
public Map<String,Integer> getList(@RequestParam Integer start,
@RequestParam Integer end){
Map<String,Integer> myList = new HashMap<>();
myList.put("鞋",400);
myList.put("干脆面",1);
myList.put("衬衫",300);
return myList;
}
/**
* 第二种需要携带参数访问的get请求
* url:ip:port/get/with/param/10/20
*/
@RequestMapping(value = "/get/with/param/{start}/{end}")
@ApiOperation(value = "需求携带参数才能访问的get请求的第二种实现",httpMethod = "GET")
public Map myGetList(@PathVariable Integer start,
@PathVariable Integer end){
Map<String,Integer> myList = new HashMap<>();
myList.put("鞋",400);
myList.put("干脆面",1);
myList.put("衬衫",300);
return myList;
}
}
4、服务类的post请求方法
package com.course.server;
import com.course.bean.User;
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;
@RestController
@Api(value = "/",tags = "这是我全部的post请求")
@RequestMapping("/v1")
public class MyPostMethod {
//这个变量是用来装我们cookies信息的
private static Cookie cookie;
//用户登陆成功获取到cookies,然后再访问其他接口获取到列表
@RequestMapping(value = "/login",method = RequestMethod.POST)
@ApiOperation(value = "登陆接口,成功后获取cookies信息",httpMethod = "POST")
public String login(HttpServletResponse response,
@RequestParam(value = "userName",required = true) String userName,
@RequestParam(value = "password",required = true) String password){
if(userName.equals("zhangsan") && password.equals("123456")){
cookie = new Cookie("login","true");
response.addCookie(cookie);
return "恭喜你登陆成功了!";
}
return "用户名或者是密码错误!";
}
@RequestMapping(value = "/getUserList",method = RequestMethod.POST)
@ApiOperation(value = "获取用户列表",httpMethod = "POST")
public String getUserList(HttpServletRequest request,
@RequestBody User u){
User user;
//获取cookies
Cookie[] cookies = request.getCookies();
//验证cookies是否合法
for(Cookie c : cookies){
if(c.getName().equals("login")
&& c.getValue().equals("true")
&& u.getUserName().equals("zhangsan")
&& u.getPassword().equals("123456")
){
user = new User();
user.setName("lisi");
user.setAge("18");
user.setSex("man");
return user.toString();
}
}
return "参数不合法";
}
}
5、swaggerConfig 接口文档生成
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.pathMapping("/")
.select()
.paths(PathSelectors.regex("/.*"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("我的接口文档")
.contact(new Contact("dazhou","","42197393@qq.com"))
.description("这是我的swaggerui生成的接口文档")
.version("1.0.0.0")
.build();
}
}
访问地址:http://localhost:8888/swagger-ui.html
配置依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>