我有一个像这样配置的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>
但是当我发送请求时,我得到以下异常:
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
我的RestProxy类看起来像这样:
@Component
public interface RestProxy {
@POST
@Path("/getSomething")
String getSomething(RequestObject RequestObject);
}
答案 0 :(得分:6)
如果您使用的是Jackson JSON库,则需要将这些xml标记添加到应用程序上下文中。
<jaxrs:providers>
<bean id="jacksonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
</jaxrs:providers>
如果您正在使用任何其他库,请将该bean添加到providers标记。希望有所帮助!
答案 1 :(得分:0)
如果您使用javax.ws.rs.client.Client
消费,请使用client.register(new JacksonJsonProvider());
答案 2 :(得分:0)
这个答案指出了我正确的方向,但我必须添加两个部分才能使它在web.xml上运行
<init-param>
<param-name>jaxrs.providers</param-name>
<param-value>
org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider
(writeXsiType=false)
</param-value>
</init-param>
在客户电话上:
List<Object> providers = new ArrayList<>();
// add custom providers if any
providers.add(new JacksonJaxbJsonProvider());
WebClient client = WebClient.create(ENDPOINT_ADDRESS,providers);