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集成结束。

 

转载于:https://www.cnblogs.com/umeall/p/10594206.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值