gpt4 book ai didi

java - 为什么在启动 REST 服务时添加注释 FormDataParam 会引发异常?

转载 作者:行者123 更新时间:2023-12-01 19:33:28 26 4
gpt4 key购买 nike

Jersey documentation (版本 2.29.1)具有以下示例(示例 9.50。@FormDataParam 注释的使用):

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String postForm(
@DefaultValue("true") @FormDataParam("enabled") boolean enabled,
@FormDataParam("data") FileData bean,
@FormDataParam("file") InputStream file,
@FormDataParam("file") FormDataContentDisposition fileDisposition) {

// ...
}

就我而言,方法如下:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/test")
public Response saveFiles(
@FormDataParam("file") InputStream file,
@FormDataParam("file") FormDataContentDisposition fileDisposition
){
return Response.ok().build();
}

当服务器启动时(即,它甚至没有到达方法调用),我收到以下错误(使用 Apache Tomcat/8.5.16):

Type Exception Report

Message Servlet.init() for servlet [rest.ApplicationConfig] threw exception

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: Servlet.init() for servlet [rest.ApplicationConfig] threw exception org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:748)

Root Cause

java.lang.IllegalStateException: The resource configuration is not modifiable in this context. org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:246) org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:193) org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:426) org.glassfish.jersey.servlet.WebComponent.(WebComponent.java:306) org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:154) org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:346) javax.servlet.GenericServlet.init(GenericServlet.java:158) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:748)

Note The full stack trace of the root cause is available in the server logs.

此外,如果删除@FormDataParam("file")InputStream file@FormDataParam("file")FormDataContentDisposition fileDisposition,则服务器正常启动:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/test")
public Response saveFiles(){
return Response.ok().build();
}

服务器本身的配置不使用 web.xml,如果重要的话:

@ApplicationPath("/")
public class ApplicationConfig extends Application {

}

这样试了一下,结果是一模一样的:

@ApplicationPath("/")
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig(){
register(MultiPartFeature.class);
}
}

问题是什么以及如何解决?

最佳答案

确实,问题出在 ApplicationConfig 类中。
首先,该类位于 rest 包中。
其次,需要将此包添加到 ApplicationConfig 类的构造函数中。

package rest;

@ApplicationPath("/")
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig(){
register(MultiPartFeature.class);
packages("rest");
}
}

关于java - 为什么在启动 REST 服务时添加注释 FormDataParam 会引发异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59242831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com