@ResponseBody 为空时覆盖默认消息 [英] Override default message when @ResponseBody is null

查看:24
本文介绍了@ResponseBody 为空时覆盖默认消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这个 Spring 类覆盖这个行为:

I want to override this behaviour from this Spring class:

public class RequestResponseBodyMethodProcessor extends AbstractMessageConverterMethodProcessor {
    ...
    @Override
    protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter,
            Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {
            ...
            if (arg == null && checkRequired(parameter)) {
                throw new HttpMessageNotReadableException("Required request body is missing: " +
                    parameter.getExecutable().toGenericString(), inputMessage);
            }
    ...
}

当@ReponseBody 来自@Controller 为空时,此方法处理错误.我尝试制作我自己的 CustomBodyMethodProcessorRequestResponseBodyMethodProcessor 扩展如下:

This method handles the error when @ReponseBody is null from a @Controller. I tried to make my own CustomBodyMethodProcessor extending from RequestResponseBodyMethodProcessor like this:

public class CustomBodyMethodProcessor extends RequestResponseBodyMethodProcessor {

    public CustomBodyMethodProcessor(List<HttpMessageConverter<?>> converters) {
        super(converters);
    }

    @Override
    protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter,
                                                   Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {

        HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
        Assert.state(servletRequest != null, "No HttpServletRequest");
        ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(servletRequest);

        Object arg = readWithMessageConverters(inputMessage, parameter, paramType);
        if (arg == null && checkRequired(parameter)) {
            //I want to change this....
            throw new HttpMessageNotReadableException("Required request body is missing: " +
                    parameter.getExecutable().toGenericString(), inputMessage);
        }
        return arg;
    }

}

所以,为了添加新的 CustomBodyMethodProcessor,我做到了:

So, to add the new CustomBodyMethodProcessor I did :

@Configuration
@EnableWebMvc
public class WebMvcConfig implements  WebMvcConfigurer {

    @Autowired
    List<HttpMessageConverter<?>> converters;


    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        WebMvcConfigurer.super.addArgumentResolvers(resolvers);
        resolvers.add(new CustomBodyMethodProcessor(converters));

    }

}

但这行不通.当我调试时,总是在 RequestResponseBodyMethodProcessorreadWithMessageConverters 方法中停止,而不是在 CustomBodyMethodProcessor

But this not works. When I debug, always stops in readWithMessageConverters method from RequestResponseBodyMethodProcessor and not in my overrided method from CustomBodyMethodProcessor

感谢您为我提供的任何帮助!!

I appreciate any help you can provide me !!

谢谢

推荐答案

找到解决方案.

    @Autowired
    private RequestMappingHandlerAdapter adapter;

    @PostConstruct
    public void prioritizeCustomArgumentMethodHandlers () {
        List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>(adapter.getArgumentResolvers ());
        List<HandlerMethodArgumentResolver> customResolvers = adapter.getCustomArgumentResolvers();
        argumentResolvers.removeAll(customResolvers);
        argumentResolvers.addAll (0, customResolvers);
        adapter.setArgumentResolvers (argumentResolvers);
    }

我的 CustomBodyMethodProcessor 已添加到解析器列表中,但原始 RequestResponseBodyMethodProcessor 具有优先级,因此从不调用 CustomBodyMethodProcessor.

My CustomBodyMethodProcessor was added to the list of resolvers but original RequestResponseBodyMethodProcessor has precedence so CustomBodyMethodProcessor is never invoked.

解决方案是从解析器中删除我的 CustomBodyMethodProcessor,给予优先权并将其重新添加到列表中!

The solution was to delete from resolvers my CustomBodyMethodProcessor, give precedence and add it again to the list!

感谢这个 asnwer https://stackoverflow.com/a/19847526/4267653@BeUndead 为线索!

Thanks to this asnwer https://stackoverflow.com/a/19847526/4267653 and to @BeUndead for the clue !

这篇关于@ResponseBody 为空时覆盖默认消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆