1、当我们还在使用doc 处理我们项目中遇到的前段分离时,后端定义的接口文档,swagger已经开始流行了,接下来我们就开始用springboot整合
2、文件位置:
3、将在pom 加入相关座标:
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
4、在新建的实体bean 引入相关组件
package com.carton.bean;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* @author Liu Runyong
* @date 2019/12/23
* @description 用户信息
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "user", description = "用户信息")
public class User implements Serializable {
@ApiModelProperty(value = "用户主键(新增#,更新*)")
private Integer userId;
@ApiModelProperty(value = "暱称")
private String userName;
@ApiModelProperty(value = "账号")
private String account;
@ApiModelProperty(value = "账号")
private String password;
@ApiModelProperty(value = "电话")
private String phone;
@ApiModelProperty(value = "通勤状态")
private String status;
@ApiModelProperty(value = "是否删除")
private Integer isDelete;
@ApiModelProperty(value = "性别")
private char sex;
@ApiModelProperty(value = "年龄")
private Integer age;
}
5、dao和service 我就不写了 还是按照我们正常的开发去书写就行
6、controller
package com.carton.controller;
import com.carton.bean.User;
import com.carton.common.utils.exception.ServerResponse;
import com.carton.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.Size;
import java.util.List;
/**
* @author Liu Runyong
* @date 2019/12/23
*/
@Api(tags = "用户信息", description = "userController")
@RestController
@RequestMapping("/user/")
public class UserController {
@Autowired
private UserService userService;
@ApiOperation("新增用户信息")
@PostMapping("insert")
public ServerResponse insertSelective(@ModelAttribute User user) {
userService.insertSelective(user);
return ServerResponse.createBySuccessMessage("success");
}
@ApiOperation("验证用户是否存在")
@PostMapping("repeat")
public ServerResponse repeatUser(String phone) {
if (userService.repeatUser(phone)) {
return ServerResponse.createBySuccessMessage("success");
} else {
return ServerResponse.createByErrorMessage("error");
}
}
@ApiOperation("查询用户信息")
@GetMapping("select")
public ServerResponse selectAllByConditions() {
return ServerResponse.createBySuccess(userService.selectAllByConditions());
}
@ApiOperation("编辑用户信息")
@PutMapping("update")
public ServerResponse updateSelectiveByPrimaryKey(@ModelAttribute User user) {
userService.updateSelectiveByPrimaryKey(user);
return ServerResponse.createBySuccessMessage("success");
}
@ApiOperation("删除用户")
@DeleteMapping("delete")
public ServerResponse deleteByPrimaryKey(@Size(min = 1, message = "请选择删除对象") List<Integer> ids) {
userService.deleteByPrimaryKey(ids);
return ServerResponse.createBySuccessMessage("success");
}
}
7、将swagger加入到springboot启动项目中
package com.carton;
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;
/**
* Swagger 配置
*
* @author Liurunyong
* @date 2019/12/23
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
/**
* swagger2的配置文件
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.carton.controller"))
.paths(PathSelectors.any())
.build();
}
/**
* 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Carton API接口文档")
.contact(new Contact("Liurunyong", "http://www.lrycq.cn", "[email protected]"))
.version("1.0")
.description("API文档")
.build();
}
}
然后就大功告成了,是不是 还可以