Quentin Moreaux
Quentin Moreaux

Reputation: 51

Problem with my Servlet and @FormDataParam Jersey

I have a problem with my web-services using Jersey. I have a form composed of two inputs, one of type "file" and one of type "text".

The problem is that when I only use the input of type "file", everything works fine, but when I add the second one, of type "text", my servlet can't even start properly.

Here is the exception I have when I start my 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)

Here is the function where I try to access to the file and the text

@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) {
....
}

I have tried with every type for my variable format, but that doesn't change anything.

And here is my web.xml file :

<?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>

I don't know if you completely understood my problem, I tried to be as clear as possible.

Upvotes: 3

Views: 1869

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208994

You need to register the MultiPartFeature in your web.xml. You can simply add an <init-param> to the 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>
  • See explanation of problem here

  • See other configuring options here

Upvotes: 2

Related Questions