SpringBoot中集成swagger形成API文档

本文档介绍了如何在SpringBoot 1.5.9.RELEASE环境中集成Swagger2,版本为2.7.0,以形成API文档。步骤包括添加依赖、配置Swagger2、在Controller中使用及处理自定义对象参数。特别提醒,在RESTful接口中,参数的paramType需设置为"path"而非"query",以避免错误。
摘要由CSDN通过智能技术生成

环境版本

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

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

在这里插入图片描述

在这里插入图片描述

参数是自定义对象

@RequestMapping(value = "/helloUser",method = RequestMethod.POST)
    @ApiOperation(value="helloUser", notes="hello to user")
    public User helloUser(@RequestBody @ApiParam(name="user",value="user entity") User user){
        return helloService.sayHelloToUser(user);
    }

并在对应的实体类中加入注解:

package com.chartered.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.katharsis.resource.annotations.JsonApiId;
import io.katharsis.resource.annotations.JsonApiResource;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.stereotype.Component;

/**
 * Created by 1593977 on 9/4/2018.
 */
@ApiModel(value="User Entity")
public class User {
   
    @ApiModelProperty(value="user id",example="2998324787234897")
    private Long userId;

   
    @ApiModelProperty(value="user email",example="xx@sc.com")
    private String email;

   
    @ApiModelProperty(value="user name",example="jack")
    private String name;

  
}

swagger2+restful接口风格

其他的没有差别,只是restful接口是参数绑定时候

    @RequestMapping(value = "department/{departmentId}", method = RequestMethod.GET)
    @ApiOperation(value="findOne", notes="Use this API to find a department by id.")
    @ApiImplicitParam(paramType ="path",name = "departmentId",value = "department id",required = true ,dataType = "String")
    public ResponseResult findOne(@PathVariable String departmentId) {
        return departmentService.queryOne(departmentId);
    }

这里的paramType =“path” 而不是之前的"query"了!!这点要记得

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值