1. 程式人生 > >SpringBoot中整合swagger形成API文件

SpringBoot中整合swagger形成API文件

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

效果如下:
在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述