springboot配置swagger2
Posted xuyatao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot配置swagger2相关的知识,希望对你有一定的参考价值。
、在pom.xml里添加jar包:
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>${springfox.version}</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>${springfox.version}</version>
- </dependency>
在pom.xml里的properties里添加版本
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- <springfox.version>2.5.0</springfox.version>
- </properties>
2、在com/demo下创建swagger文件夹,创建SwaggerConfig文件
- package com.demo.swagger;
- 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;
- import springfox.documentation.swagger2.annotations.EnableSwagger2;
- /**
- * Created by huguoju on 2016/12/29.
- */
- @Configuration
- @EnableSwagger2
- public class SwaggerConfig {
- /**
- * 可以定义多个组,比如本类中定义把test和demo区分开了 (访问页面就可以看到效果了)
- *
- */
- @Bean
- public Docket testApi() {
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo())
- .select()
- .apis(RequestHandlerSelectors.basePackage("com.demo"))
- .paths(PathSelectors.any()).build();
- }
- private ApiInfo apiInfo() {
- return new ApiInfoBuilder()
- .title("Spring Boot中使用Swagger2构建RESTful APIs")
- .description("spring Boot 中构建RESTful API")
- .termsOfServiceUrl("")
- .contact("huguoju")
- .version("1.0")
- .build();
- }
- }
以上就完成了,在页面访问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("")
- 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
- public class User {
- @ApiModelProperty("用户id")
- private Integer userCode;
- @ApiModelProperty("用户类型")
- private String userType;
- @ApiModelProperty("用户名称")
- private String userName;
- @ApiModelProperty("用户手机号")
- private String mobileNumber;
- }
以上是关于springboot配置swagger2的主要内容,如果未能解决你的问题,请参考以下文章