在执行采用多部分参数的休息服务时没有合适的 HttpMessageConverter 发现错误 [英] No suitable HttpMessageConverter found error while executing rest service that takes multipart parameters

查看:33
本文介绍了在执行采用多部分参数的休息服务时没有合适的 HttpMessageConverter 发现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用 Spring Integration.我正在尝试执行一个采用 multipart/formdata 输入参数的休息服务.我正在使用 int-http:outbound-gateway 来执行休息服务.代码如下:

I am using Spring Integration in my project. I am trying to execute a rest service which takes multipart/formdata input parameters. I am using int-http:outbound-gateway to execute rest service. The following is the code:

<int:channel id="PQcreateAttachment-Rest-Channel" />
    <int:chain input-channel="PQcreateAttachment-Rest-Channel"  output-channel="PQcreateAttachment-StoredProcedure-Router" >

        <int:header-filter  header-names="accept-encoding"/>    

         <int:service-activator  ref="httpOutboundGatewayHandler" method="buildMultipartHttpOutboundGatewayRequest" /> 

        <int-http:outbound-gateway  url-expression="headers.restResourceUrl"
                                    http-method-expression="headers.httpMethod"
                                    extract-request-payload="true"
                                    >
        </int-http:outbound-gateway>

        <int:service-activator ref="msgHandler" method="buildMessageFromExtSysResponse" />

    </int:chain>

但是当我执行上述代码时出现以下错误.

But I am getting the following error when I execute the above code.

Caused by: org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.integration.message.GenericMessage] and content type [application/x-java-serialized-object]
    at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:665)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:481)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:460)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:409)
    at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:372)
    ... 121 more

这是准备我的多部分请求的 java 代码:

Here is the java code that prepares my multipart request:

public Message<?> buildMultipartHttpOutboundGatewayRequest(Message<?> inMessage) throws Exception{

        logger.debug(" ************** buildMultipartHttpOutboundGatewayRequest Start *************************");

        String inMsgPayload = (String)inMessage.getPayload();



        SOAXml soaXml = parseSOAXml(inMsgPayload);

        String restURL      = null;
        String contentType  = null;
        String acceptHdr    = null;
        String userId = null;
        String password = null;
        String businessAreaName    = null;
        String typeName   = null;
        String attachmentLocation = null;
        String httpMethod = null;
        Message<?> outMessage = null;
        MessageHeaders inMsgHdrs = null;
        MessageBuilder<?> msgBuild = null;

        String authorization = null;
        //TODO: File location needs to be changed to standard one 
        String fileLocation = "C:\\source.xml";
            //if we reach here means, it is AWD system
            restURL     = getAwdSOAService(soaXml);

        Document document = XmlParserUtil.convertString2Document(inMsgPayload);

         userId = XmlParserUtil.getNodeValue(document,"//userId");
         password = XmlParserUtil.getNodeValue(document,"//PQcreateAttachment/password");
         businessAreaName     = XmlParserUtil.getNodeValue(document,"//businessAreaName");
         typeName = XmlParserUtil.getNodeValue(document,"//typeName");
         httpMethod = XmlParserUtil.getNodeValue(document,"//METHOD");
         attachmentLocation = XmlParserUtil.getNodeValue(document,"//attachmentLocation");

         //Construct source xml 
         //Creating document
           Document sourceDocument = DocumentHelper.createDocument();
         Element sourceInstance = sourceDocument.addElement("createSourceInstance");
         sourceInstance.addAttribute("xmlns", "http://www.dsttechnologies.com/awd/rest/v1");
         Element orderItem=sourceInstance.addElement("businessAreaName");
         orderItem.setText("SAMPLEBA");
         Element orderItemDesc=sourceInstance.addElement("typeName");
         orderItemDesc.setText("SAMPLEST");
        // create source xml file
        XmlParserUtil.createXMLFileUsingDOM4J(sourceDocument, fileLocation);
        authorization = getBasicAuthorization(userId,password);

        Resource source = new ClassPathResource(fileLocation);
        Resource attachment = new ClassPathResource(attachmentLocation);


        Map<String, Object> multipartMap = new HashMap<String, Object>();
        multipartMap.put("source", source);
        multipartMap.put("attachment", attachment);
        logger.info("Created multipart request: " + multipartMap);  


            inMessage = buildMessageForMultipart(multipartMap); 


        //  contentType = csProps.getHttpAwdContentTypeValue();
            acceptHdr   = csProps.getHttpAwdAcceptTypeValue() ;
        //  authorization = getBasicAuthorization(soaXml.getUserid(),decriptPassword(soaXml.getPassword()));

            inMsgHdrs = inMessage.getHeaders();
            msgBuild = MessageBuilder.withPayload(inMessage).copyHeaders(inMsgHdrs);
            msgBuild.removeHeader("Content-Encoding");
            msgBuild.removeHeader("accept-encoding");
            msgBuild.setHeader(csProps.getHttpUrlHdr(), restURL);
            msgBuild.setHeader(csProps.getHttpMethodHdr(), httpMethod);
            msgBuild.setHeader(csProps.getHttpAuthorizatonHdr(),authorization );
//          msgBuild.setHeader(csProps.getHttpContentTypeHdr(), contentType);
//          msgBuild.setHeader(csProps.getHttpAcceptTypeHdr(),acceptHdr);   


        outMessage = msgBuild.build();

        logger.debug(" ************** buildHttpOutboundGatewayRequest End*************************");
        logger.debug(outMessage);
        logger.debug(" ************************************************************************");

        return outMessage;

    }

对这里出了什么问题有什么想法吗?

Any ideas on what's wrong here?

推荐答案

您的问题是因为您将一条消息包装到另一条消息中.

Your problem is because you wrap one message to another.

你的 buildMessageForMultipart(multipartMap); 做了什么?

我确信简单的地图作为有效载荷和那些标头就足够了.

I'm sure the simple map as payload and those header would be enough.

不确定将一条消息包装到另一条消息有什么意义.

Not sure what is the point to wrap one message to another.

这篇关于在执行采用多部分参数的休息服务时没有合适的 HttpMessageConverter 发现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆