swagger demo code

//Application 开启注解

@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

pom.xml

                <!-- Swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
View Code

SwaggerConfig.java

package com.qmorn.appapi.config;

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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qmorn.appapi.user"))
                .paths(PathSelectors.any())
                //.apis(RequestHandlerSelectors.any())
                //.paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("对外开放接口API 文档")
                //.description("HTTP对外开放接口")
                .version("1.0.0")
                //.termsOfServiceUrl("http://xxx.xxx.com")
                //.license("LICENSE")
                //.licenseUrl("http://xxx.xxx.com")
                .build();
    }
}
View Code

SwaggerDemo.java

package com.qmorn.appapi.user;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 * @author YuwenTims
 * @Date 2018-06-14
 */

@RestController
public class SwaggerDemo {
    
    @ApiOperation(value = "paramType : query", notes = "第一行说明</br>第二行")
    @RequestMapping(value = "/byQuery", method = { RequestMethod.POST })
    public String byQuery(@RequestParam Integer id) {
        return "id = " + id;
    }
    
    @ApiOperation(value = "paramType : path")
    @RequestMapping(value = "/byPath/{id}", method = { RequestMethod.POST })
    public String byPath(@PathVariable(name = "id") Long id) {
        return "id = " + id;
    }
    
    @ApiOperation(value = "paramType : header")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "id", value = "参数", required = true) })
    @RequestMapping(value = "/byHeader", method = { RequestMethod.POST })
    public String byHeader(@RequestHeader ("id") String id) {
        return "user id = " + id;
    }
    
    @ApiOperation(value = "paramType : form")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "form", dataType = "Long", name = "id", value = "参数", required = true) })
    @RequestMapping(value = "/byForm", method = { RequestMethod.POST })
    public String byForm(@RequestParam(value="id",defaultValue="0") Long id) {
        return "user id = " + id;
    }
    
    @ApiOperation(value = "paramType : body")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "body", dataType = "String", name = "id", value = "参数", required = true) })
    @RequestMapping(value = "/queryByBody", method = { RequestMethod.POST })
    public String queryByBody(@RequestBody String id) {
        return "user id = " + id;
    }
    
    @ApiIgnore//忽略此接口说明(在swagger-ui文档中不显示)
    @RequestMapping(value = "/ignore", method = { RequestMethod.POST })
    public String ignore(@RequestParam Long id) {
        return "user id = " + id;
    }
}
View Code

 

  

 

posted @ 2018-06-14 23:58  长久悠悠  阅读(187)  评论(0编辑  收藏  举报