全局响应返回处理

简介: 全局响应返回处理

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

上代码:

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 个参数
在职责链模式中,如何判断是否应该处理请求或将请求传递给下一个节点
在职责链模式中,如何判断是否应该处理请求或将请求传递给下一个节点
|
1月前
|
缓存 调度
响应系统的作用与实现 (下)
响应系统的作用与实现
50 0
|
1月前
|
存储 JavaScript Java
响应系统的作用与实现(上)
响应系统的作用与实现
61 0
|
6月前
|
JSON JavaScript 前端开发
一个由服务端返回的long值改变引发的血案
一个由服务端返回的long值改变引发的血案
45 0
|
7月前
|
JSON 前端开发 数据格式
全局日期请求转换处理
全局日期请求转换处理
50 0
|
11月前
|
Serverless 容器
每个请求通常会触发一个新的函数实例来处理。
每个请求通常会触发一个新的函数实例来处理。
59 2
|
11月前
|
Serverless 容器
每个请求通常会触发一个新的函数实例来处理
每个请求通常会触发一个新的函数实例来处理
55 1
|
11月前
|
JSON PHP 数据格式
响应 方式
响应 方式
怎么修改请求的参数和响应
怎么修改请求的参数和响应
81 0