天天看点

Failed to meta-introspect annotation interface org.springframework.web.bind.annotation.RequestBody:

今天遇到了调试了很久的bug,最后发现是个小问题,报异常如下:
Failed to meta-introspect annotation interface org.springframework.web.bind.annotation.RequestBody: java.lang.NullPointerException
           
log4j给出了空指针的提示,又观察到跟注解RequestBody有关,疑似josn转对象失败,造成空指针异常,但自己在代码块中打印出了转换对象的属性

然而在网页开发者工具中发现请求成功

Failed to meta-introspect annotation interface org.springframework.web.bind.annotation.RequestBody:

在debug过程中发现,json转换的对象其实是成功的,空指针其实指的是服务接口

Failed to meta-introspect annotation interface org.springframework.web.bind.annotation.RequestBody:

转而目标点从请求传送对象转到服务中来,应该是zookeeper和dubbo的问题,使用zookeeper的客户端连接后查看后发现,确实是缺少消费者(consumers)服务的注册,那么问题一定出现在消费者服务的逻辑中

Failed to meta-introspect annotation interface org.springframework.web.bind.annotation.RequestBody:

缺少消费者服务的注册,反复观察代码逻辑没有任何问题,springmvc.xml中的配置也没有问题,经过仔细观察,最终发现是注解Refence引用的不对,应该引用的是dubbo的注解

错误代码

import jdk.nashorn.internal.ir.annotations.Reference;//引用错误,应该引用dubbo的Refence注解
...
@Reference //dubbo的注解,去zookeeper服务注册中心去查找CheckItemService这种接口的服务,封装到对象中
private CheckItemService checkItemService;
           

正确逻辑

import com.alibaba.dubbo.config.annotation.Reference;//正确引用
...
@Reference //dubbo的注解,去zookeeper服务注册中心去查找CheckItemService这种接口的服务,封装到对象中
private CheckItemService checkItemService;
           

再次进行发布(maven工程最好clean install一下)测试

Failed to meta-introspect annotation interface org.springframework.web.bind.annotation.RequestBody:
Failed to meta-introspect annotation interface org.springframework.web.bind.annotation.RequestBody:

测试成功,希望能帮助到问题和我一样的朋友,报错信息有点误导,需要自己经过调试发现问题,总结一下调试思路:

  1. 先看网页端的请求是否成功,用开发者工具,看前端控制台报错信息以及网络流,排除前端错误
  2. 再根据后台报错信息,通过debug调试定位信息,排除逻辑错误
  3. 检查配置文件信息,各种url、path等,尤其看spring是否开启包扫描
  4. 通过服务器的命令,排查服务是否进程出错,或者服务并没有注册上,通过工程全关闭,逐个开启观察的方式,定位出问题的模块
  5. 是否是同名类包引入错误(此步应该在编写逻辑的时候注意到)