`
vtrtbb
  • 浏览: 353139 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring boot 中用Swagger2 构建API说明文档

    博客分类:
  • java
阅读更多

maven:

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

 

配置文件:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Configuration {
	
	@Bean
    public Docket buildDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInf())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.site.portal.web"))//要扫描的API(Controller)基础包
                .paths(PathSelectors.any())
                .build();
    }
	
    private ApiInfo buildApiInf() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2 UI构建API文档")
                .contact("测试下")
                .version("1.0")
                .build();
    }
}

 

 controller 类中:

 

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "城市服务",description="简单的事例")
@RestController
@RequestMapping("/cities")
public class CityController {
	
	@Autowired
    private CityService cityService;
	
	@ApiOperation("城市列表")
    @RequestMapping
    public PageInfo<City> getAll(City city) {
        List<City> countryList = cityService.getAll();
        return new PageInfo<City>(countryList);
    }
	
	@ApiOperation("添加城市")
    @RequestMapping(value = "/add")
    public City add() {
        return new City();
    }

    @RequestMapping(value = "/view/{id}")
    public City view(@PathVariable Integer id) {
        ModelAndView result = new ModelAndView();
        City city = cityService.getById(id);
        return city;
    }
    
    @ApiOperation("删除城市")
    @RequestMapping(value = "/delete/{id}")
    public ModelMap delete(@PathVariable Integer id) {
        ModelMap result = new ModelMap();
        cityService.deleteById(id);
        result.put("msg", "删除成功!");
        return result;
    }
    
    @ApiOperation("保存城市")
    @RequestMapping(value = "/save")
    public void save() {
        ModelMap result = new ModelMap();
        City city=new City();
        city.setName("beijing"+System.currentTimeMillis());
        city.setState("1");
        String msg = city.getId() == null ? "新增成功!" : "更新成功!";
        cityService.save(city);
        result.put("city", city);
        result.put("msg", msg);
    }
}

 

重新打包部署你的项目到WEB服务器,

访问地址

http://localhost:8080/your-contextpath/api-docs即可看到注解生成的API说明

访问地址

http://localhost:8080/your-contextpath/swagger-ui.html即可看到API信息使用方法

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics