Spring boot 整合Swagger的两种方式

增加请求不被拦截

,"/swagger-ui.html"
 ,"/swagger-resources/**"



方式1:

		
			io.springfox
			springfox-swagger2
			${swagger.version}
		

		
			io.springfox
			springfox-swagger-ui
			${swagger.version}
		

配置POM.xml



		2.2.2

开启swagger

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
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;

@Configuration
@EnableSwagger2
public class Swagger2Config {
    /**
     * UI页面显示信息
     */
    private final String SWAGGER2_API_BASEPACKAGE = "com.op.copyems.Controller";
    private final String SWAGGER2_API_TITLE = "ms-API";
    private final String SWAGGER2_API_DESCRIPTION = "com.op.copyems.Controller";
    private final String SWAGGER2_API_CONTACT = "0000";
    private final String SWAGGER2_API_VERSION = "1.0";
    /**
     * createRestApi
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(SWAGGER2_API_BASEPACKAGE))
                .paths(PathSelectors.any())
                .build();
    }
    /**
     * apiInfo
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(SWAGGER2_API_TITLE)
                .description(SWAGGER2_API_DESCRIPTION)
                .contact(SWAGGER2_API_CONTACT)
                .version(SWAGGER2_API_VERSION)
                .build();
    }
}



方式2:

		
			com.spring4all
			spring-boot-starter-swagger
			${spring-boot-starter-swagger.version}
		

添加yml配置

swagger:
  enabled: true
  title: oms-API
  description: API文档
  base-package:  com.op.copyems.Controller
  base-path: /**
  exclude-path: /error
  version: @project.version@


访问对应接口即可--------------------------至此已配置完毕


配置为@RequestBody  

    @RequestMapping(value="", method=RequestMethod.POST)
    public String post(@RequestBody Book book) {
        books.put(book.getId(), book);
        return "success";
    }

在实体类中添加注解

public class Book {
    @ApiModelProperty("名字")
    private String name;
    @ApiModelProperty("价格")
    private String price;
    @ApiModelProperty("关联id")
    private Long id;

Spring boot 整合Swagger的两种方式_第1张图片



页面中显示注解

你可能感兴趣的:(Spring boot 整合Swagger的两种方式)