gpt4 book ai didi

java - 如何使用 RequestBodyAdvice

转载 作者:行者123 更新时间:2023-12-03 15:56:24 24 4
gpt4 key购买 nike

@ControllerAdvice
public class RequestBodyAdviceChain implements RequestBodyAdvice {

@Override
public boolean supports(MethodParameter methodParameter, Type type,
Class< ? extends HttpMessageConverter< ? >> aClass) {
return true;
}

@Override
public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter,
Type type, Class< ? extends HttpMessageConverter< ? >> aClass) {
return o;
}

@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter,
Type type, Class< ? extends HttpMessageConverter< ? >> aClass) throws IOException {
return httpInputMessage;
}

@Override
public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type,
Class< ? extends HttpMessageConverter< ? >> aClass) {
return o;
}
}

理想情况下,流程应该先到达这些功能,然后再到达 Controller ,但它不起作用。

最佳答案

@ControllerAdvice组件应该像任何其他组件一样在 Spring 的上下文中注册。确保 Spring 看到这个组件,e.q.通过将其包含在 @ComponentScan 中(Java 配置)或在 <context:component-scan/> 中(xml 方式)。注册它的首选上下文是 Dispatcher Servlet 的上下文。

关于java - 如何使用 RequestBodyAdvice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35277727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com