easy rest doc for Spring boot. 依赖spring AOP 发现 RestController
注解的 beans, 对其 RequestMapping
注解的 methods 进行解析, 生成 markdown 和 html 格式的文档.
使用@EnableRestDocs
注解启用自动生成, 包含name
/version
/usage
三个属性, 生成在文档最前部分总体说明
可用于单独指定method的usage内容
- name 方法名
- requestExampleClass 如果不为
Void.class
(默认值)则实例化并生成pretty json string作为接口请求body的样例 - requestExampleText 如果
requestExampleClass
不存在,使用该属性作为接口请求body的样例 - responseExampleClass 如果不为
Void.class
(默认值)则实例化并生成pretty json string作为接口输出样例 - responseExampleText 如果
responseExampleClass
不存在,使用该属性作为接口输出样例 - scopes 接口所属scope
- usage 用途说明
- errors (
@RestError
) 错误描述数组
- code 错误码
- message 错误说明
- 跳过
HttpServletRequest
/HttpServletRequest
等 - 识别
RequestParam
注解的name
/required
等属性
- name 参数名, 作为
@RequestParam.name
的fallback - description 参数作用描述
- 只扫描@RestController注解的类, 如果存在@RequestMapping并设置path, 会叠加到内部methods的uris
@RestController
@RequestMapping(value = { "/1", "/2" })
public class DemoRestApi {}
- 生成response样例, 定义样例类, 通过
@RestMethod
注解指定
static public class ResponseExample {
int code;
String message;
public String getMessage() {
return message;
}
}
@RestMethod(responseExampleClass = ResponseExample.class
errors = {
@RestError(code = -1, message = "bad")
})
@RestDoc(usage = "api used to say hello to someone")
@RequestMapping(value = "/hello", method = {RequestMethod.GET})
public Object get(
@RestParam @RequestParam(name = "who", defaultValue = "") String who
) {
return "hello," + who;
}
- 生成response样例, 需要
@RequestBody
和@RestMethod.requestExampleClass
同时具备
- 可通过 ${restdocs.base} 配置, 默认为 "/docs.md"
- 在访问路径后加 /html 能够看到 http://strapdownjs.com/ 渲染之后的html
- 支持
@PathVariable
支持 scopes- 更多完善