我的 Servlet 和 @FormDataParam Jersey 有问题
Probleme with my Servlet and @FormDataParam Jersey
我使用 Jersey 的网络服务有问题。我有一个由两个输入组成的表单,一个是 "file" 类型,一个是 "text".
类型
问题是,当我只使用 "file" 类型的输入时,一切正常,但是当我添加第二个 "text" 类型的输入时,我的 servlet 甚至无法启动正确。
这是我启动 servlet 时遇到的异常:
avr. 28, 2015 5:16:17 PM org.apache.catalina.core.StandardContext loadOnStartup
GRAVE: La servlet /SemRecSysWS a généré une exception "load()"
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public java.lang.String fr.checksem.semrecsys.Gestion.insertSKOS(java.io.InputStream,org.openrdf.rio.RDFFormat) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[text/plain; charset=utf-8], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class fr.checksem.semrecsys.Gestion, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@1a533161]}, definitionMethod=public java.lang.String fr.checksem.semrecsys.Gestion.insertSKOS(java.io.InputStream,org.openrdf.rio.RDFFormat), parameters=[Parameter [type=class java.io.InputStream, source=fichier, defaultValue=null], Parameter [type=class org.openrdf.rio.RDFFormat, source=Format, defaultValue=null]], responseType=class java.lang.String}, nameBindings=[]}']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:500)
at org.glassfish.jersey.server.ApplicationHandler.access0(ApplicationHandler.java:167)
at org.glassfish.jersey.server.ApplicationHandler.run(ApplicationHandler.java:327)
at org.glassfish.jersey.internal.Errors.call(Errors.java:289)
at org.glassfish.jersey.internal.Errors.call(Errors.java:286)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:324)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:315)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:170)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:358)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3954)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
at java.lang.Thread.run(Unknown Source)
这是我尝试访问文件和文本的函数
@POST
@Path("/initSKOS")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN + ";charset=utf-8")
public String insertSKOS(@FormDataParam("fichier") InputStream uploadedInputStream, @FormDataParam("Format") RDFFormat format) {
....
}
我已经为我的变量格式尝试了每种类型,但这并没有改变任何东西。
这是我的 web.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="SemRecSysWS" version="3.0">
<display-name>RecWS</display-name>
<servlet>
<servlet-name>fr.checksem.semrecsys.TestWS</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>fr.checksem.semrecsys</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>fr.checksem.semrecsys.TestWS</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
不知道你是否完全理解我的问题,我尽量说清楚了。
提前致谢
您需要在 web.xml 中注册 MultiPartFeature
。您只需将 <init-param>
添加到 Jersey servlet
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
看问题的解释
查看其他配置选项
我使用 Jersey 的网络服务有问题。我有一个由两个输入组成的表单,一个是 "file" 类型,一个是 "text".
类型问题是,当我只使用 "file" 类型的输入时,一切正常,但是当我添加第二个 "text" 类型的输入时,我的 servlet 甚至无法启动正确。
这是我启动 servlet 时遇到的异常:
avr. 28, 2015 5:16:17 PM org.apache.catalina.core.StandardContext loadOnStartup
GRAVE: La servlet /SemRecSysWS a généré une exception "load()"
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public java.lang.String fr.checksem.semrecsys.Gestion.insertSKOS(java.io.InputStream,org.openrdf.rio.RDFFormat) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[text/plain; charset=utf-8], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class fr.checksem.semrecsys.Gestion, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@1a533161]}, definitionMethod=public java.lang.String fr.checksem.semrecsys.Gestion.insertSKOS(java.io.InputStream,org.openrdf.rio.RDFFormat), parameters=[Parameter [type=class java.io.InputStream, source=fichier, defaultValue=null], Parameter [type=class org.openrdf.rio.RDFFormat, source=Format, defaultValue=null]], responseType=class java.lang.String}, nameBindings=[]}']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:500)
at org.glassfish.jersey.server.ApplicationHandler.access0(ApplicationHandler.java:167)
at org.glassfish.jersey.server.ApplicationHandler.run(ApplicationHandler.java:327)
at org.glassfish.jersey.internal.Errors.call(Errors.java:289)
at org.glassfish.jersey.internal.Errors.call(Errors.java:286)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:324)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:315)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:170)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:358)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3954)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
at java.lang.Thread.run(Unknown Source)
这是我尝试访问文件和文本的函数
@POST
@Path("/initSKOS")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN + ";charset=utf-8")
public String insertSKOS(@FormDataParam("fichier") InputStream uploadedInputStream, @FormDataParam("Format") RDFFormat format) {
....
}
我已经为我的变量格式尝试了每种类型,但这并没有改变任何东西。
这是我的 web.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="SemRecSysWS" version="3.0">
<display-name>RecWS</display-name>
<servlet>
<servlet-name>fr.checksem.semrecsys.TestWS</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>fr.checksem.semrecsys</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>fr.checksem.semrecsys.TestWS</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
不知道你是否完全理解我的问题,我尽量说清楚了。
提前致谢
您需要在 web.xml 中注册 MultiPartFeature
。您只需将 <init-param>
添加到 Jersey servlet
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
看问题的解释
查看其他配置选项