泽西文/ html消息正文作家遗失

时间:2012-09-18 14:22:39

标签: jaxb jersey

我正在使用jersey客户端向REST服务器发送POST请求。我在请求和响应中都使用了JAXB对象。

public RegisterUserResponse apply(RegisterUserRequest input) {

    logger.debug("sending user authentication request");   
    return webResource.
            path(requestPath).
            path(registerUserPath).
            accept(MediaType.TEXT_HTML).      // set response content type
            type(MediaType.APPLICATION_XML).        // set request content type
            post(RegisterUserResponse.class, input);
}

这里两个RegisterUserRequest / Response类都是xml注释的。

我得到的是响应是一个text / html响应,它是一个xml字符串,格式可以转换回RegistrationResponse

我收到一条错误消息,指出RegistrationResponse和text / html消息正文编写器丢失了。什么可以解决这个问题。

我的异常堆栈跟踪如下

DEBUG RegisterUser - sending user registration request
com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, RegisterUserRequest, and MIME media type, text/html, was not found
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:151)   
    at com.sun.jersey.api.client.Client.handle(Client.java:648)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)
    at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
    at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:568)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:74)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:673)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:846)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1170)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.runWorkers(TestRunner.java:1147)
    at org.testng.TestRunner.privateRun(TestRunner.java:749)
    at org.testng.TestRunner.run(TestRunner.java:600)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:317)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:312)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:274)
    at org.testng.SuiteRunner.run(SuiteRunner.java:223)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1039)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:964)
    at org.testng.TestNG.run(TestNG.java:900)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:110)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:174)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:111)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, RegisterUserRequest, and MIME media type, text/html, was not found
    at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:213)
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149)
    ... 38 more

1 个答案:

答案 0 :(得分:1)

对于text/html媒体类型,JAX-RS / Jersey似乎默认不使用JAXB。您可以利用标准MessageBodyReader / MessageBodyWriter机制来插入您自己对利用JAXB的媒体类型的处理。

以下是我给出的答案的链接,其中包含示例MessageBodyReader