RESTEASY003130: Class is not a root resource的问题原因
来源:互联网 发布:淘宝关键词重复有用吗 编辑:程序博客网 时间:2024/06/06 19:50
问题描述
今天在使用dubbox的时候,在启动服务的时候报出一个错误,导致应用无法启动,错误的提示信息为:
Initialization of bean failed; nested exception is java.lang.RuntimeException: RESTEASY003130: Class is not a root resource. It, or one of its interfaces must be annotated with @Path
dubbox是在dubbo的基础上进行了restFul的扩展,使用了JAX-RS的扩展,因此可以支持restFul的调用,默认的调用方式应该为:
Interface:
@Path("interface/doSomething")@Consumes(MediaType.APPLICATION_JSON)@Produces(MediaType.APPLICATION_JSON)public interface TestService { @POST @Path("run") boolean test(TestDTO dto);}
Interface Implements:
@Servicepublic class TestServiceImpl implements TestService { @Override public boolean test(TestDTO dto) { //doSomething....... }}
由于dubbox是基于dubbo实现的,当我们有某些方法不想再提供restFul的接口时,只是提供dubbo的调用,该怎么做呢?
于是我想当然的这么做了:
public interface TestService { boolean test(TestDTO dto);}
我把interface中的JAX-RS的注解去除了,这样就只是提供dubbo的调用方式,而不提供restFul的调用了,然后当我启动项目的时候,就报了上面的错误……
问题原因
找了好多资料,找到了原因所在,因为dubbox支持JAX-RS,因此加入相应的Jar文件,其中就有resteasy,在其中的一个类ResourceMethodRegistry.java 中的addResourceFactory()方法,找到了问题原因:
/** * ResourceFactory.getScannableClass() defines what class should be scanned * for JAX-RS annotations. The class and any implemented interfaces are scanned for annotations. * * @param ref * @param base base URI path for any resources provided by the factory, in addition to rootPath */ public void addResourceFactory(ResourceFactory ref, String base) { Class<?> clazz = ref.getScannableClass(); Class restful = GetRestful.getRootResourceClass(clazz); if (restful == null) { String msg = Messages.MESSAGES.classIsNotRootResource(clazz.getName()); for (Class intf : clazz.getInterfaces()) { msg += " " + intf.getName(); } throw new RuntimeException(msg); } addResourceFactory(ref, base, restful); }
可以看到ResourceFactory.getScannableClass()定义了扫描哪些class文件,进入看一下具体的实现:
public class DubboResourceFactory implements ResourceFactory { private Object resourceInstance; private Class scannableClass; public DubboResourceFactory(Object resourceInstance, Class scannableClass) { this.resourceInstance = resourceInstance; this.scannableClass = scannableClass; } public Class<?> getScannableClass() { return scannableClass; }}
这里只给出部分源码,从此可以看出scannableClass是由dubbo注解的class,因此可以明白其报错的原因,JAX-RS会扫描被dubbo注解的class实现的interface,由于interface没有注解@Path,因此抛出了异常。
总结
使用dubbox后,提供dubbo服务的类必须注解@Path,即使该类中没有提供restFul的调用,否则会报出”Class is not a root resource”的异常,那我们有的类中的确没有方法需要提供restFul调用的时候该怎么办呢?可以在方法上不加任何注解,这样做的话只可以进行dubbo方式的调用
@Path("interface/doSomething")@Consumes(MediaType.APPLICATION_JSON)@Produces(MediaType.APPLICATION_JSON)public interface TestService { boolean test(TestDTO dto);}
- RESTEASY003130: Class is not a root resource的问题原因
- 关于addClass is not a function问题的原因
- 出现类似'CListView' : is not a class or namespace name的原因及解决方法
- The requested resource () is not available.的一种原因
- javax.naming.NamingException: Could not load resource factory class [Root exception is java.lang.Cl
- 问题:Error: ' ' is not a valid resource name character
- 关于android.content.res.Resources$NotFoundException: Resource is not a Drawable 问题的解决办法
- 如何解决The requested resource () is not available的问题
- tomcat: The requested resource is not available.一种可能的原因
- RESTful root resource class
- The requested resource is not available. 原因,成功解决
- eclipse中的the resource is not on the build path of a java project相关问题
- eclipse中的the resource is not on the build path of a java project相关问题
- is not a class or namespace name
- is not a class or namespace name
- null is not a java.class file
- VS2005中CSCx.tmp is not a valid Win32 resource file错误的解决办法
- mysql_fetch_row(): supplied argument is not a valid MySQL result resource的问题解决方案
- poj 2945 Find the Clones
- robotframework源码解析
- oracle中的decode函数
- 结构体元素做函数参数和结构指针做函数参数
- 学习方法总结——《快速学习》总结
- RESTEASY003130: Class is not a root resource的问题原因
- ios 枚举值 位移表示的好处
- 课时18 Linux网络基础配置
- Angular vs React 最全面深入对比
- LeetCode
- python 文本写入及文本替换练习
- 实习程序1
- 关于jQuery load()方法加载页面后台发生异常而前台页面加载失败且没有任何展示信息的问题处理
- 密码发生器--蓝桥杯java组历年真题