spring boot部署swagger及其中常用注解效果演示

1,578 阅读2分钟

部署过程

1.导入依赖

修改pom.xml,在其中加入以下依赖;

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

2.创建controller层、实体类

因为只是简单的demo,而swagger只注重controller层的请求与响应,因此本文中午service层与dao层。

实体类:

public class User {

    private int id;
    private String name;

    public User(String name) {
        this.name = name;
    }

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public User() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

controller:

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping(value = "/{name}",method = RequestMethod.POST)
    public JSONObject insert(@PathVariable("name") String name)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",200);
        jsonObject.put("msg","insert");
        return jsonObject;
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public JSONObject delete(@PathVariable("id") int id)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",200);
        jsonObject.put("msg","delete "+id);
        return jsonObject;
    }

    @RequestMapping(value = "/{id}/{name}",method = RequestMethod.PUT)
    public JSONObject update(@PathVariable("id") int id,@PathVariable("name") String name)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",200);
        jsonObject.put("msg","update");
        return jsonObject;
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONObject get(@PathVariable("id") int id)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",200);
        jsonObject.put("msg","get");
        return jsonObject;
    }

}

3.创建swagger配置文件

在项目的启动类的同级文件建立swagger的配置文件

@Configuration
@EnableSwagger2
public class Swagger2 {
    //swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }


    /**
    *
    *@Author 
    *@Description
    *@Date
    *
    */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("Spring Boot 测试使用 Swagger2 构建RESTful API")
                //创建人
                .contact(new Contact("wyp", "QQ.com", "Wypeng19971214@163.com"))
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }
}

4.修改application.yml【作用等价于application.properties】

spring:
  mvc:
    static-path-pattern: /**
  resources:
    static-locations: classpath:/META-INF/resources/

打开浏览器,输入http://localhost:8080/swagger-ui.html,就可以看到效果了。

常用注解效果演示

swagger中常用的注解有

@Api 注解在controller类上 @ApiModel 注解在类上,一般是实体类 @ApiOperation 注解在方法上,表明方法级解释 @ApiImplicitParams 注解在方法上,一般与@ApiImplicitParam共用,多个参数逗号隔开,表请求参数 @ApiResponses 注解在方法上表响应,一般与@ApiResponse公用

@API

@ApiOperation