spring-boot+swagger实现WebApi文档

Posted umeall

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-boot+swagger实现WebApi文档相关的知识,希望对你有一定的参考价值。

1、引用依赖包

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

 

2、新建 SwaggerConfig

  

package cn.com.xxx.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    public Docket swaggerSpringMvcPlugin(){
        return  new Docket(DocumentationType.SWAGGER_2)
                        .select()
                        .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                        .build();
    }
}

 

3、配置接口类

 

package cn.com.xxx.controller;

import cn.com.xxx.dao.AccountDao;
import cn.com.xxx.po.T_Account;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "")
@RestController
@RequestMapping(value = "/api/test")
public class ApiTestController {
    @Autowired
    private AccountDao accountDao;

    @ApiOperation(nickname = "获取人员信息",value = "账号")
    @RequestMapping(value =  "/getAccountByUserName/{userName}",method =  RequestMethod.GET)
    public T_Account getAccountByUserName(@PathVariable("userName") String userName){


        return  accountDao.findUserInfoByUserName(userName);
    }
}

 

4、配置spring boot启动类

package cn.com.xxx;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Hello world!
 *
 */
@MapperScan(basePackages = {"cn.com.xxx.dao"})
@SpringBootApplication(scanBasePackages = {"cn.com.xxx"})
public class App 
{
    private  static  final Logger logger = LoggerFactory.getLogger(App.class);
    public static void main( String[] args )
    {
        logger.info("开始启动");
        SpringApplication.run(App.class,args);
        logger.info("启动结束");
    }

}

5、启动spring boot并访问:http://localhost:端口/swagger-ui.html 

  技术图片

6、输入测试数据并获取结果

  技术图片

技术图片

 

 至此集成spring+swagger集成结束。

 

以上是关于spring-boot+swagger实现WebApi文档的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot集成Springfox-Swagger2

无法从 spring-boot 应用程序中调出 swagger-ui

在带有 spring-boot rest 和 Swagger 的标头中使用 utf-8 字符时未加载响应

spring-boot实战06:Spring Boot中使用Swagger2

swagger使用总结

SpringBoot-整合Swagger2