Java 资源配置在此上下文实体中不可修改 Resfutl 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22388889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:28:17  来源:igfitidea点击:

The resource configuration is not modifiable in this context entities Resfutl Service

javajpa

提问by Trebor

I'm fairly new to Java and working on my first JPA project while trying to wrap my head around all of the objects that Netbeans created. I created a "User" JPA entity from a database table using the wizard. Then I went to the AbstractFacade.java where the super class exists to add a new method that will find a specific user by their email address.

我对 Java 还很陌生,我正在做我的第一个 JPA 项目,同时试图将我的头脑围绕在 Netbeans 创建的所有对象上。我使用向导从数据库表创建了一个“用户”JPA 实体。然后我转到存在超类的 AbstractFacade.java 添加一个新方法,该方法将通过电子邮件地址查找特定用户。

public T findFromEmail(String mailbox) {
    javax.persistence.Query q = getEntityManager().createNamedQuery("Users.findByEmail").setParameter("email", mailbox);
    return (T)q.getSingleResult();
}

Next I went to UsersFacaceRest.java and added the following method. I'm not sure why I needed the @override in this example as I didn't seem to be overriding any underlying object, but Netbeans gave me an error if I didn't.

接下来我去了 UsersFacaceRest.java 并添加了以下方法。我不确定为什么在这个例子中我需要 @override,因为我似乎没有覆盖任何底层对象,但是如果我没有覆盖,Netbeans 会给我一个错误。

@GET
@Path("{mailbox}")
@Produces({"application/xml", "application/json"})
@Override
public Users findFromEmail(@PathParam("mailbox") String mailbox) {
    return super.findFromEmail(mailbox);
}

Now I receive the following error:

现在我收到以下错误:

    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.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.access0(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)

Is there something else I need to do to add a method to the restful service or is my syntax incorrect? Any help would be greatly appreciated.

我还需要做些什么来向 restful 服务添加方法,还是我的语法不正确?任何帮助将不胜感激。

Thank you in advance.

先感谢您。

回答by sargue

Well, you seem to be declaring a RESTful web service using JAX-RS (or Jersey, the reference implementation).

好吧,您似乎在使用 JAX-RS(或Jersey,参考实现)声明 RESTful Web 服务。

As I've posted on another questionthis exception usually is thrown when there is an initialization error. The fact that you are using some kind of generator (NetBeans wizard) suggest there is some other code thrown arount to do the initialization.

正如我在另一个问题上发布的那样,通常在出现初始化错误时抛出此异常。您正在使用某种生成器(NetBeans 向导)这一事实表明,还有一些其他代码抛出来进行初始化。

JAX-RS is an excellent RESTful specification and Jersey is the reference implementation. It's well written and well documented. So I suggest to go and read the documentation, especially the initialization part, so you can trace your problem to its root.

JAX-RS 是一个优秀的 RESTful 规范,Jersey 是参考实现。它写得很好,有据可查。所以我建议去阅读文档,尤其是初始化部分,这样你就可以追溯到问题的根源。

回答by Yao Li

In my case, the issue is that the shaded jar did not include the javassist package that Jersey uses to do bytecode manipulation. Make sure to include org.javassist:*. for all scopes.

就我而言,问题在于阴影 jar 不包含 Jersey 用于进行字节码操作的 javassist 包。确保包含 org.javassist:*。适用于所有范围。

<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.26.0-GA</version>
</dependency>