在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Getting StartedSupported JDK:1.8+ Quick StartStep One:Add Dependencymaven: <dependency> <groupId>io.github.yedaxia</groupId> <artifactId>japidocs</artifactId> <version>1.4.4</version></dependency> gradle: compile 'io.github.yedaxia:japidocs:1.4.4' Step Two:ConfigurationYou can run code below at any main(): DocsConfig config = new DocsConfig();config.setProjectPath("your springboot project path"); // root project path config.setProjectName("ProjectName"); // project nameconfig.setApiVersion("V1.0"); // api versionconfig.setDocsPath("your api docs path"); // api docs target pathconfig.setAutoGenerate(Boolean.TRUE); // auto generate Docs.buildHtmlDocs(config); // execute to generate If there is no accident, after executing the above code, you can see the generated documents in the configured directory. Code Style RequirementsJApiDocs is implemented by parsing Java source code. To make JApiDocs work correctly, you need to follow certain coding standards in the writing of You can refer the 1. Add Necessary Code CommentsThe class comment will correspond to the first-level grouping. You can also specify the group name through /** * User API */@RequestMapping("/api/user/")@RestControllerpublic class UserController { /** * Get User List * @param listForm */ @RequestMapping(path = "list", method = {RequestMethod.GET, RequestMethod.POST} ) public ApiResult<PageResult<UserVO>> list(UserListForm listForm){ return null; } /** * Save User * @param userForm */ @PostMapping(path = "save") public ApiResult<UserVO> saveUser(@RequestBody UserForm userForm){ return null; } /** * Delete User * @param userId user id */ @PostMapping("delete") public ApiResult deleteUser(@RequestParam Long userId){ return null; }} If the submitted form is // in @param@param userId user id // at FormBeanpublic class UserListForm extends PageForm{ private Integer status; //user status private String name; //user name} This form type would show as table in the document:
If the submitted form is { "id": "long //user id", "name": "string //user name", "phone": "long // user phone", "avatar": "string // user avatar url", "gender": "byte //use gender"} 2. Return Specific Class TypeWe know that if a Controller Class declares @RestController, SpringBoot will return json data to the front end. JApiDocs also uses this feature to parse the result in the api method, but since JApiDocs parses the source code statically, you must clearly return a specific class type. JApiDocs supports complex class analysis such as inheritance, generics, and loop nesting. Such as /** * save user * @param userForm */@PostMapping(path = "save")public ApiResult<UserVO> saveUser(@RequestBody UserForm userForm){ return null;}
{ "code": "int", "errMsg": "string", "data": { "userId": "string //user id", "userName": "string //user name", "friends": [ { "userId": "string //user id", "userName": "string //user name" } ], "readBooks": [ { "bookId": "long //book id", "bookName": "string //book name" } ], "isFollow": "boolean //is follow" }} If you don't like the return type, you can also use 3. Api Java Beans Should In Source CodeWe know that there is no comment information in the compiled class bytecode. For JApiDcos to work better,your Form bean Class and return Class should be in the source code, otherwise the generated document will be missing description information.Anyway, in version 1.4.2, JApiDocs will try to parse the field information by reflection when can't find the source code (the dependent class is in the jar package). Advanced Configuration@ApiDocBy default, JApiDocs only exports the api that declares If you don't want to export all apis, you can turn off the Let's see how the
ex: @ApiDoc(result = AdminVO.class, url = "/api/v1/admin/login2", method = "post") @IgnoreIgnore Controller ClassAdd @Ignorepublic class UserController { } Ignore Method@Ignore@PostMapping("save")public ApiResult saveUser(){ return null;} Ignore FieldIf you don’t want to export a field in the object, you can add ex: public class UserForm{ @Ignore private Byte gender;} Export More FormatExport Markdownconfig.addPlugin(new MarkdownDocPlugin()); Export Pdf Or WordYou can use pandoc convert markdown to pdf or word. Custom Code TemplatesIn addition to supporting api document export, JApiDocs currently also supports the generation of return object codes for Android and iOS, corresponding to Java and Object-C languages,If you want to modify the code template, you can use the following steps: Step One:Modify Code TemplatesCopy the code templates in the Step Two:Use The New Code TemplatesUse docsConfig.setResourcePath("your new tempalte path"); More Custom FeaturesJApiDocs provides a plug-in interface. You can implement more rich features through the plug-in interface.The following describes how to make this: Step One:Implements IPluginSupport Interfacepublic class CustomPlugin implements IPluginSupport{ @Override public void execute(List<ControllerNode> controllerNodeList){ // do something you want }} Step Two:Add Your Plugin config.addPlugin(new CustomPlugin()); |
请发表评论