In the last chapter, we basically completed the construction of the project framework. Our current project is to complete a stand-alone server application similar to the traditional website, so what should we do next?

The objective of the GitHub:https://github.com/pc859107393/Go2SpringBoot.git

Those interested in springboot for rapid development can add the penguin colony below.

Implement online APIDocs

Is online ApiDocs used to do? APIDocs is a form of documentation for an API interface. It allows us to debug quickly online. Note: Swagger does not help us implement RESTFul interfaces, just that we can display RESTFul interface information on a page.

Configure Swagger Settings

Swagger is essentially an online APIDocs, which means we have to start with configuration. But we have analyzed springFox configuration a long time ago. Here we only need to focus on Swagger’s resource allocation, as shown in Figure 2.1.

Figure 2.1 Static resources of Swagger

In our previous project configuration, all resources needed to be reasonably allocated to provide external access, and we need to do the same here.

Open the boot class file of SpringBoot, I use the boot file baseApplication. kt written by Kotlin.

@SpringBootApplication
@EnableWebMvc
@EnableSwagger2
@MapperScan(value = ["cn.acheng1314.base.dao"])
@Configuration
class BaseApplication : WebMvcConfigurer {
    
    // Add static resources that need to be exposed here
    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        // Swagger and swagger's third party skins need to be registered
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/")
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/")
        
        // Here are registered druid resources
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/")
        
        // Here is the static resource access directory to register this program
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/")}// Configure swagger API grouping here
    @Bean(name = ["defaultApi"])
    fun createRestApi(a): Docket {
        return Docket(DocumentationType.SWAGGER_2)  //Docket, Springfox's private API setting is initialized to Swagger2
                .select()
                // Specify the package path of the Controller in the project to be scanned
                .apis(RequestHandlerSelectors.basePackage("cn.acheng1314.base.web"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(ApiInfoBuilder()   // Set the body description of the API document
                        .title("Acheng's SpringBoot discovery Path ApiDocs")
                        .description("Acheng's SpringBoot Quest")
                        .version("v1.01")
                        .termsOfServiceUrl("https://acheng1314.cn/")
                        .build())
                .groupName("Default interface")}// Omit other code here... please go to my Github project for details
}
Copy the code

Verify that Swagger is in effect

Let’s write a short piece of code to try it out. The code is as follows:

@Controller
class MainController {

    @GetMapping(value = ["/"], produces = [MediaType.APPLICATION_JSON_UTF8_VALUE])
    @ResponseBody
    @ApiOperation(value = "User output test", notes = "User Query", response = User::class)
    fun MainLocal(a): Any = User("Cheng"."18976962315"."123456"."Brag", Date())

    @GetMapping(value = ["/test"], produces = [MediaType.TEXT_HTML_VALUE])
    fun getTest(map: ModelMap): String {
        map["test"] = MainLocal()
        return "test1"
    }

    @PostMapping(value = ["/json"], produces = [MediaType.APPLICATION_JSON_UTF8_VALUE])
    @ResponseBody
    @ApiOperation(value = "Return the submitted User", notes = "Return the submitted User", response = User::class)
    fun getJson(@RequestBody user: User): Any {
        println(String.format("User info: %s", user.toString()))
        return GsonUtil.toJson(user)
    }

    // In the code above, GetMapping and PostMapping are request path annotations in SpringMvc. Produces specifies the content type to return.

}
Copy the code

After running the project, the result looks like Figure 2.2.

Figure 2.2 Screenshot of Swagger after deployment

Annotations (IO. Swagger. Annotations) for @apioperation, annotations (IO. Swagger.