添加依赖包
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 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. 启动项目,浏览器输入
效果如下图:
.