swagger demo code

Posted yuwentims

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

  

 






以上是关于swagger demo code的主要内容,如果未能解决你的问题,请参考以下文章

csharp 在Swashbuckle Swagger中,此片段允许按字母顺序显示操作。

用swagger生成接口文档代码

使用Swagger编写规范API接口

OpenAPI(Swagger Code Generator)注入式漏洞

Sublime Text自定制代码片段(Code Snippets)

vs code 自定义代码片段