Spring Cloud Implements Swagger2

This is the 14th day of my participation in Gwen Challenge

Spring Cloud Implements Swagger2

The previous article continued to integrate Swagger with the business layer Module

Add swagger2 Maven dependency to business Module

<dependency><! Add Swagger dependency -->
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.9.2</version>
</dependency>
<dependency><! -- Add Swagger UI dependency -->
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.9.2</version>
</dependency>
<! , add the following configuration, Java. Lang. A NumberFormatException: For input string: "" problem Swagger2.9.2 a NumberFormatException - >
<dependency>
   <groupId>io.swagger</groupId>
   <artifactId>swagger-annotations</artifactId>
   <version>1.5.22</version>
</dependency>
<dependency>
   <groupId>io.swagger</groupId>
   <artifactId>swagger-models</artifactId>
   <version>1.5.22</version>
</dependency>
Copy the code

Add SwaggerConfig file to the business Module

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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/ * * *@ClassName: SwaggerConfig
 * @Description: Configure swagger in yML file configuration mode, class configuration swagger attribute is deprecated *@Author Gxin
 * @Date2021/6/26 * roar,@Version: 1.0 * * /
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi(a){
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                    .apis(RequestHandlerSelectors.basePackage("Package path where swagger related annotations are located"))
                    .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo(a) {
        return new ApiInfoBuilder().title("Swagger API")
                    .description("Interface Document")
                        .termsOfServiceUrl("Team Website")
                            .contact(new Contact("Author name".""."E-mail"))
                                .version("Version Number") .build(); }}Copy the code

Configure swagger web page parameters with application.yml file

swagger:
  base-package: com.ozx.ozxshopservicemember.controller # Swagger Package path where the associated annotation is located
  title: Springboot2.x builds an integrated Swagger2 project
  description: API documentation is generated automatically
  version: 1.1
  terms-of-service-url: 
  contact:
    name: Gxin
    email: email
    url: 

Copy the code

Add the Controller

package com.ozx.ozxshopservicemember.controller;
import com.alibaba.fastjson.JSONObject;
import com.ozx.ozxshopapimemberdto.input.UserInpDTO;
import com.ozx.ozxshopcommon.basic.BasicResponse;
import com.ozx.ozxshopserviceapimember.service.UserRegisterService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/ * * *@ClassName: UserResigterController
 * @Description: User registration module *@Author Gxin
 * @Date 2021/6/26 15:15
 * @Version: 1.0 * * /
@RestController
@RequestMapping("/mem")
@apiModel (" User registration module ")
public class UserResigterController {
    @Autowired
    private UserRegisterService userRegisterService;

    @PostMapping("/reg")
    @apiOperation (value = "register ",notes =" register interface ")
    @APIIMPLICITParams ({@apiIMPLICITParam (name = "regCode",paramType = "query",value = "verification code ",dataType = "String",example = "12345")})
    public BasicResponse<JSONObject> register(UserInpDTO userInpDTO,String regCode){
        returnuserRegisterService.register(userInpDTO, regCode); }}Copy the code