博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
swagger2简单使用
阅读量:5269 次
发布时间:2019-06-14

本文共 4483 字,大约阅读时间需要 14 分钟。

 

1.引入jar

io.springfox
springfox-swagger2
2.7.0
io.springfox
springfox-swagger-ui
2.7.0

swaggerConfig.注解使用

import io.swagger.annotations.ApiOperation;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.ParameterBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.schema.ModelRef;import springfox.documentation.service.*;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spi.service.contexts.SecurityContext;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.util.ArrayList;import java.util.List;@Configuration@EnableSwagger2public class SwaggerConfig {    @Bean    public Docket createRestApi() {        return new Docket(DocumentationType.SWAGGER_2)                .apiInfo(apiInfo())                .select()                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))                .paths(PathSelectors.any())                .build()                .securitySchemes(securitySchemes())                .securityContexts(securityContexts());    }    private ApiInfo apiInfo() {        return new ApiInfoBuilder()                .title("springboot利用swagger构建api文档")                .description("简单优雅的restful风格")                .termsOfServiceUrl("")                .version("1.0")                .build();    }    private List
securitySchemes() { List
apiKeys = new ArrayList<>(); apiKeys.add(new ApiKey("Authorization", "Authorization", "header")); return apiKeys; } private List
securityContexts() { List
securityContexts = new ArrayList<>(); securityContexts.add(SecurityContext.builder() .securityReferences(defaultAuth()) .forPaths(PathSelectors.regex("^(?!auth).*$")).build()); return securityContexts; } private List
defaultAuth() { AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; authorizationScopes[0] = authorizationScope; List
securityReferences = new ArrayList<>(); securityReferences.add(new SecurityReference("Authorization", authorizationScopes)); return securityReferences; }}

3注解使用

import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import io.swagger.annotations.ApiOperation;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/swagger2")public class Swagger2Controller {    @PostMapping("/hello")    @ApiOperation(value = "hello",notes = "一个参数")    @ApiImplicitParam(name = "name",paramType = "query",defaultValue = "lisi")    public String hello(String name){        return "hello "+name;    }    @PostMapping("/info")    @ApiOperation(value = "info",notes = "两个参数")    @ApiImplicitParams({        @ApiImplicitParam(name = "name",paramType = "query",defaultValue = "lisi"),        @ApiImplicitParam(name = "age",paramType = "query",defaultValue = "15")    })    public String info(String name,String age){        return "hello "+name+", age:"+age;    }    @PostMapping("/getUser")    @ApiOperation(value = "getUser",notes = "参数是对象,返回值是对象")    @ApiImplicitParam(name = "user",paramType = "body",dataType = "User")    public User getUser(@RequestBody User user){        return user;    }}

 user实体类

import io.swagger.annotations.ApiModelProperty;import lombok.Data;@Datapublic class User {    @ApiModelProperty(value = "姓名",example = "zhangSan")    String name;    @ApiModelProperty(value = "年龄",example = "16")    String age;}

 

 

效果

note: lisi是默认值

 

 

 

转载于:https://www.cnblogs.com/alittlesmile/p/11254473.html

你可能感兴趣的文章
Factory Design Pattern
查看>>
P1192-台阶问题
查看>>
Java大数——a^b + b^a
查看>>
简单的数据库操作
查看>>
帧的最小长度 CSMA/CD
查看>>
树状数组及其他特别简单的扩展
查看>>
普通求素数和线性筛素数
查看>>
PHP截取中英文混合字符
查看>>
电子眼抓拍大解密
查看>>
51nod1076 (边双连通)
查看>>
Linux pipe函数
查看>>
java equals 小记
查看>>
2019春 软件工程实践 助教总结
查看>>
Zerver是一个C#开发的Nginx+PHP+Mysql+memcached+redis绿色集成开发环境
查看>>
程序的静态链接,动态链接和装载 (补充)
查看>>
关于本博客说明
查看>>
Linux服务器在外地,如何用eclipse连接hdfs
查看>>
[Kaggle] Sentiment Analysis on Movie Reviews
查看>>
价值观
查看>>
mongodb命令----批量更改文档字段名
查看>>