增加README.md文章导航
This commit is contained in:
2
spring-boot/spring-boot-swagger2/README.md
Normal file
2
spring-boot/spring-boot-swagger2/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
@Profile({"dev","test"})
|
||||
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
|
61
spring-boot/spring-boot-swagger2/pom.xml
Normal file
61
spring-boot/spring-boot-swagger2/pom.xml
Normal file
@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.heibaiying.springboot</groupId>
|
||||
<artifactId>spring-boot-swagger2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-boot-swagger2</name>
|
||||
<description>swagger2 project for Spring Boot</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--swagger2-->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>2.9.2</version>
|
||||
</dependency>
|
||||
<!--swagger-ui -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>2.9.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,14 @@
|
||||
package com.heibaiying.springboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootSwagger2Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootSwagger2Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
package com.heibaiying.springboot.bean;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@ToString
|
||||
public class Product {
|
||||
|
||||
private long id;
|
||||
private String name;
|
||||
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
|
||||
private Date date;
|
||||
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package com.heibaiying.springboot.config;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
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;
|
||||
|
||||
import static com.google.common.base.Predicates.not;
|
||||
import static springfox.documentation.builders.PathSelectors.regex;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : Swagger 配置类
|
||||
*/
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
public class SwaggerConfig {
|
||||
|
||||
@Value("${swagger.enable}")
|
||||
private boolean swaggerEnable;
|
||||
|
||||
/***
|
||||
* 配置swagger
|
||||
* 开发和测试环境下可以开启swagger辅助进行调试,而生产环境下可以关闭或者进行相应的权限控制,防止接口信息泄露
|
||||
*/
|
||||
@Bean
|
||||
public Docket createRestApi() {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.enable(swaggerEnable)
|
||||
.apiInfo(apiInfo())
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage("com.heibaiying.springboot.controller"))
|
||||
.paths(PathSelectors.any())
|
||||
.paths(doFilteringRules())
|
||||
.build();
|
||||
}
|
||||
|
||||
/***
|
||||
* 接口文档的描述信息
|
||||
*/
|
||||
private ApiInfo apiInfo() {
|
||||
return new ApiInfoBuilder()
|
||||
.title("spring boot swagger2 用例")
|
||||
.description("描述")
|
||||
.licenseUrl("https://mit-license.org/")
|
||||
.version("1.0")
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 可以使用正则定义url过滤规则
|
||||
*/
|
||||
private Predicate<String> doFilteringRules() {
|
||||
return not(
|
||||
regex("/ignore/*")
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package com.heibaiying.springboot.controller;
|
||||
|
||||
import com.heibaiying.springboot.bean.Product;
|
||||
|
||||
import io.swagger.annotations.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import springfox.documentation.annotations.ApiIgnore;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 产品查询接口
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Api(value = "产品接口", description = "产品信息接口")
|
||||
@RestController
|
||||
public class ProductController {
|
||||
|
||||
/***
|
||||
* 一个标准的swagger注解
|
||||
*/
|
||||
@ApiOperation(notes = "查询所有产品", value = "产品查询接口")
|
||||
@ApiImplicitParams(
|
||||
@ApiImplicitParam(name = "id", value = "产品编号", paramType = "path", defaultValue = "1")
|
||||
)
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(code = 200, message = "请求成功"),
|
||||
@ApiResponse(code = 400, message = "无效的请求"),
|
||||
@ApiResponse(code = 401, message = "未经过授权认证"),
|
||||
@ApiResponse(code = 403, message = "已经过授权认证,但是没有该资源对应的访问权限"),
|
||||
@ApiResponse(code = 404, message = "服务器找不到给定的资源,商品不存在"),
|
||||
@ApiResponse(code = 500, message = "服务器错误")
|
||||
})
|
||||
@GetMapping(value = "/product/{id}", produces = "application/json")
|
||||
public ResponseEntity<Product> getProduct(@PathVariable long id) {
|
||||
Product product = new Product(id, "product" + id, new Date());
|
||||
return ResponseEntity.ok(product);
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 如果用实体类接收参数,则用实体类对应的属性名称指定参数
|
||||
*/
|
||||
@ApiOperation(notes = "保存产品", value = "产品保存接口")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "id", value = "产品编号", paramType = "body", defaultValue = "1"),
|
||||
@ApiImplicitParam(name = "name", value = "产品名称", paramType = "body"),
|
||||
@ApiImplicitParam(name = "date", value = "产品生产日期", paramType = "body")
|
||||
}
|
||||
)
|
||||
@PostMapping(value = "/product")
|
||||
public ResponseEntity<Void> saveProduct(@RequestBody Product product) {
|
||||
System.out.println(product);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 在配置类中指明了该接口不被扫描到,可以在配置类中使用正则指定某一类符合规则的接口不被扫描到
|
||||
*/
|
||||
@ApiOperation(notes = "该接口会被忽略", value = "产品保存接口")
|
||||
@PostMapping(value = "/ignore")
|
||||
public ResponseEntity<Product> ignore() {
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 不加上任何swagger相关的注解也会被扫描到 如果不希望被扫描到,需要用 @ApiIgnore 修饰
|
||||
*/
|
||||
@PostMapping(value = "/normal")
|
||||
public ResponseEntity<Void> normal() {
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@ApiIgnore
|
||||
@PostMapping(value = "/apiIgnore")
|
||||
public ResponseEntity<Void> apiIgnore() {
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
swagger.enable = true
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.springboot;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringBootSwagger2ApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user