【Java快速入门】-- SpringBoot集成swagger接口文档

依赖

<!-- swagger接口文档 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>

Swagger配置

package com.xxxx.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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 依赖
 * <!-- swagger接口文档 -->
 * <dependency>
 * <groupId>io.springfox</groupId>
 * <artifactId>springfox-swagger2</artifactId>
 * <version>3.0.0</version>
 * </dependency>
 * <dependency>
 * <groupId>io.springfox</groupId>
 * <artifactId>springfox-swagger-ui</artifactId>
 * <version>3.0.0</version>
 * </dependency>
 * <dependency>
 * <groupId>io.springfox</groupId>
 * <artifactId>springfox-boot-starter</artifactId>
 * <version>3.0.0</version>
 * </dependency>
 */

// 配置接口文档,以下为固定写法
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    
    
    private ApiInfo apiInfo() {
    
    
        return new ApiInfoBuilder().title("API接口文档")
                .description("使用swagger来测试接口文档")
                .version("1.0.0")
                .build();
    }

    @Bean
    public Docket createRestApi() {
    
    
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xxxx")) // 这里写的是API接口所在的包位置

                .paths(PathSelectors.any())
                .build();
    }
}

接口配置

package com.xxxx.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import com.xxxx.utils.ResultResponse; // 该类用户自定义返回结构

// 定义一个接口
@RestController // 声明是一个javaBean对象
@Api(value = "Hello接口") // swagger 配置
public class HelloController {
    
    
    // @Value("${info.version}")
    // private String version;

    /**
     * @Autowired // 自动注解
     */
    @Autowired
    private ResultResponse rresp;

    /**
     * 
     * @RequestMapping // 设置路由
     * @RequestParam // 这是请求参数,并附默认值
     */
    @RequestMapping(method = RequestMethod.POST, value = "/hello2/{id}")
    @ApiOperation(value = "测试post请求", notes = "测试post请求") // swagger 配置
    public ResultResponse hello2(
            @RequestParam(defaultValue = "jack", name = "name") String name,
            @RequestPart("file") MultipartFile file,
            @PathVariable(value = "id") Integer id,
            @RequestParam(name = "age") Integer age) {
    
    

        Map<String, Object> m = new HashMap<String, Object>();
        // m.put("name", name);
        m.put("age", age);
        m.put("id", id);
        rresp.setCode(200);
        rresp.setMsg("成功");
        rresp.setData(m);
        System.out.println(rresp);
        return rresp;
    }
}

猜你喜欢

转载自blog.csdn.net/xzpdxz/article/details/123531493