Spring boot 整合Swagger的两种方式

来源:互联网 发布:远程桌面软件 win7 编辑:程序博客网 时间:2024/05/23 11:18

增加请求不被拦截

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



方式1:

<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>${swagger.version}</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>${swagger.version}</version></dependency>

配置POM.xml


<!--swagger--><swagger.version>2.2.2</swagger.version>

开启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@EnableSwagger2public 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:

<dependency><groupId>com.spring4all</groupId><artifactId>spring-boot-starter-swagger</artifactId><version>${spring-boot-starter-swagger.version}</version></dependency>

添加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;




页面中显示注解

原创粉丝点击