Generate Zero‑Annotation API Docs with smart‑doc for SpringBoot
This tutorial shows how to replace Swagger with smart-doc, a zero‑annotation API documentation tool for SpringBoot that generates HTML docs and Postman collections, covering Maven plugin setup, configuration files, custom tags, error‑code mapping, request‑header definitions, and step‑by‑step usage examples.
Front‑backend interface debugging requires API documentation; while Swagger is commonly used, smart-doc offers many advantages and is recommended.
Talking about Swagger
When using Swagger we often rely on annotations such as
@Apiand
@ApiOperationto generate API docs. Example code:
<code><!-- Example Swagger‑annotated controller method -->
// ...
</code>Swagger can be invasive, duplicating information in comments and annotations. smart-doc provides a zero‑annotation solution that generates documentation directly from code comments.
smart-doc Overview
smart-doc is an API documentation generator that requires only well‑written code comments to produce documentation and can also generate a Postman debug file for one‑click import.
Key advantages of smart-doc include:
Zero annotation intrusion
Automatic generation of Postman collection files
Support for custom tags and response wrappers
Generate API Documentation
First, add the smart-doc Maven plugin to the project. The plugin itself does not require additional dependencies, achieving true zero‑intrusion.
<code><plugin>
<groupId>com.github.shalousun</groupId>
<artifactId>smart-doc-maven-plugin</artifactId>
<version>2.2.8</version>
<configuration>
<!-- Path to smart‑doc configuration file -->
<configFile>./src/main/resources/smart-doc.json</configFile>
<!-- Project name -->
<projectName>mall-tiny-smart-doc</projectName>
</configuration>
</plugin>
</code>Next, create
smart-doc.jsonunder
src/main/resourceswith the following settings (comments omitted for brevity):
<code>{
"serverUrl": "http://localhost:8088",
"outPath": "src/main/resources/static/doc",
"isStrict": false,
"allInOne": true,
"createDebugPage": false,
"packageFilters": "com.macro.mall.tiny.controller.*",
"style": "xt256",
"projectName": "mall-tiny-smart-doc",
"showAuthor": false,
"allInOneDocFileName": "index.html"
}
</code>Run the Maven goal
smart-doc:htmlfrom the IDEA Maven panel. The generated documentation appears under
static/doc, including detailed request parameters and response results, accessible at
http://localhost:8088/doc/index.html.
Entity classes need only field comments; smart-doc extracts them automatically. Example entity:
<code>public class PmsBrand implements Serializable {
/** ID */
private Long id;
/** Name */
private String name;
/** First letter */
private String firstLetter;
// ... other fields and getters/setters omitted
}
</code>Controller methods also require only comments. Example controller method:
<code>/**
* Paginated brand list
* @param pageNum page number
* @param pageSize page size
*/
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
@PreAuthorize("hasRole('ADMIN')")
public CommonResult<CommonPage<PmsBrand>> listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
List<PmsBrand> brandList = brandService.listBrand(pageNum, pageSize);
return CommonResult.success(CommonPage.restPage(brandList));
}
</code>smart-doc also supports custom annotation tags to enhance documentation:
@ignore: exclude the field from the generated doc
@required: mark a request parameter as mandatory
@since: indicate the version when the field was added
To unify response structures, add the following to
smart-doc.json:
<code>{
"responseBodyAdvice": {
"className": "com.macro.mall.tiny.common.api.CommonResult"
}
}
</code>Configure error‑code dictionaries:
<code>{
"errorCodeDictionaries": [{
"title": "title",
"enumClassName": "com.macro.mall.tiny.common.api.ResultCode",
"codeField": "code",
"descField": "message"
}]
}
</code>Define custom request headers (e.g.,
Authorization) in
smart-doc.json:
<code>{
"requestHeaders": [{
"name": "Authorization",
"type": "string",
"desc": "token request header value",
"value": "token request header value",
"required": false,
"since": "-",
"pathPatterns": "/brand/**",
"excludePathPatterns": "/admin/login"
}]
}
</code>Using Postman to Test Interfaces
smart-doc’s built‑in interface testing is limited, so it provides a Postman JSON generator. Execute smart-doc:postman to create postman.json in static/doc , then import the file into Postman for full testing.
After importing, all generated interfaces appear in Postman, enabling convenient testing.
Conclusion
smart-doc is a practical API documentation tool with zero‑annotation intrusion. Although its native testing features are weak, the ability to generate Postman collections makes it highly convenient for developers.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.