Spring MVC 集成 Swagger2

本文介绍了如何将Swagger2与Spring MVC集成,包括添加相关依赖、配置、注解使用,以及如何查看和测试API的效果。通过集成Swagger2,可以方便地进行API文档的管理和测试。
摘要由CSDN通过智能技术生成

一、添加Swagger2依赖

  1. spring-mybatis-sample-web模块的pom.xml文件中添加如下依赖:
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>
  1. spring-mybatis-sample-war模块的pom.xml文件中添加如下依赖:
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.7.0</version>
</dependency>

springfox-swagger-ui 下都是静态资源,故其依赖放在spring-mybatis-sample-war模块下
在这里插入图片描述
二、添加swagger配置

  1. spring-mybatis-sample-web模块中新增swagger配置类SwaggerConfiguration.java:
package spring.mybatis.sample.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Created by liuquan on 2019/7/7.
 */
@EnableSwagger2
@Configuration
public class SwaggerConfiguration extends WebMvcConfigurationSupport {


    @Bean
    public ApiInfo apiInfo(){
        return new ApiInfoBuilder().title("接口").version("1.0.0").build();
    }

}
  1. 修改spring-servlet.xml文件指定静态文件路径:
    <mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/>
    <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>

三、添加Swagger注解

  1. 在实体类上添加注解
package spring.mybatis.sample.basic.user.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;

/**
 * Created by liuquan on 2019/3/19.
 */
@ApiModel(value = "UserInfo:用户信息")
public class UserVO implements Serializable{

    private static final long serialVersionUID = 7641683836755427868L;

    @ApiModelProperty(value = "ID")
    private String id;
    @ApiModelProperty(value = "用户名称")
    private String userName;
    @ApiModelProperty(value = "用户年龄")
    private Integer age;
    @ApiModelProperty(value = "用户性别")
    private Integer sex;
    @ApiModelProperty(value = "用户地址")
    private String address;
    @ApiModelProperty(value = "用户出生日期")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday;
	// setter 、 getter
}
  1. 在接口上添加注解
package spring.mybatis.sample.basic.user.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import spring.mybatis.sample.basic.user.bo.UserBO;
import spring.mybatis.sample.basic.user.service.UserService;
import spring.mybatis.sample.basic.user.vo.UserVO;
import spring.mybatis.sample.common.Response;
import spring.mybatis.sample.utils.BeanCopyUtils;

/**
 * Created by liuquan on 2019/3/19.
 */
@Api(tags = {"用户信息维护"})
@Controller
@RequestMapping("/userController")
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @ApiOperation(value="查询用户明细信息", httpMethod = "POST", tags = "查询用户信息")
    @ResponseBody
    @RequestMapping("/query-user-detail.json")
    public UserBO queryUserDetail(UserVO userVO){

        LOGGER.info("=======================UserController.queryUserDetail=======================");
        UserBO userBO = BeanCopyUtils.copyProperties(new UserBO(), userVO);
        UserBO newUserBO = userService.queryOneUser(userBO);
        return newUserBO;
    }
}

四、查看效果
启动项目,访问地址:http://localhost:8080/swagger-ui.html,效果如图:
在这里插入图片描述
点击URL链接展示如下内容
在这里插入图片描述
输入id的值,点击Try it out!
在这里插入图片描述
结果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值