• 周六. 10 月 12th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Try the spring fox 3.0.0 released just now, the wheels made before can not be used

King Wang

1 月 3, 2022

lately SpringFox 3.0.0 Released , From the last big version 2.9.2 A good 2 More than years . You might see the name , Many readers will be a little strange . however , Just show you these two dependencies , You will know !

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>

When we are using Spring MVC When writing interfaces , In order to generate API file , To facilitate integration Swagger, It’s all with this SpringFox This package of . however , since 2.9.2 After the update , There has been no movement , No better Spring Boot The trend of , For a while, I have been writing a configuration class to add document configuration for the project . So , This wheel was built before :

  • https://github.com/SpringForAll/spring-boot-starter-swagger

It’s not too difficult , It was made early , So I got a lot of Star. Now? SpringFox There is a starter, Take a look at the features , It’s not perfect , But it’s a lot better than our own wheels before . Let’s see what the highlights of this version :

  • Spring 5,Webflux Support ( Only request mapping support , Functional endpoints are not yet supported )
  • Spring Integration Support
  • Spring Boot Support springfox-boot-starter Dependence ( Zero configuration , Automatic configuration support )
  • Document configuration properties with autocomplete function
  • Better specification compatibility
  • Support OpenApi 3.0.3
  • Almost zero dependence ( The only library needed is spring-plugin、pswagger-core)
  • The existing swagger2 Comments will continue to be valid , And enrich open API 3.0 standard

For this update , I think there are some outstanding points :Webflux Support for , The current wheels don’t do it ; Yes OpenApi 3 Support for ; And right Swagger 2 Compatibility ( It’s easy to upgrade ).

Try something fresh

Say so much , It’s better to have a program experiment !

First step : Create a Spring Boot project , Not here , No, look at the previous tutorial : Quick start

The second step pom.xml Add dependency to :

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
<dependency>

It’s a lot simpler now , A dependency to fix !

The third step : Apply the main class to add annotations @EnableOpenApi.

@EnableOpenApi
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

Step four : Configure some interface examples , such as :

@Api(tags=" User management ")
@RestController
public class UserController {
@ApiOperation(" Create user ")
@PostMapping("/users")
public User create(@RequestBody @Valid User user) {
return user;
}
@ApiOperation(" User details ")
@GetMapping("/users/{id}")
public User findById(@PathVariable Long id) {
return new User("bbb", 21, " Shanghai ", "[email protected]");
}
@ApiOperation(" User list ")
@GetMapping("/users")
public List<User> list(@ApiParam(" Look at the page ") @RequestParam int pageIndex,
@ApiParam(" How many pieces per page ") @RequestParam int pageSize) {
List<User> result = new ArrayList<>();
result.add(new User("aaa", 50, " Beijing ", "[email protected]"));
result.add(new User("bbb", 21, " Guangzhou ", "[email protected]"));
return result;
}
@ApiIgnore
@DeleteMapping("/users/{id}")
public String deleteById(@PathVariable Long id) {
return "delete user : " + id;
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(" Basic information of users ")
public class User {
@ApiModelProperty(" full name ")
@Size(max = 20)
private String name;
@ApiModelProperty(" Age ")
@Max(150)
@Min(1)
private Integer age;
@NotNull
private String address;
@Pattern(regexp = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$")
private String email;
}

Step five : Start the application ! visit swagger page :http://localhost:8080/swagger-ui/index.html

Be careful :

  1. This update , Removed the original default swagger Page path :http://host/context-path/swagger-ui.html, Two new accessible paths :http://host/context-path/swagger-ui/index.html and http://host/context-path/swagger-ui/
  2. By adjusting the log level , You can also see a new version of swagger The document interface has also been added , In addition to the old version of the document interface /v2/api-docs outside , There’s a new version of /v3/api-docs Interface .

This series of tutorials 《Spring Boot 2.x Basic course 》 Click through !

Code example

For an example of this article, see the following in the warehouse chapter2-7 Catalog :

  • Github:https://github.com/dyc87112/SpringBoot-Learning/
  • Gitee:https://gitee.com/didispace/SpringBoot-Learning/

If you think this article is good , welcome Star Support , Your concern is the driving force of my persistence !

First article : Try the new one SpringFox 3.0.0, The wheels made before can be used out of use …, Reprint please indicate the source . Welcome to my official account. : Program the ape DD, Get exclusive learning resources and daily dry goods push . Click to the contents of this series .

发表回复