Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ClientRequest,如何序列化POJO';s是否正确地转换为json数据?找不到内容类型应用程序/json类型的编写器:_Java_Json_Serialization_Resteasy - Fatal编程技术网

Java ClientRequest,如何序列化POJO';s是否正确地转换为json数据?找不到内容类型应用程序/json类型的编写器:

Java ClientRequest,如何序列化POJO';s是否正确地转换为json数据?找不到内容类型应用程序/json类型的编写器:,java,json,serialization,resteasy,Java,Json,Serialization,Resteasy,对于REST系列(RESTeasy),我创建了如下JUnit测试用例: @Test public void a100_insertAddressTest() throws Exception { Address addr = new Address(1, "testStreet", "1", (short) 1234, "testCity"); ClientRequest request = new ClientRequest(BASE_URL + "custome

对于REST系列(RESTeasy),我创建了如下JUnit测试用例:

@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<String> response = request.post(String.class);

    Assert.assertEquals(201, response.getStatus());

    response.releaseConnection();
    request.clear();
}
因此,我创建了一个
MessageBodyWriter
,如果我使用firefox插件
RESTClient
进行POST请求,它可以正常工作:

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JsonMsbWriter implements MessageBodyWriter<Address> {

    @Override
    public boolean isWriteable(Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType) {
        if(type == Address.class){
            return true;
        }
        return false;
    }

    @Override
    public long getSize(Address t, Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType) {
        return -1;
    }

    @Override
    public void writeTo(Address t, Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType,
            MultivaluedMap<String, Object> httpHeaders,
            OutputStream entityStream) throws IOException,
            WebApplicationException {

        ObjectMapper m = new ObjectMapper();
        m.writeValue(entityStream, t);

    }
}
  • 如何为json附加这样的编写器
  • 为什么它适用于内容类型
    应用程序/xml
    之外的应用程序 盒子

  • 对于GET请求和
    application/json
    也存在同样的问题,但是读者而不是作者的问题已经解决。

    问题已经解决,不幸的是,我没有在项目构建路径中包含所有必要的第三方库。最后,我添加了以下jar:

  • jackson-core-asl-1.6.3.jar
  • jackson-jaxrs-1.6.3.jar
  • jackson-mapper-asl-1.6.3.jar
  • jackson-xc-1.6.3.jar
  • resteasy-jackson-provider-2.2.1.GA.jar
  • 这个例子将Maven的力量带到了最前面。对于Maven,实现这些依赖关系只需要一个条目

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>2.2.1.GA</version>
    </dependency>
    
    
    org.jboss.resteasy
    resteasy jackson提供商
    2.2.1.GA
    
    @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<String> response = request.post(String.class);
    
        Assert.assertEquals(201, response.getStatus());
    
        response.releaseConnection();
        request.clear();
    }
    
    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)
    
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>2.2.1.GA</version>
    </dependency>