博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring boot 集成 Swagger
阅读量:5825 次
发布时间:2019-06-18

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

 

添加依赖包

io.springfox
springfox-swagger2
2.7.0
io.springfox
springfox-swagger-ui
2.7.0

 

开启注解

@EnableSwagger2

 

 

示例如下:

1.   新建 Maven 项目 swagger

 

2.   pom.xml

4.0.0
com.java
swagger
1.0.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
1.8
org.springframework.boot
spring-boot-starter-web
io.springfox
springfox-swagger2
2.7.0
io.springfox
springfox-swagger-ui
2.7.0
org.springframework
springloaded
1.2.8.RELEASE
org.springframework.boot
spring-boot-devtools
${project.artifactId}
org.apache.maven.plugins
maven-compiler-plugin
${jdk.version}
${jdk.version}
UTF-8
org.springframework.boot
spring-boot-maven-plugin
repackage

 

3.   SwaggerStarter.java

package com.java.swagger;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SwaggerStarter {    public static void main(String[] args) {        SpringApplication.run(SwaggerStarter.class, args);    }}

 

 

4.   SwaggerConfig.java

package com.java.swagger.config;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.ApiSelectorBuilder;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration@EnableSwagger2public class SwaggerConfig {    @Bean    public Docket createRestApi() {        Docket docket = new Docket(DocumentationType.SWAGGER_2);        ApiSelectorBuilder builder = docket.apiInfo(apiInfo()).select();        builder.apis(RequestHandlerSelectors.basePackage("com.java.swagger.controller"));        builder.paths(PathSelectors.any());        return builder.build();    }    private ApiInfo apiInfo() {        ApiInfoBuilder builder = new ApiInfoBuilder();        builder.title("我的项目Swagger");        builder.description("简单易用的API文档");        builder.version("V1.0");        return builder.build();    }}

 

 

5.   User.java

package com.java.swagger.pojo;import java.util.Date;import com.fasterxml.jackson.annotation.JsonFormat;import io.swagger.annotations.ApiModelProperty;public class User {    @ApiModelProperty("用户名")    private String name;    @ApiModelProperty("年龄")    private int age;    @ApiModelProperty(value = "生日", example = "2018-12-31 10:00:00")    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")    private Date birthDay;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public Date getBirthDay() {        return birthDay;    }    public void setBirthDay(Date birthDay) {        this.birthDay = birthDay;    }}

 

 

6.   UserController.java

package com.java.swagger.controller;import java.util.Calendar;import java.util.Date;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.java.swagger.pojo.User;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;@RestController@RequestMapping("/user")public class UserController {    @GetMapping("/getUesr/{name}")    @ApiOperation(value = "查询用户信息", notes = "各种解释说明信息")    public User getUser(@ApiParam("用户名") @PathVariable String name) {        User user = new User();        user.setName(name);        user.setAge(18);        user.setBirthDay(new Date());        return user;    }    @PostMapping("/addUser")    @ApiOperation(value = "新增用户", notes = "各种解释说明信息")    public User addUser(@ApiParam("用户信息") @RequestBody User user) {        Calendar calendar = Calendar.getInstance();        calendar.setTime(user.getBirthDay());        calendar.add(Calendar.DAY_OF_MONTH, 1);        user.setBirthDay(calendar.getTime());        user.setAge(user.getAge() + 1);        return user;    }}

 

 

7. 启动项目,浏览器输入

 

效果如下图:

 

 

 

 

 

.

转载于:https://www.cnblogs.com/jonban/p/swagger.html

你可能感兴趣的文章
iOS 解决UITabelView刷新闪动
查看>>
CentOS 7 装vim遇到的问题和解决方法
查看>>
JavaScript基础教程1-20160612
查看>>
ios xmpp demo
查看>>
python matplotlib 中文显示参数设置
查看>>
【ros】Create a ROS package:package dependencies报错
查看>>
通过容器编排和服务网格来改进Java微服务的可测性
查看>>
re:Invent解读:没想到你是这样的AWS
查看>>
PyTips 0x02 - Python 中的函数式编程
查看>>
使用《Deep Image Prior》来做图像复原
查看>>
Linux基础命令---rmdir
查看>>
Android图片添加水印图片并把图片保存到文件存储
查看>>
BigDecimal 舍入模式(Rounding mode)介绍
查看>>
开源 免费 java CMS - FreeCMS1.2-标签 infoSign
查看>>
Squid 反向代理服务器配置
查看>>
Java I/O操作
查看>>
Tomcat性能调优
查看>>
Android自学--一篇文章基本掌握所有的常用View组件
查看>>
灰度图像和彩色图像
查看>>
FreeMarker-Built-ins for strings
查看>>