swagger集成及使用

2019-04-02  本文已影响0人  毛豆豆豆豆子

Swagger配置

maven引入Swagger依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.4.0</version>
        </dependency>

创建swagger配置类

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {
}

Tips:一定要放到能被包扫描的位置,swagger会从当前Swagger2.java的位置往下找带有@controller注解的类,进行生成文档

配置完成启动项目访问

导入jackson依赖

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.7</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.7</version>
        </dependency>

再次启动项目

image.png

新建Conroller试试Swagger效果

@Controller
public class DemoController {

    @ResponseBody
    @RequestMapping("demo")
    public Object demo(){
        Map<String,String> map = new HashMap();
        map.put("name","张飞");
        return map;
    }
}
我们给接口设置只能GET请求访问(效果图如下)
image.png
访问接口
image.png image.png

Swagger常用的注解

注解 位置
@Api(value = "测试Controller",tags = "测试接口") 类上
@ApiOperation(value = "演示接口",notes="详细介绍") 接口上
@ApiParam(name="性别",value="男,女",required=true) 参数
@Api(value = "测试Controller",tags = "测试接口")
@Controller
public class DemoController {

    @ApiOperation(value = "演示接口",notes="详细介绍")
    @ResponseBody
    @RequestMapping(value = "/demo",method = RequestMethod.GET)
    public Object demo(
            @RequestParam(value = "name") String name,
            @RequestParam(value = "age") Integer age,
            @ApiParam(name="性别",value="男,女",required=true) String sex

    ){
        Map<String,Object> map = new HashMap();
        map.put("name",name);
        map.put("age",age);
        map.put("sex",sex);
        return map;
    }
}
以上面的为例看看效果图
image.png
@Api(value = "测试Controller",tags = "测试接口")
@Controller
public class DemoController {

    @ApiOperation(value = "演示接口",notes="详细介绍")
    @ResponseBody
    @RequestMapping(value = "/demo",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "说明", paramType = "query",required = true),
            @ApiImplicitParam(name = "age", value = "说明", paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "sex", value = "说明", paramType = "query",required = true)
    })
    public Object demo(
            @RequestParam(value = "name") String name,
            @RequestParam(value = "age") Integer age,
            @RequestParam(value = "sex") String sex
    ){
        Map<String,Object> map = new HashMap();
        map.put("name",name);
        map.put("age",age);
        map.put("sex",sex);
        return map;
    }
}
image.png
上一篇下一篇

猜你喜欢

热点阅读