使用swagger2的作用
第一步:配置swagger类
package com.lpy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.Contact;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* 通过java代码配置
* @author Richard
*
*@Configuration表示是配置文件
*@EnableSwagger2开启这个配置
*
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
/**
* @Description:swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等,注:让我们的swagger2扫描我们controller
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.lpy.controller"))
.paths(PathSelectors.any()).build();
}
/**
* @Description: 构建 api文档的信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 设置页面标题
.title("使用swagger2构建短视频后端api接口文档")
// 设置联系人
.contact(new Contact("Richard", "http://www.lpy.com", "[email protected]"))
// 描述
.description("欢迎访问短视频接口文档,这里是描述信息")
// 定义版本号
.version("1.0").build();
}
}
第二步:给接口写注解
package com.lpy.controller;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.lpy.pojo.Users;
import com.lpy.service.UserService;
import com.lpy.utils.LpyJSONResult;
import com.lpy.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
* @RestController表示已json格式进行传递
* @RequestBody表示将json对象转换成对象
* @author Richard
*/
@RestController
@Api(value="用户注册登陆的接口",tags= {"注册和登陆的controller"})
public class RegistLoginController {
//注入service
@Autowired
private UserService userService;
@ApiOperation(value="用户注册",notes="用户注册的接口")
@PostMapping("/regist")
public LpyJSONResult regist(@RequestBody Users user) throws Exception {
//1、判断用户名和密码必须不为空
if(StringUtils.isBlank(user.getUsername())||StringUtils.isBlank(user.getPassword())) {
return LpyJSONResult.errorMsg("用户名和密码不能为空");
}
//2、判断用户名是否存在
boolean usernameIsExist=userService.queryUsernameIsExist(user.getUsername());
//3、保存用户,注册信息
if(!usernameIsExist) {
user.setNickname(user.getUsername());//昵称
user.setPassword(MD5Utils.getMD5Str(user.getPassword()));//密码
user.setFansCounts(0);//粉丝数
user.setReceiveLikeCounts(0);//喜欢的数
user.setFollowCounts(0);//追随数
userService.saveUser(user);
}else {
return LpyJSONResult.errorMsg("用户名已经存在,请换一个再试");
}
return LpyJSONResult.ok();
}
}
第三步:给实体类写注解
package com.lpy.pojo;
import javax.persistence.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(value="用户对象",description="这是用户对象")
public class Users {
@ApiModelProperty(hidden=true)
@Id
private String id;
@ApiModelProperty(value="用户名",name="username",example="lpyuser",required=true)
private String username;
@ApiModelProperty(value="密码",name="password",example="123456",required=true)
private String password;
@ApiModelProperty(hidden=true)
@Column(name = "face_image")
private String faceImage;
private String nickname;
@ApiModelProperty(hidden=true)
@Column(name = "fans_counts")
private Integer fansCounts;
@ApiModelProperty(hidden=true)
@Column(name = "follow_counts")
private Integer followCounts;
@ApiModelProperty(hidden=true)
@Column(name = "receive_like_counts")
private Integer receiveLikeCounts;
/**
* @return id
*/
public String getId() {
return id;
}
/**
* @param id
*/
public void setId(String id) {
this.id = id;
}
/**
* @return username
*/
public String getUsername() {
return username;
}
/**
* @param username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return password
*/
public String getPassword() {
return password;
}
/**
* @param password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return face_image
*/
public String getFaceImage() {
return faceImage;
}
/**
* @param faceImage
*/
public void setFaceImage(String faceImage) {
this.faceImage = faceImage;
}
/**
* @return nickname
*/
public String getNickname() {
return nickname;
}
/**
* @param nickname
*/
public void setNickname(String nickname) {
this.nickname = nickname;
}
/**
* @return fans_counts
*/
public Integer getFansCounts() {
return fansCounts;
}
/**
* @param fansCounts
*/
public void setFansCounts(Integer fansCounts) {
this.fansCounts = fansCounts;
}
/**
* @return follow_counts
*/
public Integer getFollowCounts() {
return followCounts;
}
/**
* @param followCounts
*/
public void setFollowCounts(Integer followCounts) {
this.followCounts = followCounts;
}
/**
* @return receive_like_counts
*/
public Integer getReceiveLikeCounts() {
return receiveLikeCounts;
}
/**
* @param receiveLikeCounts
*/
public void setReceiveLikeCounts(Integer receiveLikeCounts) {
this.receiveLikeCounts = receiveLikeCounts;
}
}
访问地址:http://localhost:8082/swagger-ui.html