title: 'Illegal attempt to call getOutputStream() after getWriter() has already been called' during Jersey test
body: <p>I have a Jersey endpoint that I am trying to test.</p>
  
  <p>Endpoint:</p>
  
  <pre><code>@Path("image")
  class ImageResource {
  
      @Inject
      private ImageService imageService;
  
      @POST
      @Produces(MediaType.APPLICATION_JSON)
      @Consumes(MediaType.APPLICATION_JSON)
      public Response save(ImageVO imageVO) {
          imageVO = imageService.save(imageVO);
          ResponseVO responseVO = new ResponseVO(true, imageVO);
          return Response.status(Response.Status.CREATED).entity(responseVO).build();
      }
  }
  </code></pre>
  
  <p>Test:</p>
  
  <pre><code>class ImageResourceTest extends JerseyTest {
  
      private static final String BASE_ENDPOINT = "rest";
      private static final String IMAGE_ENDPOINT = "image";
  
      @Mock
      private ImageService imageService;
  
      @InjectMocks
      private ImageResource imageResource;
  
      @Override
      protected URI getBaseUri() {
          return UriBuilder.fromUri(super.getBaseUri()).path(BASE_ENDPOINT).build();
      }
  
      @Override
      protected Application configure() {
          MockitoAnnotations.initMocks(this);
  
          ResourceConfig config = new ResourceConfig();
          config.register(imageResource);
          return config;
      }
  
      @Test
      public void testSave() {
          ImageVO imageVO = new ImageVO();
          imageVO.setId("TEST");
          when(imageService.save(imageVO)).thenReturn(imageVO);
  
          Entity e = Entity.entity(imageVO, MediaType.APPLICATION_JSON);
          Response response = target().path(IMAGE_ENDPOINT).request().post(e, Response.class);
          assertEquals(201, response.getStatus());
  
          ResponseVO responseVO = response.readEntity(ResponseVO.class);
          assertTrue(responseVO.isSuccess());
      }
  }
  </code></pre>
  
  <p>And the exception: </p>
  
  <pre><code>Jul 13, 2015 9:14:28 AM org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory$GrizzlyTestContainer start
  INFO: Starting GrizzlyTestContainer...
  Jul 13, 2015 9:14:28 AM org.glassfish.grizzly.http.server.NetworkListener start
  INFO: Started listener bound to [localhost:9998]
  Jul 13, 2015 9:14:28 AM org.glassfish.grizzly.http.server.HttpServer start
  INFO: [HttpServer] Started.
  Jul 13, 2015 9:14:28 AM org.glassfish.jersey.server.ServerRuntime$Responder process
  SEVERE: Error occurred when processing a response created from an already mapped exception.
  InboundJaxrsResponse{ClientResponse{method=POST, uri=http://localhost:9998/rest/image, status=500, reason=Request failed.}}
  Jul 13, 2015 9:14:28 AM org.glassfish.jersey.server.ServerRuntime$Responder release
  WARNING: Attempt to release request processing resources has failed for a request.
  java.lang.IllegalStateException: Illegal attempt to call getOutputStream() after getWriter() has already been called.
      at org.glassfish.grizzly.http.server.Response.getNIOOutputStream(Response.java:621)
      at org.glassfish.grizzly.http.server.Response.getOutputStream(Response.java:646)
      at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer$ResponseWriter.writeResponseStatusAndHeaders(GrizzlyHttpContainer.java:265)
      at org.glassfish.jersey.server.ServerRuntime$Responder$1.getOutputStream(ServerRuntime.java:561)
      at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:200)
      at org.glassfish.jersey.message.internal.CommittingOutputStream.flushBuffer(CommittingOutputStream.java:305)
      at org.glassfish.jersey.message.internal.CommittingOutputStream.commit(CommittingOutputStream.java:261)
      at org.glassfish.jersey.message.internal.CommittingOutputStream.close(CommittingOutputStream.java:276)
      at org.glassfish.jersey.message.internal.OutboundMessageContext.close(OutboundMessageContext.java:835)
      at org.glassfish.jersey.server.ContainerResponse.close(ContainerResponse.java:411)
      at org.glassfish.jersey.server.ServerRuntime$Responder.release(ServerRuntime.java:667)
      at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:438)
      at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:265)
      at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
      at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
      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.process(Errors.java:267)
      at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:319)
      at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
      at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
      at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:363)
      at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:217)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
      at java.lang.Thread.run(Thread.java:619)
  Jul 13, 2015 9:14:28 AM org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory$GrizzlyTestContainer stop
  INFO: Stopping GrizzlyTestContainer...
  Jul 13, 2015 9:14:28 AM org.glassfish.grizzly.http.server.NetworkListener shutdownNow
  INFO: Stopped listener bound to [localhost:9998]
  </code></pre>
  
  <p>I have been beating my head against the wall with this for a couple of days, but can't find out the solution. I have multiple other endpoints that I test the same way, that do not have this issue. I'm using Jersey 2.6 with Java 6u5.</p>