Using swagger to automate the generation of API documents not only increases the maintainability of the project, but also improves the transparency of the API, which facilitates faster testing and so on, facilitating quicker discovery and resolution of problems.
This article only records the integration process, and other features such as security configuration are not discussed here, interested in the following links to learn more.
Reference Documentation:
https://howtodoinjava.com/swagger2/swagger-spring-mvc-rest-example/http://www.baeldung.com/ swagger-2-documentation-for-spring-rest-apihttp://blog.didispace.com/springbootswagger2/
Version of each component in the project:
spring.version=4.3. 18.RELEASE jackson.version=2.9. 0 swagger.version=2.7. 0
Core POM configuration (omitted from spring):
<Dependency> <groupId>Io.springfox</groupId> <Artifactid>Springfox-swagger2</Artifactid> <version>${swagger.version}</version></Dependency><Dependency> <groupId>Io.springfox</groupId> <Artifactid>Springfox-swagger-ui</Artifactid> <version>${swagger.version}</version></Dependency><Dependency> <groupId>Com.fasterxml.jackson.core</groupId> <Artifactid>Jackson-databind</Artifactid> <version>${jackson.version}</version></Dependency><Dependency> <groupId>Com.fasterxml.jackson.core</groupId> <Artifactid>Jackson-core</Artifactid> <version>${jackson.version}</version></Dependency><Dependency> <groupId>Com.fasterxml.jackson.core</groupId> <Artifactid>Jackson-annotations</Artifactid> <version>${jackson.version}</version></Dependency>
To write the Swagger configuration class:
Tip: The students who did the interception process need to be aware of the open Swagger Resource access path:/swagger-resources/*,/swagger-ui.html,/v2/api-docs ,/webjars/*
@Configuration @enableswagger2@enablewebmvc@componentscan ("Springfox") Public classSwaggerconfigextendsWebmvcconfigureradapter {@Bean PublicDocket Createrestapi () {return NewDocket (documentationtype.swagger_2). Select (). APIs (Requesthandlerselectors.withclassan Notation (Api.class). paths (Pathselectors.any ()). Build (). Apiinfo (Apiinfo ()); } Privateapiinfo Apiinfo () {return NewApiinfobuilder (). Title ("REST API Description Document"). Description ("REST API Description Document"). Version ("1.0"). Termsofserviceurl ("Http://localhost:9080/"). Contact (NewContact ("Lichmama", "", "" "). License ("Apache License 2.0"). Licenseurl ("Https://www.apache.org/licenses/LICENSE-2.0"). build (); } @Override Public voidaddresourcehandlers (Resourcehandlerregistry registry) {Registry.addresourcehandler ("Swagger-ui.html"). Addresourcelocations ("classpath:/meta-inf/resources/")); Registry.addresourcehandler ("/webjars/**"). Addresourcelocations ("classpath:/meta-inf/resources/webjars/")); }}
Add Configuration in Springmvc-servlet.xml:
<class= "Com.lichmama.demo.core.swagger.SwaggerConfig"/>
Use swagger annotations (where apioperation and Apiimplicitparam are particularly critical) on Restcontroller to automatically generate documents:
@RestController @requestmapping ("/config") @Api (description = "Configuration Management Interface") @Slf4j Public classconfigaction {@PostMapping ("/set") @ApiOperation (value= "Change or new configuration information") @ApiResponses (value= {@ApiResponse (code = $, message = "System error"), @ApiResponse (code = 0, message = "Succeeded")}) @ApiImplicitParams ({@ApiImplicitParam (name= "Key", value = "Key", Paramtype = "Form", DataType = "string"), @ApiImplicitParam (name= "Value", value = "values", Paramtype = "Form", DataType = "string") }) Publicactionmessage setconfig (string key, String value) {Log.debug ("Key: {}, Value: {}", key, value); Configutil.setconfig (key, value); returnactionstatus.success (); } @GetMapping ("/get") @ApiOperation (value= "Get configuration Information") @ApiImplicitParam (name= "Key", value = "Key", Paramtype = "Query", DataType = "string") PublicMap<string, object>GetConfig (String key) {Object value=Configutil.getconfig (key); Log.debug ("Key: {}, Value: {}", key, value); Map<string, object> map =NewHashmap<>(); Map.put (key, value); returnmap; }}
Start the project, Access http://{host:port}/{project}/swagger-ui.html to see if the configuration takes effect:
Look no problem, under test:
PS: The article on the internet about swagger configuration most of the problems, so can not be copied directly to use. When you deploy swagger, you need to modify the configuration according to the actual project, such as spring, swagger version, and so on.
Using Swagger2 to build RESTful API documents in SPRINGMVC projects