How do I change Swagger's skin?

We talked about integrating Swagger 2 components in Spring Boot, so today we'll talk about how to change Swagger's skin? Environment building: Spring Boot relies on Swagger Spring Boot starter for rapid building. For specific Swagger Spring Boot starter, please refer to: https://github.com/SpringForA... . The build tool is Maven, the development tool is IDEA, and the JDK version is 1.8.

Step 1: Maven quickly builds the web project of Spring Boot

Step 2: unzip, IDEA import project

Step 3: integrate swagger spring boot starter

Dependency in pom:

<dependency>
    <groupId>com.spring4all</groupId>
    <artifactId>swagger-spring-boot-starter</artifactId>
    <version>1.8.0.RELEASE</version>
</dependency>

Add @ EnableSwagger2Doc add enable swagger annotation. By default, all request mapping documents loaded by current Spring MVC will be generated.

Step 4: configure swagger

# Configure in application.properties
swagger.title=Code Song academy API
swagger.description=Relevant interfaces of the school API File
swagger.version=1.1
swagger.base-package=com.mage

Please refer to GitHub for other specific configurations, https://github.com/SpringForA... . Note that there is Chinese in the profile in IDEA, so you need to set the encoding of its profile to UTF-8. Specific settings: File - > Settings - > editor - > file encoding set the Default encoding for properties files under Properties Files (*.properties) to UTF-8, and check the box before transparent native to ASCII conversion.

Step 5: write TestController

package com.mage.swagger02.controller;

import com.mage.swagger02.model.Test;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("test")
@Api(tags = "test API Interface")
public class TestController {

    @GetMapping("")
    @ApiOperation(value="Get list data", notes="Get test data under the list")
    public String list() {
        return "Query list data!";
    }

    @GetMapping("{id}")
    @ApiOperation(value="Obtain ID data", notes="according to ID Get a test data")
    @ApiImplicitParam(name = "id", value = "Primary key id", paramType = "path", required = true)
    public String find(@PathVariable Integer id) {
        return String.format("Query data according to primary key: %d", id);
    }

    @PostMapping("")
    @ApiOperation(value="New data")
    @ApiParam(name = "test", value = "Added test model entities")
    public String add(@RequestBody Test test) {
        return "insert data!";
    }

    @PutMapping("{id}")
    @ApiOperation(value="Update data", notes="according to ID Update test data")
    @ApiImplicitParam(name = "id", value = "Primary key id", paramType = "path", required = true)
    public String update(@PathVariable Integer id, @ApiParam(name = "test", value = "Updated test model entities") @RequestBody Test test) {
        return String.format("Update a record according to the primary key: %d", id);
    }

    @DeleteMapping("{id}")
    @ApiOperation(value="Delete data", notes="according to ID Delete test data")
    @ApiImplicitParam(name = "id", value = "Primary key id", paramType = "path", required = true)
    public String delete(@PathVariable Integer id) {
        return String.format("Delete records based on primary key: %d", id);
    }
}

Step 6: start execution, browser input http://localhost:8080/swagger-ui.html

Step 7: change skin

If you think the skin of swagger is not good-looking, you can replace it. You only need to introduce the following jar package into pom:

<dependency>
    <groupId>com.github.caspar-chen</groupId>
    <artifactId>swagger-ui-layer</artifactId>
    <version>1.1.2</version>
</dependency>

Then the browser enters: http://localhost:8080/docs.html

Now that the skin change is complete, download the source code: https://github.com/magekang/s...

Keywords: Java Spring github encoding Maven

Added by cybercookie72 on Tue, 03 Dec 2019 16:54:38 +0200