【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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值