RestEasy 客户端提供找不到内容类型应用程序/xml 类型的编写器

Posted

技术标签:

【中文标题】RestEasy 客户端提供找不到内容类型应用程序/xml 类型的编写器【英文标题】:RestEasy Client giving could not find writer for content-type application/xml type 【发布时间】:2020-04-24 12:08:25 【问题描述】:

我正在为我的端点编写一个测试客户端,但它无法执行提供的连接

EXCEPTION : could not find writer for content-type application/xml type: com.gepower.gees.ifs.goet.porequest.model.CreatePOReq

在行中

ClientResponse<CreatePOResp> clientResponse = client.post(CreatePOResp.class);

从 Postman,端点返回结果很好。 我试图在我的项目中添加所有 RestEasy jar,但它仍然给出同样的错误,而

public static CreatePOResp createUpdateALFPO(CreatePOReq req) 
    CreatePOResp response = new CreatePOResp();
    String operation = "createUpdatePO";

    try 

        String xmlReq = "";
        try 
            xmlReq = XmlUtils.marshalXmlToString(req);
            System.out.println(operation + " - XML SOAP REQUEST: " + xmlReq);
         catch (Exception xmlex) 
            System.out.println(xmlex.getMessage());
        
        ClientRequest client = new ClientRequest("http://localhost/ofsrestws/fs/porequest/addpo");

        client.body(MediaType.APPLICATION_XML, req);
        System.out.println("URI ====" + client.getUri());
        ClientResponse<CreatePOResp> clientResponse = client.post(CreatePOResp.class);
        ResteasyProviderFactory.getInstance().addBuiltInMessageBodyReader(new JAXBXmlTypeProvider());

        if (clientResponse.getStatus() == 200) 
            response = clientResponse.getEntity();
            System.out.println("web service is OK");
            return response;
         else 
            System.out.println("an issue has occure during the call of the web service: "
                    + clientResponse.getResponseStatus());
            // Object resp = clientResponse.getResponseStatus();

            return null;
        
     catch (NullPointerException e) 
        System.out.println("EXCEPTION : " + e.getMessage());
        return null;
     catch (Exception e) 
        System.out.println("EXCEPTION : " + e.getMessage());
        return null;
     finally 

堆栈跟踪:

java.lang.RuntimeException: could not find writer for content-type application/xml type: com.gepower.gees.ifs.goet.porequest.model.CreatePOReq
    at org.jboss.resteasy.client.ClientRequest.writeRequestBody(ClientRequest.java:409)
    at org.jboss.resteasy.client.core.executors.ApacheHttpClientExecutor$ClientRequestEntity.<init>(ApacheHttpClientExecutor.java:117)
    at org.jboss.resteasy.client.core.executors.ApacheHttpClientExecutor.loadHttpMethod(ApacheHttpClientExecutor.java:188)
    at org.jboss.resteasy.client.core.executors.ApacheHttpClientExecutor.execute(ApacheHttpClientExecutor.java:56)
    at org.jboss.resteasy.client.ClientRequest.execute(ClientRequest.java:378)
    at org.jboss.resteasy.client.ClientRequest.httpMethod(ClientRequest.java:590)
    at org.jboss.resteasy.client.ClientRequest.post(ClientRequest.java:496)
    at org.jboss.resteasy.client.ClientRequest.post(ClientRequest.java:501)
    at org.tcs.TestJava.createUpdateALFPO(TestJava.java:52)
    at org.tcs.TestJava.main(TestJava.java:31)

请找到依赖列表

【问题讨论】:

这通常是依赖问题。你确定你的类路径中有 resteasy-jaxb-provider 吗? 看看这里:***.com/questions/40111268/… 如果这不能解决问题,请告诉我们您使用的依赖项。 正如您在屏幕截图中看到的 -resteasy-jaxb-provider-3.0.9.Final.jar 在那里 添加依赖列表 【参考方案1】:

我唯一能看到的是您在发出 post 请求后注册了适配器:

ClientResponse<CreatePOResp> clientResponse = client.post(CreatePOResp.class);
ResteasyProviderFactory.getInstance().addBuiltInMessageBodyReader(new JAXBXmlTypeProvider());

由于post调用中出现异常,或许你应该尝试切换顺序。

我注意到您正在使用已弃用的 RestEasy 客户端 API。请注意,JAX-RS 2.0 客户端实现将自动加载所有可用的 提供者类(通过服务加载器机制)。也许你应该考虑改用那个 API。

【讨论】:

以上是关于RestEasy 客户端提供找不到内容类型应用程序/xml 类型的编写器的主要内容,如果未能解决你的问题,请参考以下文章

org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure:找不到响应对象类型的 MessageBodyWriter

RESTEASY003900:找不到公共构造函数 - 我错过了啥?

RestEasy 客户端所需的 jar

Quarkus 找不到内容类型多部分/表单数据休息客户端的编写器

错误:在提供程序包中找不到类型“SingleChildCloneableWidget”

在 SqlServer 提供程序清单中找不到存储类型“Varchar(100)”