我的Servlet和@FormDataParam Jersey

时间:2015-04-28 15:45:15

标签: java rest jersey multipartform-data jersey-2.0

我使用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.access$500(ApplicationHandler.java:167)
at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:327)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
at org.glassfish.jersey.internal.Errors$2.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>

我不知道你是否完全理解我的问题,我尽量保持清醒。

提前致谢

1 个答案:

答案 0 :(得分:2)

您需要在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>
  • 查看问题here

  • 的说明
  • 请参阅其他配置选项here