【简单到离谱而又十分实用的测试工具】SSM框架轻松整合Swagger
程序员文章站
2022-07-10 10:41:04
...
首先要在pom.xml中导入swagger的坐标依赖:
<!-- swagger整合SSM start -->
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<!-- swagger整合SSM end -->
在applicationContext.xml文件中注入swagger:
<!-- 配置swagger -->
<bean class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration" id="swagger2Config"/>
在SpringMVC.xml文件中引入swagger相关的文件:
<!-- 配置swagger -->
<!-- 引入swagger相关的配置文件 -->
<bean class="com.ysw.utils.MySwaggerConfig"/>
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
编写MySwaggerConfig.java配置文件:
package com.ysw.utils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* swagger配置类
*/
@Configuration
@EnableSwagger2 //Loads the spring beans required by the framework
public class MySwaggerConfig {
@Bean
public Docket userApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select() // 选择那些路径和api会生成document
.apis(RequestHandlerSelectors.any()) // 对所有api进行监控
.paths(PathSelectors.any()) // 对所有路径进行监控
.build();
}
@Bean
public RequestMappingInfoHandlerMapping requestMapping(){
return new RequestMappingHandlerMapping();
}
}
直接在controller层调用swagger注解 @Api(value = "restful", description = "测试") 和 @ApiOperation(value = "测试专用") 进行测试:
package com.ysw.controller;
import com.wordnik.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import com.ysw.domain.User;
import com.ysw.service.UserService;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
@RequestMapping("/userController")
@Api(value = "restful", description = "测试") //swagger接口配置
public class UserController {
@Autowired
private UserService userService;
/**
* 登录方法
*
* @param user
*/
@ApiOperation(value = "测试专用")
@RequestMapping("/userLogin")
@ResponseBody
public String userLogin(User user){
System.out.println(userService.login(user));
//判断是否登录成功:true-成功 / false-失败
if (userService.login(user) != null) {
//登录成功之后
return "success";
} else {
//登录失败之后
return "fail";
}
}
}
运行Tomcat,大功告成!进行测试!输入地址:http://端口号/项目名/swagger-ui.html#/
这里七种测试方法分别对应七种请求方式:
这里我想测试登录方法,只需要输入username和password即可调用,选中我用的PUT方式进行测试:
最后测试成功,响应的内容如下图所示:
下一篇: Maven+IDEA配置SSM项目