- 引入依赖
<!--swagger2配置-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<!--用于改变风格-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.6</version>
</dependency>
- 编写配置类
package com.imooc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* <Description>
* Swagger2配置类
* @author
* @version 1.0
* @createDate 2020/02/23 19:56 <br>
* @see com.imooc.config <br>
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
//http://localhost:8088/swagger-ui.html 一般情况的访问
//http://localhost:8088/doc.html 因为引入的依赖,我们可以使用另外一种
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.imooc.controller")) //扫描接口所在包
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("天天吃货电商平台接口API") //文档也标题
.contact(new Contact("xxxxxx","xxxx","xxxxx"))
.description("API文档") //描述
.version("1.0")
.termsOfServiceUrl("xxxx")
.build();
}
}
- 常用注解:
(1)@ApiIgnore :用于接口类注解,可以使此类的接口隐藏。
(2)@Api (value = “注册登陆”,tags ={“用于注册登陆的相关接口”} ):用于类注解,对此类的接口进行说明。
(3)@ApiOperation (value = “判断用户名是否存在”,notes = “判断用户名是否存在”,httpMethod = “GET”):接口注解,用于接口方法上。
例如:
package com.imooc.controller;
import com.imooc.pojo.bo.UserBO;
import com.imooc.service.UserService;
import com.imooc.utils.IMOOCJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* <Description>
* 用户登陆控制
* @author
* @version 1.0
* @createDate 2020/02/23 16:42 <br>
* @see com.imooc.controller <br>
*/
@Api(value = "注册登陆",tags ="用于注册登陆的相关接口" )
@RestController
@RequestMapping("/passport")
public class PassPortController {
@Resource
private UserService userService;
/**
* 判断用户名是否存在
* @param username
* @return
*/
@ApiOperation(value = "判断用户名是否存在",notes = "判断用户名是否存在",httpMethod = "GET")
@GetMapping("/usernameIsExist")
public IMOOCJSONResult usernameIsExist(@RequestParam String username){
//判断请求参数是否为空
if (StringUtils.isBlank(username)){
return IMOOCJSONResult.errorMsg("用户名不能为空");
}
//判断用户名是否存在
if (userService.queryUsernameIsExist(username)){
return IMOOCJSONResult.errorMsg("用户名已经存在");
};
return IMOOCJSONResult.ok();
}
/**
* 注册用户
* @param userBO
* @return
*/
@ApiOperation(value = "用户注册",notes = "用户注册",httpMethod = "POST")
@PostMapping("/register")
public IMOOCJSONResult register(@RequestBody UserBO userBO){
String username = userBO.getUsername();
String password = userBO.getPassword();
String confirmPassword = userBO.getConfirmPassword();
//1. 判断用户名密码不能为空;
if (StringUtils.isBlank(username)||StringUtils.isBlank(password)||StringUtils.isBlank(confirmPassword)){
return IMOOCJSONResult.errorMsg("用户名或密码不能为空");
}
//2.判断用户名是否存在;
if (userService.queryUsernameIsExist(username)){
return IMOOCJSONResult.errorMsg("用户已经存在!");
}
//3. 判断密码长度不能小于6位
if (password.length()<6){
return IMOOCJSONResult.errorMsg("密码长度不能小于6位");
}
//4.判断两次的密码是否相同;
if (!password.equals(confirmPassword)){
return IMOOCJSONResult.errorMsg("输入的用户密码不一致,请检查重新输入!");
}
//4.实现注册;
userService.createUser(userBO);
return IMOOCJSONResult.ok();
}
}
(4)**@ApiModel**(value = "用户注册业务对象",description = "由用户传入的注册数据的封装"):用于参数说明,添加于参数实体类上。
(5)**@ApiModelProperty**(value = "用户名",name = "username",example = "imooc",required = true):用于参数实体类的属性。required = true表示此属性必填。
例如:
package com.imooc.pojo.bo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* <Description>
* 用户BO类,用于接收注册时的参数
* @author HuZuYou
* @version 1.0
* @createDate 2020/02/23 17:20 <br>
* @see com.imooc.pojo.bo <br>
*/
@ApiModel(value = "用户注册业务对象",description = "由用户传入的注册数据的封装")
@Data
public class UserBO {
/**
* 用户名
*/
@ApiModelProperty(value = "用户名",name = "username",example = "imooc",required = true)
private String username;
/**
* 密码
*/
@ApiModelProperty(value = "密码",name = "password",example = "123123",required = true)
private String password;
/**
* 确认密码
*/
@ApiModelProperty(value = "确认密码",name = "confirmPassword",example = "123123",required = true)
private String confirmPassword;
}
(6)