Spring cloud (5): the Integration of Swagger2
1. Concept
Swagger2-Service interface document, used for front-end and back-end contact, improve communication between projects, more efficient completion of joint adjustment between projects.
2. Integration
2.1.Import swagger through pom file
Swagger-springmvc
Swagger-springmvc
2.2.Building swagger module and writing module class
@ Configuration
@ EnableWebMvc
@ EnableSwagger
@ ComponentScan (basePackages= {"com.sz.xx"})
Public class ApiSwaggerConfig extends WebMvcConfigurerAdapter {
Private SpringSwaggerConfig springSwaggerConfig
@ Autowired
Public void setSpringSwaggerConfig (SpringSwaggerConfig springSwaggerConfig)
{
This.springSwaggerConfig = springSwaggerConfig
}
@ Bean
Public SwaggerSpringMvcPlugin customImplementation ()
{
Return new SwaggerSpringMvcPlugin (this.springSwaggerConfig) .apiInfo (apiInfo ()) .origindePatterns (new String [] {"/ xx/.*?"})
.apiVersion ("0.0.1")
.swagger Group ("api")
}
Private ApiInfo apiInfo () {
ApiInfo apiInfo = new ApiInfo ("XX API Interface document", "API-DOC", "test@163.com", "My License", "My Apps API License URL")
Return apiInfo
}
Public void configureDefaultServletHandling (DefaultServletHandlerConfigurer configurer)
{
Configurer.enable ()
}
Class GtPaths extends SwaggerPathProvider {
GtPaths () {
}
Protected String applicationPath () {
Return "/ restapi"
}
Protected String getDocumentationPath ()
{
Return "/ restapi"
}
}
}
}
3. Use swagger in the code
3.1 、 UserContoller
Api (value= "userApi", description= "user Management Interface", position=1)
@ Controller
@ ApiService
Public class UserContoller {
@ ApiOperation (notes= "new user interface", value= "new user (developer responsible person: xx)")
@ ApiServiceOperation ("New user Interface")
@ RequestMapping (value= {"/ xx/user/add"}, method= {RequestMethod.POST})
@ ResponseBody
Public Response createSoftsimBinding (@ RequestBody SoftsimBindingCreateReq req)
{
}
4. Import swagger-related ui into the project, and you can create a static folder to store swagger ui files.
5. Access via address http://localhost:8020/swagger/index.html