ssm 就不不说了。swagger 是当前在比较流行的REST APIs文档生成工具。无论是给手机端还是作为第三方提供接口都是非常不错的。在线调试用起来也超方便。说下整合的步骤
1.引入jar 包
swagger-annotations-1.3.13.jar
swagger-models-1.0.2.jar
swagger-springmvc-1.0.2.jar
2.建立一个Swaggerconfig的配置文件
**
* SwaggerUI配置
*/
@Configuration
@EnableSwagger
@EnableWebMvc
@ComponentScan(basePackages ={"com.thinkgem.jeesite.swagger"}) //制定扫描的controller包路劲
public class SwaggerConfig extends WebMvcConfigurerAdapter{
private SpringSwaggerConfig springSwaggerConfig;
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
{
this.springSwaggerConfig = springSwaggerConfig;
}
/**
* Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
* framework - allowing for multiple swagger groups i.e. same code base
* multiple swagger resource listings.
*/
@Bean
public SwaggerSpringMvcPlugin customImplementation()
{
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
.apiInfo(apiInfo())
.includePatterns(".*")
.swaggerGroup("XmPlatform")
.apiVersion("1.0.0");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
/*
* "标题 title",
* "描述 description",
* "termsOfServiceUrl",
* "联系邮箱 contact email",
* "许可证的类型 license type",
* "许可证的链接 license url"
*/
private ApiInfo apiInfo()
{
ApiInfo apiInfo = new ApiInfo(
"金融平台API文档",
"详细的后台所有Restful接口",
"",
"联系作者李雅强@qq.com",
"联系作者李雅强",
"联系作者李雅强");
return apiInfo;
}
}
3.书写测试的controller
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mangofactory.swagger.models.dto.Model;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
@Controller
@RequestMapping(value = "/test")
@Api(value="TestssController",description="测试接口描述")
public class TestssController {
@Autowired
private SystemService systemService;
/*
* @ApiOperation(value = "接口说明", httpMethod ="接口请求方式", response ="接口返回参数类型", notes ="接口发布说明"
* @ApiParam(required = "是否必须参数", name ="参数名称", value ="参数具体描述"
*/
@RequestMapping(value = {""})
@ApiOperation(value="接口说明(测试)",httpMethod="GET",notes="在没有会话、没有签名的情况下,进入方法体")
public void test(HttpServletRequest request, HttpServletResponse response, Model model) {
try {
response.getWriter().write("ignoreAll");
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping(value="/user/{id}",method=RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "根据用户名获取用户对象", httpMethod = "GET", response = String.class, notes = "根据用户名获取用户对象")
public void get(@PathVariable Integer id,PrintWriter pw){
System.out.println("get"+id);
//return "/hello";
pw.print("hello:"+id);
}
@RequestMapping(value = "/EFP0045")
@ResponseBody
@ApiOperation(value="根据ID获取用户信息",httpMethod="GET",notes="get user by id",response=User.class)
public User getUser(@ApiParam(required=true,value="用户ID",name="userId")@RequestParam(value="userId")String userId) {
return systemService.getUser(userId);
}
}
4.去Swagger 的官网下载Swagger-ui 将dist 下的内容拷贝到一个新建的文件夹swagger
5.修改swagger 文件夹下的index.html
6.访问项目路径下的index.html
需要注意的是在项目的swagger-ui.js 中需要取出rest 这个,不让项目请求会求不到