1. Add jar package
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency>
2. Added @EnableSwagger2
@SpringBootApplication(scanBasePackages = {"org.java.com.*"}) @EnableSwagger2 public class Application { /** * 1.java -jar * <p> * 2.mvn spring-boot:run * * @param args */ public static void main(String[] args) { SpringApplication.run(Application.class, args); } }3. Add model class
@ApiModel("User Model") @AllArgsConstructor @Data public class User implements Serializable { private static final long serialVersionUID = -3378006049247087150L; @ApiModelProperty("User ID") private Long id; @ApiModelProperty("User Name") private String name; @ApiModelProperty("Login Password") private String password; }
4.Add annotations to controller
@Api("user related Api") @RestController @RequestMapping("/firstboot") public class FirstBootController { @ApiOperation("Get user information based on id") @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", dataType = "long", required = true, value = "用户编号id")}) @ApiResponses({ @ApiResponse(code = 0000, message = "Call succeeded", response = User.class) }) @GetMapping(value = "/sayHello") public User getUserInfo(@RequestParam("id") Long id) { return new User(1L, "测试", "12334"); } }
5. Browser input
http://www.localhost.com:8080/swagger-ui.html
The effect is as follows:
View available services
View the model
request call