博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot(六)配置swagger2
阅读量:4299 次
发布时间:2019-05-27

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

1、在pom.xml里添加jar包:

io.springfox
springfox-swagger-ui
${springfox.version}
io.springfox
springfox-swagger2
${springfox.version}
   在pom.xml里的properties里添加版本

UTF-8
UTF-8
1.8
2.7.0

2、在com/demo下创建swagger文件夹,创建SwaggerConfig文件

package com.boot.common.config;import com.google.common.base.Predicate;import io.swagger.annotations.ApiOperation;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import springfox.documentation.RequestHandler;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.service.ApiKey;import springfox.documentation.service.BasicAuth;import springfox.documentation.service.Contact;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;import static com.google.common.collect.Lists.newArrayList;@Configuration@EnableSwagger2public class SwaggerConfig{   @Bean   public Docket userApi() {       Predicate
swaggerSelector = RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class); return new Docket(DocumentationType.SWAGGER_2) .securitySchemes(newArrayList(new ApiKey[]{this.apiKey()}))// .securitySchemes(newArrayList(new BasicAuth("test"))) //账号密码登录// .enable(false) //禁止使用 .apiInfo(apiInfo()) .select()// .apis(RequestHandlerSelectors.basePackage("com.boot")) .apis(swaggerSelector) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("boot RESTFul APIs") .description("构建restful api,learn more:springfox.io") .license("Licens") .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html") .contact(new Contact("boot","官网地址url","email地址")) .version("0.0.1") .build(); } ApiKey apiKey() { return new ApiKey("sessionId", "sessionId", "header"); }} 当securitySchemes为apiKey时,这里存的是sessionId,也可以存其他数据,在controller里使用 String api_key = request.getParameter("sessionId"); 获取到存储的sessionId。 当securitySchemes为basicAuth时,controller里使用 request.getHeader("Authorization"); 获取数据,数据是经过base64编码的:Basic ZWVlOnd3dw== ,需要解码: String auth=s3.replace("Basic","").trim(); new String(Base64.getDecoder().decode(auth));在swagger-ui.html最上面点击Authorize就可以输入sessionId或者用户名密码。 以上就完成了,在页面访问localhost:8080/swagger-ui.html,就看见了,下面主要说说怎么用 1、在com/dem/controller下创建TestController, package com.demo.controller;import com.demo.model.User;import com.demo.service.TestService;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.MediaType;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;/** * Created by huguoju on 2016/12/28. */@Controller@RequestMapping("test")@Api(value = "测试类",tags = "测试接口")public class TestController { @Autowired private TestService testService; @RequestMapping(value = "testData",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = {RequestMethod.POST,RequestMethod.GET}) @ApiOperation("测试读写分离") public String testDateSource( @ApiParam(name = "userCode",value = "用户id",required = true) @RequestParam Integer userCode){ User user=testService.selectByUserCode(userCode); Integer integer=testService.insertUser(user); return "oo"; }} 以上就是controller里主要用到的@Api,  @ApiOperation ,@ApiParam 在model里使用@ApiModelProperty("") package com.example.model;import com.fasterxml.jackson.annotation.JsonIdentityInfo;import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;import lombok.Data;import org.springframework.stereotype.Component;import java.util.Date;@Data@ApiModel(value="用户信息") public class User { @ApiModelProperty("用户id") private Integer userCode; @ApiModelProperty("用户类型") private String userType; @ApiModelProperty("用户名称") private String userName; @ApiModelProperty("用户手机号") private String mobileNumber;}

转载地址:http://empws.baihongyu.com/

你可能感兴趣的文章
Python+Selenium练习篇之21-验证控件是否被选中
查看>>
Python+Selenium练习篇之22-获取页面元素大小
查看>>
Python+Selenium练习篇之23-组合键-全选文字
查看>>
Python+Selenium练习篇之24-组合键-退格键删除文字
查看>>
Python+Selenium练习篇之25-鼠标右键
查看>>
Python+Selenium练习篇之26-执行JavaScript
查看>>
Jenkins高级篇之Pipeline实践篇-5-Selenium和Jenkins持续集成-Pipelinejob草稿版
查看>>
Jenkins高级篇之Pipeline实践篇-6-Selenium和Jenkins持续集成-pipeline参数化构建selenium自动化测试
查看>>
Jenkins高级篇之Pipeline实践篇-7-Selenium和Jenkins持续集成-publish html report插件的pipeline使用介绍
查看>>
Jenkins高级篇之Pipeline实践篇-8-Selenium和Jenkins持续集成-添加事后删除报告功能和解决报告名称硬编码
查看>>
Jenkins高级篇之Pipeline实践篇-9-Selenium和Jenkins持续集成-日志文件归档和插件rebuild介绍--完结篇
查看>>
Java多线程-48-单例设计模式
查看>>
Java多线程-49-Runtime类学习
查看>>
Java多线程-50-Timer类
查看>>
Java多线程-51-两个线程之间的通信
查看>>
Java多线程-52-三个和三个以上线程之间的通信
查看>>
Java多线程-53-ReentrantLock类-互斥锁
查看>>
Java多线程-54-ThreadGroup线程组
查看>>
Java多线程-55-ExecutorService线程池
查看>>
Java多线程-56-简单工厂设计模式
查看>>