Spring ResponseBodyAdvice、RequestBodyAdvice接口

ResponseBodyAdvice

主要作用是响应体写出之前做一些处理。

@Order(1)  
@ControllerAdvice(basePackages = "com.github")  
public class MyResponseBodyAdvice implements ResponseBodyAdvice<Object> {  

    @Override  
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> converterType) {  
        return methodParameter.getMethod().getReturnType().isAssignableFrom(User.class);  
    }  

    @Override  
    public Object beforeBodyWrite(  
            Object obj, MethodParameter methodParameter, MediaType mediaType,  
            Class<? extends HttpMessageConverter<?>> converterType,  
            ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {  

        User user = ((User)obj);  
        user.setName("---" + user.getName() + "---");  
        return user;  
    }  
} 
RequestBodyAdvice

请求增强。在读取请求body之前或者在body转换成对象之前可以做相应的增强。
接口定义如下:

public interface RequestBodyAdvice {

    /**
     * Invoked first to determine if this interceptor applies.
     * @param methodParameter the method parameter
     * @param targetType the target type, not necessarily the same as the method
     * parameter type, e.g. for {@code HttpEntity<String>}.
     * @param converterType the selected converter type
     * @return whether this interceptor should be invoked or not
     */
    boolean supports(MethodParameter methodParameter, Type targetType,
            Class<? extends HttpMessageConverter<?>> converterType);

    /**
     * Invoked second before the request body is read and converted.
     * @param inputMessage the request
     * @param parameter the target method parameter
     * @param targetType the target type, not necessarily the same as the method
     * parameter type, e.g. for {@code HttpEntity<String>}.
     * @param converterType the converter used to deserialize the body
     * @return the input request or a new instance, never {@code null}
     */
    HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter,
            Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException;

    /**
     * Invoked third (and last) after the request body is converted to an Object.
     * @param body set to the converter Object before the first advice is called
     * @param inputMessage the request
     * @param parameter the target method parameter
     * @param targetType the target type, not necessarily the same as the method
     * parameter type, e.g. for {@code HttpEntity<String>}.
     * @param converterType the converter used to deserialize the body
     * @return the same body or a new instance
     */
    Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
            Type targetType, Class<? extends HttpMessageConverter<?>> converterType);

    /**
     * Invoked second (and last) if the body is empty.
     * @param body usually set to {@code null} before the first advice is called
     * @param inputMessage the request
     * @param parameter the method parameter
     * @param targetType the target type, not necessarily the same as the method
     * parameter type, e.g. for {@code HttpEntity<String>}.
     * @param converterType the selected converter type
     * @return the value to use or {@code null} which may then raise an
     * {@code HttpMessageNotReadableException} if the argument is required.
     */
    @Nullable
    Object handleEmptyBody(@Nullable Object body, HttpInputMessage inputMessage, MethodParameter parameter,
            Type targetType, Class<? extends HttpMessageConverter<?>> converterType);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值