springboot搭建Swagger2

1.添加jar

                
		
			io.springfox
			springfox-swagger2
			2.6.1
		
		
			io.springfox
			springfox-swagger-ui
			2.6.1
		

 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 list (){
        return cardService.list();
    }

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

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

springboot搭建Swagger2_第1张图片 

 

你可能感兴趣的:(springboot)