博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot集成Swagger2
阅读量:2389 次
发布时间:2019-05-10

本文共 3018 字,大约阅读时间需要 10 分钟。

创建一个SpringBoot工程SpringbootSwagger。

添加依赖

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

编写swagger配置文件

package com.example;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;/** * Created by chenh on 2017/3/24. */@EnableSwagger2@Configurationpublic class Swagger2 {    @Bean    public Docket createRestApi(){        return new Docket(DocumentationType.SWAGGER_2)                .apiInfo(apiInfo())                .select()                .apis(RequestHandlerSelectors.basePackage("com.example"))                .paths(PathSelectors.any())                .build();    }    private ApiInfo apiInfo(){        return new ApiInfoBuilder()                .title("SpringBoot测试使用Swagger2构建RESTful API")                .contact("陈海峰")                .version("1.0")                .description("API 描述")                .build();    }}

编写Restful API

@RestControllerpublic class Controller {    private HashMap
users = new HashMap
(); @ApiOperation(value = "测试post请求", notes = "注意事项") @ApiImplicitParam(dataType = "User", name = "user", value = "用户信息", paramType = "body", required = true) @RequestMapping(value = "/createUser", method = RequestMethod.POST) public String createUser(@RequestBody User user){ users.put(user.getId(), user); System.out.println(users); return "success"; } @ApiOperation(value = "测试get请求", notes="注意事项") @ApiImplicitParam(name = "id", value = "用户id", dataType = "String", paramType = "path") @RequestMapping(value = "/selectUser/{id}", method = RequestMethod.GET) public User selectUser(@PathVariable String id){ System.out.println(id); User user = users.get(Integer.parseInt(id)); System.out.println("user: " + user); return user; } @ApiOperation(value = "测试组合注解", notes = "注意事项") @ApiImplicitParams({ @ApiImplicitParam(dataType = "User", name = "user", value = "用户信息", required = true, paramType = "body"), @ApiImplicitParam(dataType = "string", name = "id", value = "用户id", required = true, paramType = "path") }) @RequestMapping(value = "/updateUser/{id}", method = RequestMethod.POST) public User updateUser(@PathVariable String id, @RequestBody User user){ users.put(user.getId(), user); return user; } @ApiIgnore public String testIgnore(){ return "success"; }}
运行效果

工程源代码

https://github.com/chenhaifeng2016/SpringbootSwagger

你可能感兴趣的文章
Oracle ASM Partnership and Status Table
查看>>
Oracle 12CR2 Oracle Restart - ASM Startup fails with PRCR-1079
查看>>
In-Memory Column Store
查看>>
Oracle 12C ASM asmcmd amdu_extract
查看>>
Oracle AMDU- ASM Metadata Dump Utility
查看>>
C/C++程序中的profile
查看>>
一个更好的Post process结构,三角形代替四边形。
查看>>
利用Vertex shader实现Point Sprites
查看>>
图形处理器历史简介
查看>>
System Memory,AGP Memory and Video Memory in D3D.
查看>>
使用辅助库建立openGL编程环境
查看>>
使用Win32API开始openGL编程
查看>>
使用MFC开始openGL编程
查看>>
关于Gbuffer中的normal存储
查看>>
近距离观察Tone mapping.
查看>>
Physically based shading
查看>>
Color correction
查看>>
Temporal AA
查看>>
miniz compared to other real-time and high-ratio compressors
查看>>
Random number for GPU
查看>>