But the words restful style service swagger2 is used to generate and direct calls
The following code is posted springboot integration swagger2
A, maven dependence
spring-boot version used here is 2.1.1.RELEASE, swagger2 using 2.9.2, I started using springboot1.5.6.RELEASE, swagger from 2.4 to 2.9.2 are used, the result has a lot jar package conflicts , springboot replaced 2. * version remains the same, there are still jar conflict, frustration, conflict can only rely on to find out, there are specific error message, exclude him out on the line, and the idea of maven-helper plug-in help also quite large
<!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> <exclusions> <exclusion> <artifactId>spring-context</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-beans</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-aop</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <!-- swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
Two, swagger2 configuration
package com.hy.other.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; importspringfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @ EnableSwagger2 public class SwaggerConfiguration { @Bean public Docket No. createRestApi () { return new new Docket No. (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ()) .Select () // generate the api package path (usually the path of our controller package) .apis (RequestHandlerSelectors.basePackage ( "com.hy.other.controller" )) .paths (PathSelectors.any ()) .build (); } // Swagger ui inside page Some information privateApiInfo ApiInfo () { return new new ApiInfoBuilder () // page title .title ( "the Boot Swagger the Spring" ) // founder .contact ( new new Business Card ( "HY", "https://www.cnblogs.com/xhy- Shine "," " )) // version .version (" 1.0 " ) // description .description (" API interface document " ) .build (); } }
Note : @ EnableSwagger2 must be added, or will be unable to access the swagger of ui interface, the following error will be reported
Third, the code (mainly swagger2 comment)
There are a lot of notes, more commonly used is @ Api, @ ApiOperation, @ ApiImplicitParam, @ ApiModel, @ ApiModelProperty
@Api: include all of the following interfaces, meaning a little class comment
@ApiOperation: to increase Interfaces
@ ApiImplicitParams, @ ApiImplicitParam: Interface parameters to add a description
@ApiModel: Description Object Request type parameter
@ApiModelProperty: description of object properties
Note: @ApiModelProperty annotated Parameter Description
paramType: where to put the designated parameter (header: request header using @RequestHeader acquired; query: Get used @RequestParam; path: obtaining using @PathVariable; body, form the two are not used) can refer to: HTTPS: // Swagger .io / docs / specification / describing- parameters /
name: parameter name
dataType: Parameter Type
required: Must
value: the meaning of the parameters
defaultValue: defaults
package com.hy.other.controller; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @Api(description = "MQ接口") @RestController @RequestMapping("/messageQueue") public class MessageQueueController { @Value("${rabbitmq.exchangeName}") private String exchangeName; @Value("${rabbitmq.queueName}") private String queueName; @Value("${rabbitmq.routeKey}") private String routeKey; @Autowired private RabbitTemplate rabbitTemplate; @ApiOperation(value = "send message to spin", notes = "发送消息到spin") @ApiImplicitParam(name = "message", value = "消息内容", required = true, paramType = "query") @RequestMapping("/sendWso2ToSpin") public String sendWso2ToSpin(@RequestParam("message") String message) { rabbitTemplate.convertAndSend(exchangeName, routeKey, message); return message; } }
Access address the root path of your project plus the swagger-ui.html on the line
1, swagger will decide to submit a form or application / json format (Parameter content type appears on the map as application / json is because I took notes before the request parameters into a @RequestBody) according to @ RequestParam, @ RequestBody comment
2、会根据@RequestMapping、@PostMapping、@GetMapping等注解生成对应的请求,如上,我写的@RequestMapping,他把所有请求格式都生成了
3、如果接收的是对象,可以使用@ApiModel结合@ApiModelProperty