tj
2025-06-05 2d549a04870d1315868a7cf19952b64e8071e711
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
package com.cloudroam.common.aop;
 
import com.cloudroam.common.configuration.CodeMessageConfiguration;
import com.cloudroam.vo.UnifyResponseVO;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
 
/**
 * 处理返回结果为 UnifyResponseVO 的控制器层方法
 * message 默认为 null,在此处通过 code 设置为对应消息
 *
 * @author 
 * @author 
 * @author 
 */
@Aspect
@Component
public class ResultAspect {
    @AfterReturning(returning = "result", pointcut = "execution(public * com.cloudroam.controller..*.*(..))")
    public void doAfterReturning(UnifyResponseVO<String> result) {
        int code = result.getCode();
        String messageOfVO = result.getMessage();
        // code-message.properties 中配置的 message
        String messageOfConfiguration = CodeMessageConfiguration.getMessage(code);
 
        // 如果 code-message.properties 中指定了相应的 message 并且 UnifyResponseVO 的 message 为null
        // 则使用 messageOfConfiguration 替换 messageOfVO
        if (StringUtils.hasText(messageOfConfiguration) && !StringUtils.hasText(messageOfVO)) {
            result.setMessage(messageOfConfiguration);
        }
    }
}