springboot搭建Swagger2

本文详细介绍了如何在Spring Boot项目中集成Swagger2,包括添加依赖、配置启动类及使用注解来实现API文档的自动生成。通过具体示例展示了如何访问生成的文档页面。

1.添加jar

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

 2.写一个Swagger2启动类

package com.test.springboot.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.swagger.annotations.ApiOperation;
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 {

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

}

3.注解实现部分

package com.test.springboot.controller;

import com.test.springboot.bean.CardBean;
import com.test.springboot.service.CardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.List;

@Api(
        value = "/accountlogin",
        tags = {"信息卡接口"})
@RestController
public class HelloController {
    @Autowired
    private CardService cardService;

    @ApiOperation(
            value = "获取卡列表",
            produces = "application/json",
            consumes = "application/json")
    @RequestMapping(value ="/getlist",method = RequestMethod.GET)
    @ResponseBody
    public List<CardBean> list (){
        return cardService.list();
    }

    @ApiOperation(
            value = "添加卡接口",
            produces = "application/json",
            consumes = "application/json")
    @RequestMapping(value ="/addbill",method = RequestMethod.POST)
    @ResponseBody
    public List<CardBean> addbill (){
        return cardService.list();
    }
}

4.ok,直接访问:http://localhost:8080/swagger-ui.html

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值