美文网首页
Springboot整合swagger2

Springboot整合swagger2

作者: 吕小凯 | 来源:发表于2019-07-04 09:50 被阅读0次

导入依赖

 <!--引入swagger依赖-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.8.5</version>
        </dependency>

定义配置类(basePackage为待扫描的controller包)

package com.example.swagger.xk.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
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;

@Configuration
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.swagger.xk.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot利用swagger构建api文档")
                .description("简单优雅的restfun风格,http://blog.csdn.net/saytime")
                .termsOfServiceUrl("http://localhost:8080/")
                .version("1.0")
                .build();
    }
}

启动类配置swagger开启注解

package com.example.swagger;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
public class SwaggerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SwaggerApplication.class, args);
    }
}

定义controller测试接口

package com.example.swagger.xk.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api(value = "cont/worker", tags = {"用户信息测试"})
@RequestMapping("cont/worker")
@RestController
public class UserController {

    @ApiOperation(value = "添加用户", httpMethod = "POST")
    @PostMapping("/add")
    public Object query() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("data", "hello,world");
        map.put("status", true);
        return map;
    }
}

开启项目,浏览器输入http://localhost:8080/doc.html
如下所示即代表成功

image.png

相关文章

网友评论

      本文标题:Springboot整合swagger2

      本文链接:https://www.haomeiwen.com/subject/ckfshctx.html