*/
// @Override
// public void addResourceHandlers(ResourceHandlerRegistry registry) {
// registry.addResourceHandler(“swagger-ui.html”)
// .addResourceLocations(“classpath:/META-INF/resources/”);
//
// registry.addResourceHandler(“/webjars/**”)
// .addResourceLocations(“classpath:/META-INF/resources/webjars/”);
// }
/**
-
可以定义多个组,比如本类中定义把test和demo区分开了
-
(访问页面就可以看到效果了)
*/
@Bean
public Docket testApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName(“test”)
.genericModelSubstitutes(DeferredResult.class)
// .genericModelSubstitutes(ResponseEntity.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(true)
.pathMapping(“/”)// base,最终调用接口后会和paths拼接在一起
.select()
.paths(or(regex(“/api/.*”)))//过滤的接口
.build()
.apiInfo(testApiInfo());
}
@Bean
public Docket demoApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName(“demo”)
.genericModelSubstitutes(DeferredResult.class)
// .genericModelSubstitutes(ResponseEntity.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(false)
.pathMapping(“/”)
.select()
.paths(or(regex(“/demo/.*”)))//过滤的接口
.build()
.apiInfo(demoApiInfo());
}
private ApiInfo testApiInfo() {
ApiInfo apiInfo = new ApiInfo(“Electronic Health Record(EHR) Platform API”,//大标题
“EHR Platform’s REST API, all the applications could access the Object model data via JSON.”,//小标题
“0.1”,//版本
“NO terms of service”,
“365384722@qq.com”,//作者
“The Apache License, Version 2.0”,//链接显示文字
“http://www.apache.org/licenses/LICENSE-2.0.html”//网站链接
);
return apiInfo;
}
private ApiInfo demoApiInfo() {
ApiInfo apiInfo = new ApiInfo(“Electronic Health Record(EHR) Platform API”,//大标题
“EHR Platform’s REST API, for system administrator”,//小标题
“1.0”,//版本
“NO terms of service”,
“365384722@qq.com”,//作者
“The Apache License, Version 2.0”,//链接显示文字
“http://www.apache.org/licenses/LICENSE-2.0.html”//网站链接
);
return apiInfo;
}
}
经过这2步配置后,我们启动服务后,访问:http://localhost:8080/swagger-ui.html 就完成了集成。
下面创建2个Controller来测试:
1、TestController.Java
@Controller
@RequestMapping(“/api/test”)
public class TestController {
@ResponseBody
@RequestMapping(value = “/show”, method=RequestMethod.POST)// 这里指定RequestMethod,如果不指定Swagger会把所有RequestMethod都输出,在实际应用中,具体指定请求类型也使接口更为严谨。
@ApiOperation(value=“测试接口”, notes=“测试接口详细描述”)
public String show(
@ApiParam(required=true, name=“name”, value=“姓名”)
@RequestParam(name = “name”) String stuName){
return “success”;
}
}
2、DemoController.java
/**
-
DemoController
*/
@Controller
@RequestMapping(value = “/demo”)
public class DemoController {
private Logger logger = LoggerFactory.getLogger(DemoController.class);
/**
-
可以直接使用@ResponseBody响应JSON
-
@param request
-
@param response
-
@return
*/
@ResponseBody
@RequestMapping(value = “/getcount”, method = RequestMethod.POST)
@ApiOperation(value=“测试-getCount”, notes=“getCount更多说明”)
public ModelMap getCount(HttpServletRequest request,
HttpServletResponse response) {
logger.info(“>>>>>>>> begin getCount >>>>>>>>”);
ModelMap map = new ModelMap();
map.addAttribute(“count”, 158);
// 后台获取的国际化信息
map.addAttribute(“xstest”, “测试”);
return map;
}
/**
-
可以直接使用@ResponseBody响应JSON
-
@param request
-
@param response
-
@return
*/
@ApiIgnore//使用该注解忽略这个API
@ResponseBody
@RequestMapping(value = “/jsonTest1”, method = RequestMethod.POST)
public ModelMap jsonTest(HttpServletRequest request,
HttpServletResponse response) {
ModelMap map = new ModelMap();
map.addAttribute(“hello”, “你好”);
map.addAttribute(“veryGood”, “很好”);
return map;
}
/**
-
可以直接使用@ResponseBody响应JSON
-
@param request
-
@param response
-
@return
*/
@ResponseBody
@RequestMapping(value = “/jsonTest3”, method = RequestMethod.POST)
public List jsonTest3(HttpServletRequest request,
HttpServletResponse response) {
List list = new ArrayList();
list.add(“hello”);
list.add(“你好”);
return list;
}
/**
-
JSON请求一个对象
-
(Ajax Post Data:{“name”:“名称”,“content”:“内容”})
-
@param version
-
@return
*/
@ResponseBody
@RequestMapping(value = “/jsonTest2”, method = RequestMethod.POST)
public ModelMap jsonTest2(@RequestBody Demo demo) {
logger.info(“demoName:” + demo.getName());
logger.info(“demoContent:” + demo.getContent());
ModelMap map = new ModelMap();
map.addAttribute(“result”, “ok”);
return map;
}
/**
-
直接读取URL参数值
-
/demo/jsonTest6.do?name=Hello&content=World
-
@param demoName
-
@param content
-
@return
*/
@ResponseBody
@RequestMapping(value = “/jsonTest6”, method = RequestMethod.POST)
public ModelMap jsonTest6(@RequestParam(“name”) String demoName, @RequestParam String content) {
logger.info(“demoName:” + demoName);
ModelMap map = new ModelMap();
map.addAttribute(“name”,demoName + “AAA”);
map.addAttribute(“content”,content + “BBB”);
map.addAttribute(“date”,new java.util.Date());
return map;
}
/**
-
JSON请求一个对象,将RequestBody自动转换为JSONObject对象
-
(Ajax Post Data:{“name”:“名称”,“content”:“内容”})
-
使用JSONObject请添加依赖
-
<groupId>net.sf.json-lib</groupId>
-
<artifactId>json-lib</artifactId>
-
<version>2.4</version>
-
<!--指定jdk版本 -->
-
<classifier>jdk15</classifier>
-
@param version
-
@return
*/
@ResponseBody
@RequestMapping(value = “/jsonTest5”, method = RequestMethod.POST)
public ModelMap jsonTest5(@RequestBody JSONObject jsonObject) {
String name = jsonObject.getString(“name”);
logger.info(“demoName:” + name);
ModelMap map = new ModelMap();
map.addAttribute(“demoName”,name);
return map;
}
/**
-
输入 和输出为JSON格式的数据的方式 HttpEntity<?> ResponseEntity<?>
-
@param u
-
@return
*/
@ResponseBody
@RequestMapping(value = “/jsonTest4”, method = RequestMethod.POST)
public ResponseEntity jsonTest4(HttpEntity demo,
HttpServletRequest request, HttpSession session) {
//获取Headers方法
HttpHeaders headers = demo.getHeaders();
// 获取内容
String demoContent = demo.getBody().getContent();
// 这里直接new一个对象(HttpHeaders headers = new HttpHeaders();)
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add(“MyHeaderName”, “SHANHY”);
ResponseEntity responseResult = new ResponseEntity(