SpringBoot Generates JavaDoc

 1. Introduction      

With the development of microservices and the scale of the web application, it is easy to encounter codes with numerous classes and packages. Therefore, having a good habit of illustrating the entities and controllers clearly in a project is crucial for efficient application development. Besides, as the frontend-backend-separated programming style is becoming mainstream, our backend code should be as straightforward as possible for front-end programmers to read. Today's tutorials will introduce using OpenAPI to generate JavaDocs in SpringBoot.

2. Details

First, we need to add our OpenAPI to obtain practical classes in our maven pom file. 1.6.11 is the latest version as this tutorial comes out. Actually, with the latest feature of OpenAPI, there aren't many set up for us to handle anymore. A beautifully designed JavaDoc file will be generated automatically.

<dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-ui</artifactId>
      <version>1.6.11</version>
   </dependency>

To test our project, I have prepared a simple MVC program with an entity bean called the SpaceCraft and a controller bean.

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class SpaceCraft {

    private String name;

    private Integer weight;

    private String description;

    private Integer numberOfCrew;
}

(Lombok is also used in this project for shortened codes)

@Tag(name = "星球飞船的控制器")
@RestController
public class SpaceCraftController {

    private List<SpaceCraft> crafts = new ArrayList<>();

    @GetMapping("/spacecraft")
    @Operation(summary = "获取目前可得的飞船")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "Found the book",
                    content = { @Content(mediaType = "application/json",
                            schema = @Schema(implementation = SpaceCraft.class)) }),
            @ApiResponse(responseCode = "400", description = "Invalid id supplied",
                    content = @Content),
            @ApiResponse(responseCode = "404", description = "Book not found",
                    content = @Content) })
    public List<SpaceCraft> returnSomeCraft(){
        crafts.add(new SpaceCraft("东方号",20,"最大的战列舰",5));
        return crafts;
    }

    @PostMapping("/spacecraft")
    public SpaceCraft createCraft(@RequestParam String craftName,
                              @RequestParam Integer crew,
                              @RequestParam Integer weight,
                              @RequestParam String description){
        SpaceCraft spaceCraft = SpaceCraft.builder().description(description).weight(weight).numberOfCrew(crew)
                .name(craftName).build();
        crafts.add(spaceCraft);
        return spaceCraft;

    }
}

Here, the tag annotation indicates the name of the whole controller, while the "operation" annotation states the name of a single controller. The "ApiResponse" annotation will display the result of a controller, including the return's type and required parameters. Although the OpenApi will generate files even if we don't write any of these annotations, it is better for us to make it clear with more details.

@Bean
	public OpenAPI springShopOpenAPI() {
		return new OpenAPI()
				.info(new Info().title("SpringShop API")
						.description("Spring shop sample application")
						.version("v0.0.1")
						.license(new License().name("Apache 2.0").url("http://springdoc.org")))
				.externalDocs(new ExternalDocumentation()
						.description("SpringShop Wiki Documentation")
						.url("https://springshop.wiki.github.org/docs"));
	}

Finally, there is some information we can add to our JavaDoc file, including version, description, author etc. Usually, this information is written in the application class. 

Our JavaDoc file is officially generated! After starting the server, we can visit [URL]:[port]/swagger-ui/index.html to see the result!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值