title: Error creating MultiPart Response in Jax-rs Jersey
body: <p>I am trying to return a Multipart response in a <code>@GET</code> call. No matter what I seem to do I always receive the same error:</p>
  
  <blockquote>
    <p>"Cannot get entity on a MultiPart instance (through reference chain: org.glassfish.jersey.media.multipart.MultiPart["entity"])"</p>
  </blockquote>
  
  <p>I have <code>jersey-media-multipart</code> referenced in my <code>pom.xml</code>. I have tried adding references, removing references to no avail. I am using the (test) code below to create the <code>Response</code>:</p>
  
  <pre class="lang-java prettyprint-override"><code>return Response.ok(new MultiPart()
                          .bodyPart("This is the first segment", new MediaType("text", "plain"))
                          .bodyPart("&lt;outer&gt;&lt;inner&gt;value&lt;/inner&gt;&lt;/outer&gt;", new MediaType("text", "xml"))).build();
  </code></pre>
  
  <p>I have the method annotated with:</p>
  
  <pre><code>@GET
  @Produces({"application/json","multipart/mixed"})
  </code></pre>
  
  <p>as it can also return JSON under some conditions. Removing the <code>@Produces</code> completely or changing to only <code>"multipart/mixed"</code> does not alter the error message. </p>
  
  <p>I also have an application class that registers the <code>MultiPartFeature.class</code>. I must add that I have a <code>@POST</code> method in this class in which I use <code>MultiPart</code> to retrieve <code>multipart/mixed</code> data without a problem. The relevant sections of the application class are as follows:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class OpApplication extends ResourceConfig
  {
      public OpApplication()
      {
          // make the REST classes available
          super(Resource classes listed here...);
  
          // register the filters
          register(AuthorizationRequestFilter.class);
          register(AuthorizationResponseFilter.class);
  
          // register the multipart package for attachments
          packages("org.glassfish.jersey.examples.multipart");
          register(MultiPartFeature.class);
          register(JacksonFeature.class);
      }
  }
  </code></pre>
  
  <p>The @GET method:</p>
  
  <pre><code>@GET
  @Produces({"application/json","multipart/mixed"})
  public Response getData(@Context UriInfo ui)
  {
      boolean is_multipart = false;
  
      // retrieve the parameter map
      MultivaluedMap&lt;String, String&gt; queryParams = ui.getQueryParameters();
  
      ParameterValidator parameterValidator = new ParameterValidator();
  
      // validate any existing parameters
      if (!parameterValidator.getCallParametersAreValid(queryParams))
      {
          return Response.status(Response.Status.BAD_REQUEST).build();
  
      }
      else   // check for the presence of parameter requesting a multipart response
      {
          is_multipart = parameterValidator.is_multipart();
      }
  
  
      JSONObject json = getDataAsJson(queryParams);
  
      if (json == null || json.length() == 0)
      {
          return Response.status(Response.Status.NOT_FOUND).build();
      }
      else
      {
          // BodyParts would be gathered here. JSON and a file is most likely output
        /*  MultiPart objMultiPart = new MultiPart();
          BodyPart coverageBodyPart = new BodyPart().
                  type(MediaType.APPLICATION_JSON_TYPE).
                  entity(json);
          objMultiPart.getBodyParts().add(coverageBodyPart);
        */
          if (is_multipart)
              {
                 // simple test
                  return Response.ok(new MultiPart()
                          .bodyPart("This is the first segment", new MediaType("text", "plain"))
                          .bodyPart("&lt;outer&gt;&lt;inner&gt;value&lt;/inner&gt;&lt;/outer&gt;", new MediaType("text", "xml"))).build();
               }
              else
              {
                  return Response.ok(json.toString(), MediaType.APPLICATION_JSON).build();
              }
      }
  }
  </code></pre>
  
  <p>The contents of the <code>pom.xml</code> file:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  http://maven.apache.org/maven-v4_0_0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;opls&lt;/groupId&gt;
      &lt;artifactId&gt;oprest&lt;/artifactId&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;version&gt;6.7.5&lt;/version&gt;
      &lt;name&gt;OPREST&lt;/name&gt;
      &lt;properties&gt;
          &lt;jdk.version&gt;1.8&lt;/jdk.version&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;!-- PROJECT SPECIFIC DEPENDENCIES --&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;com.google.guava&lt;/groupId&gt;
              &lt;artifactId&gt;guava&lt;/artifactId&gt;
              &lt;version&gt;14.0.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.javassist&lt;/groupId&gt;
              &lt;artifactId&gt;javassist&lt;/artifactId&gt;
              &lt;version&gt;3.18.1-GA&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.annotation&lt;/groupId&gt;
              &lt;artifactId&gt;javax.annotation-api&lt;/artifactId&gt;
              &lt;version&gt;1.2&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.ws.rs&lt;/groupId&gt;
              &lt;artifactId&gt;javax.ws.rs-api&lt;/artifactId&gt;
              &lt;version&gt;2.0&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-media-json-jackson&lt;/artifactId&gt;
              &lt;version&gt;2.25.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
              &lt;version&gt;2.8.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-container-servlet&lt;/artifactId&gt;
              &lt;version&gt;2.25.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.security&lt;/groupId&gt;
              &lt;artifactId&gt;oauth1-server&lt;/artifactId&gt;
              &lt;version&gt;2.25.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.security&lt;/groupId&gt;
              &lt;artifactId&gt;oauth1-signature&lt;/artifactId&gt;
              &lt;version&gt;2.25.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-media-multipart&lt;/artifactId&gt;
              &lt;version&gt;2.25.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.json&lt;/groupId&gt;
              &lt;artifactId&gt;json&lt;/artifactId&gt;
              &lt;version&gt;20151123&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;com.github.wnameless&lt;/groupId&gt;
              &lt;artifactId&gt;json-flattener&lt;/artifactId&gt;
              &lt;version&gt;0.4.0&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.osgi&lt;/groupId&gt;
              &lt;artifactId&gt;org.osgi.core&lt;/artifactId&gt;
              &lt;version&gt;4.2.0&lt;/version&gt;
          &lt;/dependency&gt;
  
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.hk2&lt;/groupId&gt;
              &lt;artifactId&gt;osgi-resource-locator&lt;/artifactId&gt;
              &lt;version&gt;1.0.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.validation&lt;/groupId&gt;
              &lt;artifactId&gt;validation-api&lt;/artifactId&gt;
              &lt;version&gt;1.1.0.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.ehcache&lt;/groupId&gt;
              &lt;artifactId&gt;ehcache&lt;/artifactId&gt;
              &lt;version&gt;3.1.0&lt;/version&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
                      &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
              &lt;/dependencies&gt;
              &lt;build&gt;
                  &lt;finalName&gt;oplrs-${project.version}&lt;/finalName&gt;
                  &lt;sourceDirectory&gt;src/&lt;/sourceDirectory&gt;
                  &lt;plugins&gt;
                      &lt;plugin&gt;
                          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                          &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                          &lt;version&gt;2.2&lt;/version&gt;
                          &lt;configuration&gt;
                              &lt;webXml&gt;web/WEB-INF/web.xml&lt;/webXml&gt;
                              &lt;webResources&gt;
                                  &lt;resource&gt;
                                      &lt;directory&gt;web&lt;/directory&gt;
                                  &lt;/resource&gt;
                              &lt;/webResources&gt;
  
                              &lt;archive&gt;
                                  &lt;manifest&gt;
                                      &lt;addDefaultImplementationEntries&gt;true&lt;/addDefaultImplementationEntries&gt;
                                      &lt;addDefaultSpecificationEntries&gt;true&lt;/addDefaultSpecificationEntries&gt;
                                  &lt;/manifest&gt;
                              &lt;/archive&gt;
                          &lt;/configuration&gt;
                      &lt;/plugin&gt;
                      &lt;plugin&gt;
                          &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
                          &lt;version&gt;2.6&lt;/version&gt;
                          &lt;executions&gt;
                              &lt;execution&gt;
                                  &lt;id&gt;copy-resources&lt;/id&gt;
                                  &lt;!-- here the phase you need --&gt;
                          &lt;phase&gt;compile&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;copy-resources&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;outputDirectory&gt;${basedir}/target/classes/&lt;/outputDirectory&gt;
                              &lt;resources&gt;
                                  &lt;resource&gt;
                                      &lt;directory&gt;src/&lt;/directory&gt;
                                      &lt;includes&gt;
                                          &lt;include&gt;*.ccf&lt;/include&gt;
                                      &lt;/includes&gt;
                                      &lt;filtering&gt;true&lt;/filtering&gt;
                                  &lt;/resource&gt;
                              &lt;/resources&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.2&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.8&lt;/source&gt;
                      &lt;target&gt;1.8&lt;/target&gt;
                      &lt;debug&gt;true&lt;/debug&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>In response to the code example (thanks!) from Sergey I have created a new, simple endpoint. It still gives the same error:</p>
  
  <pre><code>import org.glassfish.jersey.media.multipart.MultiPart;
  import org.glassfish.jersey.media.multipart.MultiPartMediaTypes;
  
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.Context;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.Response;
  import javax.ws.rs.core.UriInfo;
  
  @Path("/test")
  public class Test
  {
      @GET
      @Produces({MediaType.APPLICATION_JSON, MultiPartMediaTypes.MULTIPART_MIXED})
      public Response getData(@Context UriInfo ui) {
          final boolean condition = false;
          if (condition) {
              return Response.ok("{}", MediaType.APPLICATION_JSON).build();
          }
  
          return Response.ok(
                  new MultiPart()
                          .bodyPart("This is the first segment", MediaType.TEXT_PLAIN_TYPE)
                          .bodyPart("&lt;outer&gt;&lt;inner&gt;value&lt;/inner&gt;&lt;/outer&gt;", MediaType.TEXT_XML_TYPE),
                  MultiPartMediaTypes.MULTIPART_MIXED_TYPE
          ).build();
      }
  }
  </code></pre>