【问题标题】:jersey text/html message body writer missing缺少球衣文本/html 消息正文作者
【发布时间】:2012-09-10 19:57:58
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: jaxb jersey


    【解决方案1】:

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

    下面是我给出的答案的链接,其中包括一个示例MessageBodyReader

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      相关资源
      最近更新 更多