SpringFox 3.0.0 was recently released, more than two years after its last major release, 2.9.2. May see this name, many readers will be a little strange. But just take a look at these two dependencies and you’ll see!
< the 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 >Copy the code
When we use Spring MVC to write interfaces, to generate API documentation and to integrate Swagger, we use this SpringFox package. However, since the 2.9.2 update, there has been no movement, nor the big Spring Boot trend, and for a while it was always a matter of writing a configuration class to add documentation to a project. To that end, this wheel was built earlier:
- Github.com/SpringForAl…
It was not difficult, but it was built early, so I got a lot of stars. Now SpringFox has a starter, and we looked at the features, and it’s not perfect, but it’s a lot better than our own wheels. Take a look at some of the highlights of this release:
- Spring 5, Webflux support (mapping support only requested, functional endpoints not yet supported)
- Spring Integration support
- Spring Boot supports springfox-boot-starter dependency (zero configuration, automatic configuration support)
- Documented configuration properties with auto-completion
- Better specification compatibility
- Support OpenApi 3.0.3
- Almost zero dependencies (the only libraries required are Spring-plugin, pSwagger-core)
- The existing Swagger2 annotation will remain in effect and enrich the Open API 3.0 specification
For this update, I think there are a few points that stand out: Webflux support, which the current wheels do not; OpenApi 3 support; And Swagger 2 compatibility.
Learn to try
Say so much, how about a procedural experiment is more direct!
Step 1: Create a Spring Boot project, which will not be expanded here and will not be seen in the previous tutorial: Quick Start
Step 2: Add dependencies to pom.xml:
< the dependency > < groupId > IO. Springfox < / groupId > < artifactId > springfox - the boot - starter < / artifactId > < version > 3.0.0 < / version > <dependency>Copy the code
Now a lot of succinct, a dependency fix!
Step 3: Apply the @enableOpenAPI annotation to the main class.
@EnableOpenApi @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code
Step 4: 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 (" userid ") @getMapping ("/users/{id}") public User findById(@pathVariable Long id) {return new User(" BBB ", 21, "Shanghai ", "[email protected]"); } @apiOperation (" User ") @getMapping ("/users") public List<User> List (@apiparam (" User ") @requestParam int pageIndex, @apiParam (" how many pages per page ") @requestParam int pageSize) {List<User> result = new ArrayList<>(); Result. The add (new User (" aaa ", 50, "Beijing", "[email protected]")); Result. The 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 () public class User {@apiModelProperty (" 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; }Copy the code
Step 5: Launch the app! Access to swagger page: http://localhost:8080/swagger-ui/index.html
Note:
- This update removes the default swagger page path:
http://host/context-path/swagger-ui.html
, two new accessible paths are added:http://host/context-path/swagger-ui/index.html
andhttp://host/context-path/swagger-ui/
- By adjusting the log level, you can also see the new Swagger document interface, in addition to the old document interface
/v2/api-docs
In addition, there is a new version/v3/api-docs
Interface.
Spring Boot 2.x Basic Tutorial
Code sample
For an example of this article, see the chapter2-7 directory in the repository below:
- Github:github.com/dyc87112/Sp…
- Gitee:gitee.com/didispace/S…
If you found this article good, welcomeStar
Support, your attention is my motivation!
SpringFox 3.0.0 has just been released, and the previously built wheels can no longer be used… Please indicate the source. Welcome to pay attention to my official account: Program ape DD, for exclusive learning resources and daily dry goods push. Click through to the tutorial catalog for this series.