SpringBoot中集成swagger形成API文档

环境版本

springboot 1.5.9.RELEASE
swagger2 2.7.0

开始使用

1.添加依赖

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

2.添加Swagger2配置类

package com.chartered;

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;

/**
 * @ClassName Swagger2
 * @Description
 * @Author Y-Rainson
 * @Date 11/29/2018 10:09 AM
 **/
@Configuration
@EnableSwagger2
public class Swagger2 {

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

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xx项目 RESTful APIs")
                .description("xx项目后台api接口文档")
                .version("1.0")
                .build();
    }

}

3.在Controller中使用

package com.chartered.controller;

import com.chartered.service.HelloService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName HelloController
 * @Description
 * @Author Y-Rainson
 * @Date 12/3/2018 09:54 AM
 **/
@RequestMapping("/Hi")
@ResponseBody
@Controller
@Api(value = "HelloController", description = "Hi接口")
public class HelloController {
    @Autowired
    private HelloService helloService;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    @ApiOperation(value="sayHello", notes="you can use this API to say hello")
    public String sayHello(){
        return helloService.sayHello();
    }

    @RequestMapping(value = "/helloPeople",method = RequestMethod.POST)
    @ApiOperation(value="sayHelloToPeople", notes="you can use this API to say hello to somebody")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "name",value = "you name",required = true ,dataType = "String"),
    })
    public String sayHelloToPeople(@RequestParam("name") String name){
        return helloService.sayHelloToPeople(name);
    }
}

注意:
若接口中有参数的传递,一定要注意添加 paramType=“query” 属性,否则会报错

@ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "name",value = "you name",required = true ,dataType = "String"),
    })

报错如下:

{
  "timestamp": 1543807979427,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.web.bind.MissingServletRequestParameterException",
  "message": "Required String parameter 'name' is not present",
  "path": "/Hi/helloPeople"
}

4.访问

浏览器访问

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

效果如下:
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_30308981/article/details/84754968