SSM backend development (Swagger)

technology stack

        ​ ​ swagger: used to generate, describe, call and visualize RESTful style web services. To put it bluntly, it means exposing the interfaces needed in the project.

accomplish

        ​ ​ 1. Use maven to add swagger dependencies
<dependency>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-jaxrs2-servlet-initializer</artifactId>
    <version>2.2.15</version>
</dependency>
        2. Create a spring boot project

         ​ ​Create a spring boot project through Spring Initializr

        Add web support

        3. SwaggerConfig configuration class
@OpenAPIDefinition(
        info = @Info(
                title = "",
                description = "",
                contact = @Contact(
                        name = "",
                        email = "",
                        url = ""
                ),
                version = "",
                summary = ""
        ),
        security = @SecurityRequirement(name = "Authorization")
)
@SecurityScheme(type = SecuritySchemeType.HTTP
        , name = "Authorization", in = SecuritySchemeIn.HEADER
        , scheme = "Bearer")
@Configuration
public class SwaggerConfig {
        @Bean
        ForwardedHeaderFilter forwardedHeaderFilter() {
                return new ForwardedHeaderFilter();
        }

}
        4. Open the web page to view

        5. Configure Swagger

        Mainly configure the tag, operation and schema of swagger

@Tag(name = "Role", description = "用户角色")
public class Role implements Serializable {

    @Schema(accessMode = Schema.AccessMode.READ_ONLY)
    private int id;

    private String roleName;

    private String roleCode;

    private String description;

    @Schema(
            accessMode = Schema.AccessMode.READ_ONLY
            , pattern = "YYYY-mm-dd HH:MM:ss"
    )
    @JsonFormat(pattern = "YYYY-mm-dd HH:MM:ss")
    private Date createTime;

    @Schema(
            accessMode = Schema.AccessMode.READ_ONLY
            , pattern = "YYYY-mm-dd HH:MM:ss"
    )
    @JsonFormat(pattern = "YYYY-mm-dd HH:MM:ss")
    private Date updateTime;

    private static final long serialVersionUID = 1L;
}

         View on the web page

        

@Tag(name = "auth", description = "认证相关的Controller")
public class AuthController {


    private final AuthService authService;

    @Autowired
    public AuthController(AuthService authService) {
        this.authService = authService;
    }

    @Operation(
            method = "POST",
            description = "用户登录",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    content = @Content(mediaType = "application/json")
            ),
            security = @SecurityRequirement(name = "", scopes = ""),
            summary = "用户登陆"
    )
    @PostMapping("login")
    public ResponseEntity<LoginVO> login(@RequestBody LoginParams login) {
        ResponseEntity<LoginVO> entity;
        try {
            String token = authService.auth(login.getUsername(), login.getPassword());
            entity = ResponseWrapper.responseEntityAccept(new LoginVO(token));
        } catch (UserLoginErrorException e) {
            entity = ResponseWrapper.responseEntityFail(e.getMsg());
        }
        return entity;
    }
}

        View in web page

Guess you like

Origin blog.csdn.net/2201_75875170/article/details/133959868