Spring Boot + React 写聊天软件 Ep.2 - 一些细节与技巧

182 阅读3分钟

转载自我的博客:blog.lyc8503.site/post/my-cha…

本系列记录了我从零开始学习 Spring + React 并完成一个小项目的经历和心得, 仅供参考, 并不是相关的教程. 项目概要见本系列 Ep.0.

上篇未讲完的一些琐碎的问题, 就在这里记录一下, 主要是一些我找到的能让 Coding 效率变高, 代码更简洁的方法.

Lombok

Springboot 搭配 Lombok 食用风味更佳~

@Slf4j 直接在当前类中加入 logger 对象, 方便地打 log.

比如一个 Entity 所有的 Getter/Setter 不再需要使用 IDE 生成, 可以直接使用 @Data 注解一键生成. @Builder 一键生成 Builder, @AllArgsConstructor 还可以一键生成构造器.

新版本的 Spring 直接在 Field 上使用 @Autowired 会给出 not recommend 的警告, 这时只要将需要注入的 Bean 设置为 final 并添加 @RequiredArgsContructor 即可.

1
2
3
4
5
6
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    private final UserDao userDao;  // 这里的 Dao 能自动注入
	// ....
}

统一响应格式

项目的一开始就要统一一下响应格式, 我定义了一个 CommonResponse 表示所有响应都遵循的格式.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class CommonResponse<T> {
    @Schema(description = "响应码")
    private int code;
    @Schema(description = "响应消息")
    private String msg;
    @Schema(description = "响应数据")
    private T data;

@Hidden
@JsonIgnore
// used to define http status code
private int httpCode;

public static &lt;T&gt; CommonResponse&lt;T&gt; success() {
    return success(null);
}

public static &lt;T&gt; CommonResponse&lt;T&gt; success(T data) {
    return success(data, 200);
}

public static &lt;T&gt; CommonResponse&lt;T&gt; success(T data, int httpCode) {
    return new CommonResponse&lt;&gt;(0, "success", data, httpCode);
}

public static &lt;T&gt; CommonResponse&lt;T&gt; error(ErrorType type) {
    return new CommonResponse&lt;&gt;(type.getCode(), type.getMessage(), null, type.getHttpCode());
}

public static &lt;T&gt; CommonResponse&lt;T&gt; error(ErrorType type, String msg) {
    return new CommonResponse&lt;&gt;(type.getCode(), msg, null, type.getHttpCode());
}

public static &lt;T&gt; CommonResponse&lt;T&gt; error(BizException exception) {
    return new CommonResponse&lt;&gt;(exception.getCode(), exception.getMessage(), null, exception.getHttpCode());
}




}

}

统一异常处理

RESTful 要求使用 Http status code 表示响应的状态.

但 status code 的表达能力有限, 为了更好的表现出细致的业务异常, 我选择同时使用 Http status code 和自定义的响应状态码.

我是用枚举表示了可能遇到的异常状态, 并赋予对应的 code 和 httpCode.

github.com/lyc8503/Lyc…

在 Service 类中业务出错时, 则直接 throw new BizException(ErrorType.xxx), 上层捕获异常后能直接取出其中的错误类型并返回给前端.

捕获异常使用自定义的 ExceptionHandler:

github.com/lyc8503/Lyc…

为了处理 CommonReponse 中的 httpCode, 我使用了 ControllerAdvice 拦截响应并将 Http 状态码设置为预期的值.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@ControllerAdvice
public class HttpStatusCodeControllerAdvice implements ResponseBodyAdvice<CommonResponse<?>> {
    /**
     * Used to set http status code
     */

@Override
public boolean supports(MethodParameter returnType, Class converterType) {
    return returnType.getParameterType().isAssignableFrom(CommonResponse.class);
}

@Override
public CommonResponse&lt;?&gt; beforeBodyWrite(CommonResponse&lt;?&gt; body, MethodParameter returnType, MediaType selectedContentType, Class&lt;? extends HttpMessageConverter&lt;?&gt;&gt; selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    if (body != null) {
        response.setStatusCode(HttpStatus.valueOf(body.getHttpCode()));
    }
    return body;
}




}


本文使用 文章同步助手 同步