excludepathpatterns不生效(Required request body is missing异常)

excludepathpatterns不生效(Required request body is missing异常)

记录一个springboot配置的拦截器excludepathpatterns不拦截的地址失效问题

自己定义的拦截器

public class MyHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("TOKEN");
        //token无效重定向
        if (token==null||token.equals("")){
            return false;
        }
        return true;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) 		throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}

配置文件

@Configuration
public class MyServerConfig extends WebMvcConfigurationSupport {
    /**
     * 添加自定义拦截器
     * @param registry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        //获取提前实例化的拦截器
        registry.addInterceptor(getMyHandlerInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/user-info/login");
        super.addInterceptors(registry);
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }

    /**
     * 提前实例化拦截器(拦截器使用redis要提前实例化)
     * @return
     */
    @Bean
    public MyHandlerInterceptor getMyHandlerInterceptor(){
        return new MyHandlerInterceptor();
    }
}

相关的controller类

    @RequestMapping("/user-info/login")
    public Object login(@RequestBody UserInfo userInfo){
        return null;
    }

出现的问题:

配置文件中我们直接放行了/user-info/login这个地址,按道理说请求这个接口http://localhost:8089/user-info/login时不应该经过拦截器,但是在开发中就遇到了,报了Required request body is missing异常。在网上查了很多资料,尝试了很多此发现都不行。

后来尝试把拦截器关掉,发现得到的userInfo为null,或将@RequestBody改为@RequestBody(required = false)就可以。原来问题就出现在

@RequestBody UserInfo userInfo 这里,这里并不能接收前端传过来的参数,也就是说,有可能是前端传过来的数据是有问题的。

查看前端:

        fetchData({
                    url:'/user-info/login',
                    methods:'post',
                    data:{}
                }).then(res=>{
                  console.log(res)
                }).catch(err=>{
                    console.log(err)
                })

发现问题了吗

原来是我将method 写成了methods。。。。。。当场脑溢血。。。。。。

正确的应该是

        fetchData({
                    url:'/user-info/login',
                    method:'post',
                    data:{}
                }).then(res=>{
                  console.log(res)
                }).catch(err=>{
                    console.log(err)
                })

至于为什么这里错了导致这个请求路径还能进入拦截器,是因为controller那里报了错,导致框架直接转跳到error(相当于重定向),而error并没有被excludepathpatterns,所以会经过拦截器(这是我查资料得到的解释,学到了)

总结:

所以excludepathpatterns无效,也有可能时前端操作不当营造的假象。就是所受多打了一个字母就变成这样了。如果你很确定不是后台的问题,好好检查一下前端吧。当然也有可能是其他的原因造成的。

至于我这个脑溢血行为,看到这篇文章的大家引以为戒(别喷)。。。。。。

如果看到本篇文章有什么问题或者不妥的话欢迎指正。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值