SSM 后端开发(Swagger)

技术栈

        swagger:用于生成、描述、调用和可视化RESTful风格的Web服务。直白地说就是将项目中需要的接口暴露出去。

实现

        一、使用maven添加swagger依赖
<dependency>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-jaxrs2-servlet-initializer</artifactId>
    <version>2.2.15</version>
</dependency>
        二、创建一个spring boot项目

         通过Spring Initializr创建spring boot项目

        添加web支持

        三、SwaggerConfig配置类
@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();
        }

}
        四、打开web网页查看

        五、配置Swagger

        主要对swagger的Tag、Operation以及Schema进行配置

@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;
}

         在web页面查看

        

@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;
    }
}

        在web网页中查看

猜你喜欢

转载自blog.csdn.net/2201_75875170/article/details/133959868
今日推荐