JAXRS客户端找不到邮件正文编写器

wvp*_*wvp 4 java json cxf jax-rs

我有一个像这样配置的jaxrs客户端:

<jaxrs:client id="opaRestProxy" name="opaRestProxy"
        address="${endpoint}" serviceClass="com.test.RestProxy"
        inheritHeaders="true" threadSafe="true">
        <jaxrs:headers>
            <entry key="Accept" value="application/json" />
            <entry key="Content-Type" value="application/json" />
        </jaxrs:headers>
    </jaxrs:client>
Run Code Online (Sandbox Code Playgroud)

但是当我发送请求时,我得到以下异常:

Caused by: org.apache.cxf.interceptor.Fault: .No message body writer has been found for class : class com.test.RequestObject, ContentType : application/json.
    at org.apache.cxf.jaxrs.client.ClientProxyImpl$BodyWriter.handleMessage(ClientProxyImpl.java:646)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
    at org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:527)
    ... 47 more
Run Code Online (Sandbox Code Playgroud)

我的RestProxy类看起来像这样:

@Component
public interface RestProxy {

  @POST
  @Path("/getSomething")
  String getSomething(RequestObject RequestObject);
}
Run Code Online (Sandbox Code Playgroud)

sai*_*ath 7

如果您使用的是Jackson JSON库,则需要将这些xml标记添加到应用程序上下文中.

<jaxrs:providers>
<bean id="jacksonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
</jaxrs:providers>
Run Code Online (Sandbox Code Playgroud)

如果您正在使用任何其他库,请将该bean添加到providers标记.希望有所帮助!

  • @ Blamkin86你有没有找到杰克逊2的解决方案? (2认同)