0

I have several other services that are working, but they would fail if i include the below method to upload a file:

@POST
@Path("/image2")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadImage(@FormDataParam("file") InputStream uploadedInputStream,
                            @FormDataParam("file") FormDataContentDisposition fileDetails)
        throws ServletException, IOException {

    System.out.println(fileDetails.getFileName());
    return Response.ok().build();
}

enter image description here

Without the above method, other services work normally.

enter image description here

Here's my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <display-name>Talent Core</display-name>
 <servlet>
   <servlet-name>javax.ws.rs.core.Application</servlet-name>
 </servlet>
 <servlet-mapping>
   <servlet-name>javax.ws.rs.core.Application</servlet-name>
   <url-pattern>/service/*</url-pattern>
  </servlet-mapping>
  <listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
  </listener>

  <filter>
    <filter-name>CorsFilter</filter-name>
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
      <init-param>
        <param-name>cors.allowed.origins</param-name>
        <param-value>*</param-value>
      </init-param>
      <init-param>
        <param-name>cors.allowed.methods</param-name>
        <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
      </init-param>
      <init-param>
        <param-name>cors.allowed.headers</param-name>
        <param-value>Content-Type,X-Requested-With,Accept,Accept-Encoding,Accept-Language,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Connection,Host,authorization</param-value>
      </init-param>
      <init-param>
        <param-name>cors.exposed.headers</param-name>
        <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
      </init-param>
    </filter>

    <filter-mapping>
      <filter-name>CorsFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

    <resource-env-ref>
     <resource-env-ref-name>BeanManager</resource-env-ref-name>
     <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
    </resource-env-ref>
 </web-app>

Environment:

  • Jdk 8
  • Tomcat 9

I tried as suggested here MULTIPART_FORM_DATA: No injection source found for a parameter of type public javax.ws.rs.core.Response but the same error.

Suren Konathala
  • 3,497
  • 5
  • 43
  • 73

2 Answers2

1

It sounds like this post might be applicable:

Why would “java.lang.IllegalStateException: The resource configuration is not modifiable in this context.” appear deploying Jersey app?

In my case, I had a Jersey POST resource for file uploads. The resource specified the parameter:

@FormDataParam("file") InputStream file

and consumed

MediaType.MULTIPART_FORM_DATA

To fix the issue, I had to add the following to the Jersey REST configuration in my web.xml file:

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>

ORIGINAL PROBLEM: HTTP 500 doing file upload under Tomcat/Jersey: java.lang.IllegalStateException: The resource configuration is not modifiable in this context.

SOLUTION: Add org.glassfish.jersey.media.multipart.MultiPartFeature to web.xml.

NEW PROBLEM: 404- Not Found

SUGGESTIONS:

  1. Step into the debugger, and see if your code is even getting to uploadImage().

  2. If it is, print the filename and filepath from fileDetails to System.out. Make sure that fileDetails isn't null, and that the file actually exists.

  3. Otherwise, set a breakpoint in the code that's calling "uploadImage()", and make sure it's passing a valid filename and filepath to upload.

Please post back what you find.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I'm getting a 404- Not Found after including this ` javax.ws.rs.core.Application jersey.config.server.provider.classnames org.glassfish.jersey.media.multipart.MultiPartFeature ` – Suren Konathala Feb 19 '19 at 23:41
  • I tried as suggested.. i added sysouts but nothing prints. None of the services work if i have the `@FormDataParam` method. If i comment all services work normally. – Suren Konathala Feb 20 '19 at 16:27
  • Sigh... Statements like "Nothing prints" and "None of the services work" are unhelpfully vague. I believe the original problem, "HTTP 500: java.lang.IllegalStateException: The resource configuration is not modifiable in this context." is resolved (by adding "MultiPartFeature" to web.xml). Correct? I assume invoking /image2 is still causing "HTTP 404- Not Found", correct? Q: Are you using an IDE (for example, Eclipse)? Have you tried the Eclipse debugger? NEXT STEP (if the logs and if the Eclipse debugger don't help): write an [MCVE]. – paulsm4 Feb 20 '19 at 17:24
  • I have several other services (cruds - get,post), and they all work without including the imageUpload method. If i enable/uncomment it, then none of the other services work. That's what i meant. I'm building a WAR file and deploying it to Tomcat manually so not sure if debugging will work. Tomcat not running via IDE. I'm trying to do debug where this is missing by adding one method/param at a time now. Will update here if i can solve this. – Suren Konathala Feb 20 '19 at 17:28
0

Here's a solution that worked. I was using web.xml to register the class and for somereason that was not working. I changed to use Application instead of web.xml

Added a new class ApplicationConfig under root (/tf-core/src/main/java/com/tf/core):

import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.media.multipart.MultiPartFeature;

@ApplicationPath("service") // set the path to REST web services
public class ApplicationConfig extends ResourceConfig {
  public ApplicationConfig() {
    packages("com.tf.core").register(MultiPartFeature.class);
  }
}

Servlet class (at /tf-core/src/main/java/com/tf/core/servlet):

@POST
@Path("/image")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public ResponseBean uploadImage(@FormDataParam("file") InputStream uploadedInputStream,
                                @FormDataParam("file") FormDataContentDisposition fileDetails) {
    System.out.println(fileDetails.getFileName());
    ...
}

pom.xml

<dependency>
  <groupId>org.glassfish.jersey.media</groupId>
  <artifactId>jersey-media-multipart</artifactId>
  <version>2.27</version>
</dependency>

Full example on Github

Suren Konathala
  • 3,497
  • 5
  • 43
  • 73