Java JAXRS 客户端找不到消息正文编写器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24653329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:49:48  来源:igfitidea点击:

JAXRS client can't find message body writer

javajsoncxfjax-rs

提问by wvp

I have a jaxrs client configured like this:

我有一个配置如下的 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>

But when I send a request I get the following exception:

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

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

My RestProxy class looks like this:

我的 RestProxy 类如下所示:

@Component
public interface RestProxy {

  @POST
  @Path("/getSomething")
  String getSomething(RequestObject RequestObject);
}

回答by saibharath

If you are using Hymanson JSON library you need to add these xml tags to your application context.

如果您使用 Hymanson JSON 库,您需要将这些 xml 标签添加到您的应用程序上下文中。

<jaxrs:providers>
<bean id="HymansonProvider" class="org.codehaus.Hymanson.jaxrs.HymansonJsonProvider" />
</jaxrs:providers>

If you are using any other library add that bean to the providers tag. Hope that helps!

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

回答by user902997

If you are consuming using javax.ws.rs.client.Client, please register the provider using client.register(new HymansonJsonProvider());

如果您正在使用 using javax.ws.rs.client.Client,请使用注册提供程序client.register(new HymansonJsonProvider());

回答by cabaji99

This answers point me in the right direction, yet i had to add on two parts to make it work on web.xml

这个答案为我指明了正确的方向,但我必须添加两部分才能使其在 web.xml 上工作

 <init-param>
        <param-name>jaxrs.providers</param-name>
        <param-value>
            org.codehaus.Hymanson.jaxrs.HymansonJaxbJsonProvider
            (writeXsiType=false)
        </param-value>
    </init-param>

And on the client call:

在客户端调用中:

List<Object> providers = new ArrayList<>();
    // add custom providers if any
    providers.add(new HymansonJaxbJsonProvider());
    WebClient client = WebClient.create(ENDPOINT_ADDRESS,providers);