title: ClientRequest, how to serialize POJO's to json data correctly? could not find writer for content-type application/json type:
body: <p>For a REST serives (RESTeasy) I have created JUnit test cases like:</p>
  
  <pre><code>@Test
  public void a100_insertAddressTest() throws Exception {
       Address addr = new Address(1, "testStreet", "1", (short) 1234,
       "testCity");
  
      ClientRequest request = new ClientRequest(BASE_URL + "customerID/{id}",
              sslExecutor_schusb);
      request.body(MediaType.APPLICATION_XML, addr).pathParameter(
              "id", 1);
      ClientResponse&lt;String&gt; response = request.post(String.class);
  
      Assert.assertEquals(201, response.getStatus());
  
      response.releaseConnection();
      request.clear();
  }
  </code></pre>
  
  <p>If I change the media type of the request body to "application/json", the test case fails with the error:</p>
  
  <pre><code>java.lang.RuntimeException: could not find writer for content-type application/json type: at.fhj.ase.dao.data.Address
      at org.jboss.resteasy.client.ClientRequest.writeRequestBody(ClientRequest.java:469)
      at org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor.loadHttpMethod(ApacheHttpClient4Executor.java:221)
      at org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor.execute(ApacheHttpClient4Executor.java:107)
      at org.jboss.resteasy.core.interception.ClientExecutionContextImpl.proceed(ClientExecutionContextImpl.java:39)
      at org.jboss.resteasy.plugins.interceptors.encoding.AcceptEncodingGZIPInterceptor.execute(AcceptEncodingGZIPInterceptor.java:40)
      at org.jboss.resteasy.core.interception.ClientExecutionContextImpl.proceed(ClientExecutionContextImpl.java:45)
      at org.jboss.resteasy.client.ClientRequest.execute(ClientRequest.java:443)
      at org.jboss.resteasy.client.ClientRequest.httpMethod(ClientRequest.java:674)
      at org.jboss.resteasy.client.ClientRequest.post(ClientRequest.java:565)
      at org.jboss.resteasy.client.ClientRequest.post(ClientRequest.java:570)
      at at.fhj.ase.business.ServiceAddressImplTest.a100_insertAddressTest(ServiceAddressImplTest.java:59)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:168)
      at org.junit.rules.RunRules.evaluate(RunRules.java:20)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  </code></pre>
  
  <p>Hence I created a <code>MessageBodyWriter</code> which works fine if I do a POST request by using the firefox plugin <code>RESTClient</code>:</p>
  
  <pre><code>@Provider
  @Produces(MediaType.APPLICATION_JSON)
  public class JsonMsbWriter implements MessageBodyWriter&lt;Address&gt; {
  
      @Override
      public boolean isWriteable(Class&lt;?&gt; type, Type genericType,
              Annotation[] annotations, MediaType mediaType) {
          if(type == Address.class){
              return true;
          }
          return false;
      }
  
      @Override
      public long getSize(Address t, Class&lt;?&gt; type, Type genericType,
              Annotation[] annotations, MediaType mediaType) {
          return -1;
      }
  
      @Override
      public void writeTo(Address t, Class&lt;?&gt; type, Type genericType,
              Annotation[] annotations, MediaType mediaType,
              MultivaluedMap&lt;String, Object&gt; httpHeaders,
              OutputStream entityStream) throws IOException,
              WebApplicationException {
  
          ObjectMapper m = new ObjectMapper();
          m.writeValue(entityStream, t);
  
      }
  }
  </code></pre>
  
  <p>and updated the JUnit test case to:</p>
  
  <pre><code>@Test
  public void a100_insertAddressTest() throws Exception {
      Address addr = new Address(1, "testStreet", "1", (short) 1234,
              "testCity");
  
      ResteasyProviderFactory fact = ResteasyProviderFactory.getInstance();
      fact.addMessageBodyWriter(JsonMsbWriter.class);
  
      ClientRequest request = new ClientRequest(BASE_URL + "customerID/{id}",
              sslExecutor_schusb);
      request.body(MediaType.APPLICATION_JSON, addr).pathParameter("id", 1);
      ClientResponse&lt;String&gt; response = request.post(String.class);
  
      Assert.assertEquals(201, response.getStatus());
  
      response.releaseConnection();
      request.clear();
  }
  </code></pre>
  
  <p>But now I get the error:</p>
  
  <pre><code>java.lang.VerifyError: (class: org/codehaus/jackson/map/ObjectMapper, method: writeValueAsBytes signature: (Ljava/lang/Object;)[B) Incompatible argument to function
      at at.fhj.ase.xmlvalidation.msbreader.JsonMsbWriter.writeTo(JsonMsbWriter.java:46)
      at at.fhj.ase.xmlvalidation.msbreader.JsonMsbWriter.writeTo(JsonMsbWriter.java:1)
      at org.jboss.resteasy.core.interception.MessageBodyWriterContextImpl.proceed(MessageBodyWriterContextImpl.java:117)
      at org.jboss.resteasy.plugins.interceptors.encoding.GZIPEncodingInterceptor.write(GZIPEncodingInterceptor.java:100)
      at org.jboss.resteasy.core.interception.MessageBodyWriterContextImpl.proceed(MessageBodyWriterContextImpl.java:123)
      at org.jboss.resteasy.client.ClientRequest.writeRequestBody(ClientRequest.java:472)
      at org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor.loadHttpMethod(ApacheHttpClient4Executor.java:221)
      at org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor.execute(ApacheHttpClient4Executor.java:107)
      at org.jboss.resteasy.core.interception.ClientExecutionContextImpl.proceed(ClientExecutionContextImpl.java:39)
      at org.jboss.resteasy.plugins.interceptors.encoding.AcceptEncodingGZIPInterceptor.execute(AcceptEncodingGZIPInterceptor.java:40)
      at org.jboss.resteasy.core.interception.ClientExecutionContextImpl.proceed(ClientExecutionContextImpl.java:45)
      at org.jboss.resteasy.client.ClientRequest.execute(ClientRequest.java:443)
      at org.jboss.resteasy.client.ClientRequest.httpMethod(ClientRequest.java:674)
      at org.jboss.resteasy.client.ClientRequest.post(ClientRequest.java:565)
      at org.jboss.resteasy.client.ClientRequest.post(ClientRequest.java:570)
      at at.fhj.ase.business.ServiceAddressImplTest.a100_insertAddressTest(ServiceAddressImplTest.java:65)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:168)
      at org.junit.rules.RunRules.evaluate(RunRules.java:20)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  </code></pre>
  
  <ol>
  <li>How can I attach such a writer for json?</li>
  <li>And why does it work for content type <code>application/xml</code> out of the
  box?</li>
  </ol>
  
  <p>For GET requests and <code>application/json</code> there is the same problem, however with the reader instead of the writer.</p>