首页 > 代码库 > springboot配置swagger2
springboot配置swagger2
、在pom.xml里添加jar包:
[java] view plain copy
- <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里添加版本
[java] view plain copy
- <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文件
[java] view plain copy
- 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,
[java] view plain copy
- 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。