Spring boot - Integrated Swagger2

Recently the company project uses an API interface test tools Swagger, before seemingly no contact, interface testing tool postman to get started, so I quickly catching up here ......

1. Add required depend Swagger2:

<!-- 添加Spring boot Swagger2 依赖 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
</dependency>

2. Add a project SwaggerConfig categories:

package com.xianwen.sp.springjtool.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.swagger.annotations.ApiOperation;
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;

/**
 * @author : zhoux
 * 
                .apis (RequestHandlerSelectors.withMethodAnnotation (ApiOperation.class))
 Project for the this Swagger2 the Configuration *. 
 *   
 * / 
@Configuration 
@ EnableSwagger2 
public class SwaggerConfig { 
    
    @Bean 
    public Docket No. creatRestApi () { 
        return new new Docket No. (DocumentationType.SWAGGER_2) 
                .groupName ( "Spring jtool") 
                .apiInfo (getApiInfo ()) 
                . SELECT () 
                 // all methods of all classes of a labeled @Api basePackage will be provided as package API 
                .apis (RequestHandlerSelectors.basePackage ( "com.xianwen.sp.springjtool.controller")) 
                // only tagged @ApiOperation the method will be exposed to Swagger 
                .paths (PathSelectors.any ()) // PathSelectors.regex ( "/ API /.*") 
                .build ();
    }
    
    private ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
                .title("API Document")
                .description("Swagger2 API Document")
                //.contact(new Contact("zhoux", "http://localhost/swagger-ui.html", "[email protected]")) 
                .version("1.0.0")
                .build();
    }

}

3. In the controller @Api (tag = "*****") annotation tag, the interface to be tested using the method @ApiOperation ( "*****") annotation tag:

package com.xianwen.sp.springjtool.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xianwen.sp.springjtool.entity.User;
import com.xianwen.sp.springjtool.service.impl.UserServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**   
 * @ClassName:  UserController   
 * @Description:  the controller for user.
 * 
 * @author: zhoux 
 * 
 * @date:  2019-9-10  10:10:59  
 *     
 */
@Api(description = "用户管理 - 用户数据")
@RestController
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    private UserServiceImpl mUserServiceImpl;
    
    @ApiOperation("获取所有的用户信息")
    @PostMapping(value = "/findUser", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<User>
    @ApiOperation ( "The user information acquisition id")
    }findAllUser () {
        return mUserServiceImpl.findAllUser();
    
    @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<User> findAllById(@PathVariable("id") Long id) {
        return mUserServiceImpl.findById(id);
    }
    
    @ApiOperation("根据name获取用户信息")
    @GetMapping(value = "/{name}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<User> findAllByName(@PathVariable("name") String name) {
        return mUserServiceImpl.findByName(name);
    }

}

Guess you like

Origin www.cnblogs.com/zhoux955792/p/11517382.html