本文共 4770 字,大约阅读时间需要 15 分钟。
1、在pom.xml里添加jar包:
在pom.xml里的properties里添加版本io.springfox springfox-swagger-ui ${springfox.version} io.springfox springfox-swagger2 ${springfox.version}
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() { PredicateswaggerSelector = 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/