Spring整合Swagger,Java自動產生Api文檔

來源:互聯網
上載者:User

標籤:bubuko   hand   index   expected   rest   掃描   exp   control   作者   

博主很懶...

Swagger官網:http://swagger.io

GitHub地址:https://github.com/swagger-api

官方註解文檔:http://docs.swagger.io/swagger-core/apidocs/index.html

Swagger-UI地址:https://github.com/swagger-api/swagger-ui

swagger最終

 

好,開始說Spring怎麼配置Swagger了1.pom.xml引入需要的jar包
<!-- 構建Restful API 我這版本是2.6.1 --><dependency>    <groupId>io.springfox</groupId>    <artifactId>springfox-swagger2</artifactId>    <version>${swagger2.version}</version></dependency><dependency>    <groupId>io.springfox</groupId>    <artifactId>springfox-swagger-ui</artifactId>    <version>${swagger2.version}</version></dependency>
2.掃描你的Swagger2config.java所在的包
<!-- url是你需要掃描swagger的包路徑 --><context:component-scan  base-package="com.nickwilde.microtrain.common.config.swagger"/>
3.Swagger2config.java[類名自訂]
package com.nickwilde.microtrain.common.config.swagger2;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;/** * @author NickWilde * @version 2018/1/18 10:23 * @description: Swagger2Config * TODO 用一句話描述 */@Configuration@EnableSwagger2public class Swagger2Config{    @Bean    public Docket api() {        return new Docket(DocumentationType.SWAGGER_2).forCodeGeneration(true)                .select()                .apis(RequestHandlerSelectors.any())                //過濾產生連結                .paths(PathSelectors.any())                .build()                .apiInfo(apiInfo());    }    //api介面作者相關資訊    private ApiInfo apiInfo() {        Contact contact = new Contact("NickWilde", "", "[email protected]");        ApiInfo apiInfo = new ApiInfoBuilder()                .license("Apache License Version 2.0")                .title("Unexpectedly系統")                .description("介面文檔")                .contact(contact)                .version("1.0")                .build();        return apiInfo;    }}
4.給你的controller加上額外註解(不加也是有的)
@ApiOperation(value="根據User對象建立使用者", notes="根據User對象建立使用者",httpMethod = "POST") //httpMehtod如果不定義,那麼swagger會有把該介面的所有要求方法都一一列舉出來@ApiImplicitParam(name = "data", value = "data描述", required = true, dataType = "UserInfo") //參數定義..類型..之類的,這是基礎簡單,其餘的看api吧~

 

Spring整合Swagger,Java自動產生Api文檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.