我是Java的新手,在尝试围绕Netbeans创建的所有对象时,正在开发我的第一个JPA项目。我使用向导从数据库表创建了一个“User”JPA实体。然后我去了超级类所在的AbstractFacade.java,添加了一个新方法,可以通过他们的电子邮件地址找到特定用户。
public T findFromEmail(String mailbox) {
javax.persistence.Query q = getEntityManager().createNamedQuery("Users.findByEmail").setParameter("email", mailbox);
return (T)q.getSingleResult();
}
接下来,我转到UsersFacaceRest.java并添加了以下方法。我不确定为什么我在这个例子中需要@override,因为我似乎没有覆盖任何底层对象,但如果我没有,Netbeans给了我一个错误。
@GET
@Path("{mailbox}")
@Produces({"application/xml", "application/json"})
@Override
public Users findFromEmail(@PathParam("mailbox") String mailbox) {
return super.findFromEmail(mailbox);
}
现在我收到以下错误:
WebModule[/LMSWebService]StandardWrapper.Throwable java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:257)
at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:205)
at org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:435)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:261)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:167)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:349)
at javax.servlet.GenericServlet.init(GenericServlet.java:244)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1583)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1225)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:237)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:246)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:724)
在为restful服务添加方法或我的语法不正确时,我还需要做些什么吗?任何帮助将不胜感激。
提前谢谢。
答案 0 :(得分:0)
好吧,您似乎正在使用JAX-RS(或Jersey参考实现)声明RESTful Web服务。
作为I've posted on another question,当出现初始化错误时,通常会抛出此异常。您正在使用某种生成器(NetBeans向导)的事实表明还有一些其他代码被抛出来进行初始化。
JAX-RS是一个优秀的RESTful规范,Jersey是参考实现。写得很好well documented。所以我建议去阅读文档,特别是the initialization part,这样你就可以将问题追溯到它的根目录。
答案 1 :(得分:0)
在我的情况下,问题是有阴影的jar不包含Jersey用来进行字节码操作的javassist软件包。确保包括org.javassist:*。适用于所有范围。
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.26.0-GA</version>
</dependency>