全局响应返回处理

简介: 全局响应返回处理

我喜欢我的懦弱,痛苦和难堪也喜欢。喜欢夏天的光照,风的气息,蝉的鸣叫,喜欢这些,喜欢得不得了。——《寻羊冒险记》

上代码:

import io.github.vampireachao.stream.core.optional.Opp;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
/**
 * @author VampireAchao
 */
@RestControllerAdvice(basePackages = "com.ruben.controller")
public class AnimeResultHandler implements ResponseBodyAdvice<Object> {
    /**
     * 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
     */
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return !(returnType.getParameterType().isAssignableFrom(Result.class));
    }
    /**
     * Invoked after an {@code HttpMessageConverter} is selected and just before
     * its write method is invoked.
     *
     * @param body                  the body to be written
     * @param returnType            the return type of the controller method
     * @param selectedContentType   the content type selected through content negotiation
     * @param selectedConverterType the converter type selected to write to the response
     * @param request               the current request
     * @param response              the current response
     * @return the body that was passed in or a modified (possibly new) instance
     */
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        return Opp.ofNullable(body).map(Result::ok).orElseGet(Result::ok);
    }
}


相关文章
解决传入的请求具有过多的参数,该服务器支持最多 2100 个参数
解决传入的请求具有过多的参数,该服务器支持最多 2100 个参数
|
6月前
|
JavaScript 前端开发
Vue中传递自定义参数到后端、后端获取数据(使用Map接收参数)
这篇文章讲述了如何在Vue中通过Axios二次封装传递自定义参数到后端,并展示了后端如何使用Map接收这些参数,以及如何避免参数转换错误和统一接口设计的方法。
|
6月前
|
Go 数据处理
深入理解函数返回多个值的机制
【8月更文挑战第31天】
24 0
|
6月前
|
运维 Serverless 调度
函数计算产品使用问题之怎么在HTTP触发的函数里添加或读取自定义头部
函数计算产品作为一种事件驱动的全托管计算服务,让用户能够专注于业务逻辑的编写,而无需关心底层服务器的管理与运维。你可以有效地利用函数计算产品来支撑各类应用场景,从简单的数据处理到复杂的业务逻辑,实现快速、高效、低成本的云上部署与运维。以下是一些关于使用函数计算产品的合集和要点,帮助你更好地理解和应用这一服务。
|
7月前
|
存储 JavaScript Serverless
函数计算产品使用问题之如何获取请求ID并响应给调用者
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
7月前
|
前端开发 Java Spring
设置响应内容类型的几种方法比较
设置响应内容类型的几种方法比较
在职责链模式中,如何判断是否应该处理请求或将请求传递给下一个节点
在职责链模式中,如何判断是否应该处理请求或将请求传递给下一个节点
|
9月前
|
存储 JavaScript Java
响应系统的作用与实现(上)
响应系统的作用与实现
104 0
|
9月前
|
缓存 调度
响应系统的作用与实现 (下)
响应系统的作用与实现
77 0
ResponseBodyAdvice还可以用于处理控制器抛出异常时的响应格式
除了对控制器返回数据进行统一的处理外,ResponseBodyAdvice还可以用于处理控制器抛出异常时的响应格式。以下是一个示例,实现了对异常的包装,添加了响应码和响应消息: