一、添加Swagger2
依赖
- 在
spring-mybatis-sample-web
模块的pom.xml
文件中添加如下依赖:
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.7.0</version>
</dependency>
- 在
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
配置
- 在
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();}}
- 修改
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
注解
- 在实体类上添加注解
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
}
- 在接口上添加注解
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!
结果如下: