码迷,mamicode.com
首页 > 编程语言 > 详细

spring-boot+swagger实现WebApi文档

时间:2019-03-25 16:15:58      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:play   9.png   mybatis   oca   operation   ring   imp   framework   启动   

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文档

标签:play   9.png   mybatis   oca   operation   ring   imp   framework   启动   

原文地址:https://www.cnblogs.com/umeall/p/10594206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!