A, springboot integration swagger2
1.1 introduced dependence
<!-- swagger2 接口文档 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<!-- swagger2的页面 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
</dependency>
<!--书写配置文件提示的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
1.2 Creating a configuration property class
Use the lombok. In use, the mounting plug and imported lombok lombok package
package com.zxm.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "swagger2")
public class SwaggerProperties {
/**
* 要对那个包的Controller 做文档的生成
*/
private String basePackage;
/**
* 作者的名称
*/
private String name ;
/**
* 主页
*/
private String url;
/**
* 邮箱
*/
private String email ;
/**
* 标题
*/
private String title ;
/**
* 描述
*/
private String description ;
/**
* 服务的团队
*/
private String termsOfServiceUrl;
/**
* 授权信息
*/
private String license ;
/**
* 授权的url
*/
private String licenseUrl ;
/**
* swagger2的版本
*/
private String version;
}
1.3 Creating automatic configuration class
package com.zxm.config;
import io.swagger.models.Swagger;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
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;
@Configuration
@EnableSwagger2 // 开启swagger的功能
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerAutoConfiguration {
private SwaggerProperties swaggerProperties;
public SwaggerAutoConfiguration(SwaggerProperties swaggerProperties){
this.swaggerProperties = swaggerProperties;
}
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.select().apis(RequestHandlerSelectors.basePackage(this.swaggerProperties.getBasePackage())).build();
}
private ApiInfo getApiInfo() {
Contact contact = new Contact(this.swaggerProperties.getName(), this.swaggerProperties.getUrl(), this.swaggerProperties.getEmail());
return new ApiInfoBuilder().
contact(contact).
title(this.swaggerProperties.getTitle()).
description(this.swaggerProperties.getDescription()).
termsOfServiceUrl(this.swaggerProperties.getTermsOfServiceUrl()).
license(this.swaggerProperties.getLicense()).
licenseUrl(this.swaggerProperties.getLicenseUrl()).
version(this.swaggerProperties.getVersion()).
build();
}
}
1.4 release swagger2 in the shiro
Failure to use shiro can not be set
1.5 Configuring Personal Information
1.5 Access page
http://localhost:8085/doc.html
Two, mybatis-plus tab configuration
@Configuration
public class MybatisConfig {
/**
* 分页的拦截器
* @return
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setDialectType("mysql") ;
return paginationInterceptor ;
}
}
Three, Restful interface design
3.1 paging query
A minimum of two request parameters: current page
SysUser —> sys/user/page
例:http://localhost:8001/proxyApi/sys/user/page?t=1582615122917¤t=1&size=10
element | Manifestations |
---|---|
Request method | get |
Request parameter | Integer current: The current page Integer page: the number of pieces per page |
return value | IPage |
Url request | $ {Base_url} / table name / page |
3.2 Echo single data
element | Manifestations |
---|---|
Request method | get |
Request parameter | Long id parameter to the url id |
return value | SysUser |
Url request | $ {Base_url} / table name / info / {id} |
Eg: echo a management
/sys/user/info/1
3.3 delete data
element | Manifestations |
---|---|
Request method | delete |
Request parameter | Long id parameter to the url id |
return value | Void |
Url request | $ {Base_url} / table name / {id} |
3.4 Delete a set of (bulk deletion)
element | Manifestations |
---|---|
Request method | delete |
Request parameter | List ids |
return value | Void |
Url request | $ {Base_url} / table name |
Request parameters: Json Data: [id1, id2 ...]
3.5 a new data
element | Manifestations |
---|---|
Request method | post |
Request parameter | SysUser sysUser |
return value | Void |
Url request | $ {Base_url} / table name |
Request parameters: a new data of the object json
3.6 Full query data
element | Manifestations |
---|---|
Request method | get |
Request parameter | no |
return value | List |
Url request | Name $ {base_url} / table / list |
Fourth, the example of using restful style
package com.sxt.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.entity.SysUser;
import com.sxt.service.SysUserService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/sys/user")
public class UserController {
@Autowired
private SysUserService sysUserService ;
/**
* 分页查询
*/
@GetMapping("/page")
@ApiOperation("分页的查询")
public ResponseEntity<IPage<SysUser>> findByPage(
@RequestParam(defaultValue = "1") Integer current,
@RequestParam(defaultValue = "10") Integer size
){
IPage<SysUser> page = sysUserService.page(new Page<>(current, size));
return ResponseEntity.ok(page) ;
}
/**
* 删除单个值
* @param id
* @return
*/
@DeleteMapping("/{id}")
@ApiOperation("删除单个值")
public ResponseEntity<Void> delete(@PathVariable("id")Long id){
sysUserService.removeById(id);
return ResponseEntity.ok().build() ;
}
/**
* 删除多个值
*/
@DeleteMapping
@ApiOperation("删除多个值")
public ResponseEntity<Void> delete(@RequestBody List<Long> ids){
sysUserService.removeByIds(ids);
return ResponseEntity.ok().build() ;
}
/**
* 回显一个值
*/
@GetMapping("/info/{id}")
@ApiOperation("回显一个值")
public ResponseEntity<SysUser> findById(@PathVariable("id") Long id){
SysUser sysUser = sysUserService.getById(id);
return ResponseEntity.ok(sysUser) ;
}
/**
* 新增一个值
* @param sysUser
* @return
*/
@PostMapping
@ApiOperation("新增一个值")
public ResponseEntity<Void> add(@RequestBody SysUser sysUser){
sysUserService.save(sysUser);
return ResponseEntity.ok().build() ;
}
/**
* 修改一个值
* @param sysUser
* @return
*/
@PutMapping
@ApiOperation("修改一个值")
public ResponseEntity<Void> update(@RequestBody SysUser sysUser){
sysUserService.updateById(sysUser);
return ResponseEntity.ok().build() ;
}
}