当前位置: 代码迷 >> 综合 >> Spring MVC 集成 Swagger2
  详细解决方案

Spring MVC 集成 Swagger2

热度:20   发布时间:2023-12-08 17:18:48.0

一、添加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 {
    @Beanpublic 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);@Autowiredprivate 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!
在这里插入图片描述
结果如下:
在这里插入图片描述

  相关解决方案