springboot2.0+swagger集成

场景:项目中添加Swagger配置,可以加速项目的开发,在快速开发项目中十分重要。

1、pom.xml添加swagger

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>

 

2、添加SwaggerConfig.java文件

/*
 * (C) Copyright 2018 Siyue Holding Group.
 */
package com.mycat.demo.config;

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.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class Swagger {
  @Bean
  public Docket createRestApi() {
    ParameterBuilder tokenPar = new ParameterBuilder();
    List<Parameter> pars = new ArrayList<>();
    tokenPar.name("token").description("令牌")
        .modelRef(new ModelRef("string")).parameterType("header").required(false).build();
    pars.add(tokenPar.build());
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.mycat.demo.controller")) #扫描的controller包
        .paths(PathSelectors.any())
        .build().globalOperationParameters(pars)  ;
  }

 /* @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.mycat.demo.controller"))
            .paths(PathSelectors.any())
            .build();
  }*/

  @SuppressWarnings("deprecation")
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("Mycat配置测试")
        .description("Mycat微服务 API 文档")
        .contact("Develop Team")
        .version("1.0")
        .build();
  }

}

 

3、在controller类中添加,@ApiOperation(nickname = "addMember",value="添加用户",httpMethod = "POST")

@RequestMapping("/addMember")
@ApiOperation(nickname = "addMember",value="添加用户",httpMethod = "POST")
@ResponseBody
public JSONObject addMember(HttpServletRequest request, HttpServletResponse response, @RequestBody Member member) {
JSONObject jsonObject = new JSONObject();
/* Member member = new Member();
member.setName("yangwenjie345");
member.setAge(20);*/
int insertNum = memberService.insert(member);
if(insertNum == 0){
jsonObject.put("code", CodeEnum.ERROR_CREATE_FAIL.getValue());
jsonObject.put("message",CodeEnum.ERROR_CREATE_FAIL.getMessage());
return jsonObject;
}
//查询有结果
jsonObject.put("code", CodeEnum.SUCCESS.getValue());
jsonObject.put("message",CodeEnum.SUCCESS.getMessage());
return jsonObject;
}

 

4、启动项目访问

http://localhost:8080/swagger-ui.html

 

demo的github地址:https://github.com/812406210/springboot2.0-swagger

如有问题,可以留言!

posted @ 2019-01-31 16:50  小白啊小白,Fighting  阅读(1134)  评论(0编辑  收藏  举报