springboot实现返回值处理

  1. 统一添加接口返回结果处理 ResponseBodyAdvice切面操作返回值
  2. 统一添加接口参数输出日志,级别debug

1、ResponseBodyAdvice可以在注解@ResponseBody将返回值处理成相应格式之前操作返回值。实现这个接口即可完成相应操作。可用于返回值加密

实现相应类:ResponseBodyAdvice
源码如下:

public interface ResponseBodyAdvice<T>{
  	/**
     * Whether this component supports the given controller method return type
     * and the selected {@code HttpMessageConverter} type.(此组件是否支持给定的控制器方法返回值类型)
     * @param returnType the return type(返回类型)
     * @param converterType the selected converter type(选中的转换器类型)
     * @return {@code true} if {@link #beforeBodyWrite} should be invoked;
     * {@code false} otherwise(返回是否调用处理方法)
     */
	boolean support(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType);

	/**
     * Invoked after an {@code HttpMessageConverter} is selected and just before
     * its write 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,可以通过以下步骤实现接口返回的加密: 1. 创建一个自定义的响应对象,用于封装加密后的返回数据。例如,可以创建一个名为`EncryptedResponse`的类,包含一个`data`字段用于存储加密后的数据。 ```java public class EncryptedResponse { private String data; public EncryptedResponse(String data) { this.data = data; } // getter and setter } ``` 2. 创建一个自定义的响应切面,在切面中对接口返回进行加密处理。可以使用Spring AOP来实现这一步骤。 ```java @Aspect @Component public class EncryptionAspect { @Autowired private EncryptionService encryptionService; @Pointcut("execution(* com.example.controllers.*.*(..))") public void controllerMethods() {} @Around("controllerMethods()") public Object encryptResponse(ProceedingJoinPoint joinPoint) throws Throwable { // 执行目标方法 Object result = joinPoint.proceed(); // 对返回进行加密处理 String encryptedData = encryptionService.encrypt(result.toString()); // 创建加密后的响应对象 EncryptedResponse encryptedResponse = new EncryptedResponse(encryptedData); return encryptedResponse; } } ``` 在上述代码中,`EncryptionService`是一个自定义的加密服务,用于对返回进行加密。你可以根据具体需求选择适合的加密算法和实现方式。 3. 在Spring Boot的配置文件中启用AOP。 ```java @EnableAspectJAutoProxy @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 通过以上步骤,你可以在Spring Boot中实现接口返回的加密处理。每次调用接口时,返回的数据将会被加密后封装在`EncryptedResponse`对象中返回给客户端。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值