无法提取响应:找不到适合响应类型的 HttpMessageConverter [英] Could not extract response: no suitable HttpMessageConverter found for response type

查看:52
本文介绍了无法提取响应:找不到适合响应类型的 HttpMessageConverter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 spring 集成的新手,并在 spring 集成 http 模块中为我的项目需求工作.我作为 http 客户端从出站网关发送请求.我正在尝试向服务器发起请求,服务器应该使用我的设置值返回消息负载.我正在使用发送到服务器将对象转换为 JSON我正在从如下所示的客户端(HttpClientDemo)向服务器端的入站网关发送请求.为此,我将我的对象转换为 JSON,然后在客户端将对象转换为 JSON 字符串,在那里执行一些简单的操作并将其发送回客户端(HttpClientDemo),但在此之前,我遇到了与HttpMessageConverter 如下:

线程main"org.springframework.web.client.RestClientException 中的异常:无法提取响应:找不到适合响应类型 [class com.mycompany.MyChannel.model.FFSampleResponseHttp] 和内容类型 [的 HttpMessageConverter文本/纯文本;字符集=UTF-8]在 org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:108)在 org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:784)在 org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:769)在 org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:549)在 org.springframework.web.client.RestTemplate.execute(RestTemplate.java:517)在 org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:462)在 org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:421)在 org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:170)在 org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)在 org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)在 org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:101)在 org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97)在 org.springframework.integration.channel.AbstractSubscribablMyChannel.doSend(AbstractSubscribablMyChannel.java:77)在 org.springframework.integration.channel.AbstractMessagMyChannel.send(AbstractMessagMyChannel.java:255)在 org.springframework.integration.channel.AbstractMessagMyChannel.send(AbstractMessagMyChannel.java:223)在 org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:114)在 org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:44)在 org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:93)

请在下面找到相关代码:

客户端代码:HttpClientDemo.java

公共类HttpClientDemo {私有静态记录器记录器 = Logger.getLogger(HttpClientDemo.class);公共静态无效主(字符串 [] args){ApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration/http-outbound-config.xml");RequestGateway requestGateway = context.getBean("requestGateway", RequestGateway.class);FFSampleRequestHttp FFSampleRequesthttp = new FFSampleRequestHttp();FFSampleRequesthttp.setMyChannelID("1");FFSampleRequesthttp.setMyNumber("88");FFSampleRequesthttp.setReferenceID("9I");FFSampleRequesthttp.setTemplateType(1);FFSampleRequesthttp.setTimestamp("今天");FFSampleResponseHttp 回复 = requestGateway.FFSampleResponsegatway(FFSampleRequesthttp);logger.info("回复:" + reply);}}

我的请求网关如下:RequestGateway.java

公共接口 RequestGateway {FFSampleResponseHttp FFSampleResponsegatway(FFSampleRequestHttp request);}

http-outbound-config.xml

<int:channel id="requestChannel"/><int:channel id="requestChannel1"/><!-- com.mycompany.MyChannel.model.FFSampleResponseHttp --><int-http:outbound-gateway request-channel="requestChannel1" reply-channel="replyChannel1" url="http://localhost:8080/MyChannel_prj-1.0.0.BUILD-SNAPSHOT/receiveGateway" http-method=POST"extract-request-payload="true" expected-response-type="com.mycompany.MyChannel.model.FFSampleResponseHttp"/><int:object-to-json-transformer input-channel="requestChannel" output-channel="requestChannel1" content-type="application/json" result-type="STRING"/><bean id="FFSampleRequestHttp" class="com.mycompany.MyChannel.model.FFSampleRequestHttp"></bean></豆类>

Web.xml

<小服务程序><servlet-name>MyChannel-http</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/servlet-config.xml</param-value></init-param><启动时加载>2</启动时加载><servlet-mapping><servlet-name>MyChannel-http</servlet-name><url-pattern>/receiveGateway</url-pattern></servlet-mapping><欢迎文件列表><welcome-file>index.html</welcome-file></welcome-file-list></web-app>

servlet-config.xml

 <int-http:inbound-gateway request-channel="receivMyChannel" path="/receiveGateway" supported-methods="POST"/><int:service-activator input-channel="receivMyChannel"><bean class="com.mycompany.MyChannel.serviceImpl.FFSampleHttpImpl"><constructor-arg ref = "FFSampleRequestHttp"></constructor-arg></bean></int:service-activator><bean id="FFSampleRequestHttp" class="com.mycompany.MyChannel.model.FFSampleRequestHttp"></bean><bean id="FFSampleResponseHttp" class="com.mycompany.MyChannel.model.FFSampleResponseHttp"></bean></豆类>公共类 FFSampleHttpImpl{私有静态 org.apache.log4j.Logger 日志 = Logger.getLogger(FFSampleImpl.class);@自动连线FFSampleRequestHttp 请求;公共 FFSampleHttpImpl() {}公共 FFSampleHttpImpl(FFSampleRequestHttp 请求) {超级();this.request = 请求;}public String issueResponseFor(String str) 抛出 JsonParseException, JsonMappingException, IOException {ObjectMapper mapper = new ObjectMapper();FFSampleRequestHttp FFSampleRequestHttp = mapper.readValue(new String(str), FFSampleRequestHttp.class);FFSampleRequestHttp.setReferenceID("你好我的号码");String strs = new String();strs = mapper.writeValueAsString(FFSampleRequestHttp);返回字符串;}}

FFSampleRequestHttp.java

公共类FFSampleRequestHttp {受保护的字符串 MyNumber;受保护的字符串引用ID;受保护的字符串 myChannelID;受保护的字符串时间戳;受保护的 int 模板类型;公共字符串 getMyNumber() {返回我的号码;}public void setMyNumber(String MyNumber) {this.MyNumber = MyNumber;}公共字符串 getReferenceID() {返回参考ID;}public void setReferenceID(String referenceID) {this.referenceID = referenceID;}公共字符串 getMyChannelID() {返回 myChannelID;}public void setMyChannelID(String myChannelID) {this.myChannelID = myChannelID;}公共字符串 getTimestamp() {返回时间戳;}公共无效setTimestamp(字符串时间戳){this.timestamp = 时间戳;}公共 int getTemplateType() {返回模板类型;}public void setTemplateType(int templateType) {this.templateType = templateType;}}

FFSampleResponseHttp.java

公共类FFSampleResponseHttp {受保护的字符串 MyNumber;受保护的字符串引用ID;受保护的字符串 myChannelID;受保护的字符串时间戳;受保护的 int 模板类型;公共字符串 getMyNumber() {返回我的号码;}public void setMyNumber(String MyNumber) {this.MyNumber = MyNumber;}公共字符串 getReferenceID() {返回参考ID;}public void setReferenceID(String referenceID) {this.referenceID = referenceID;}公共字符串 getMyChannelID() {返回 myChannelID;}public void setMyChannelID(String myChannelID) {this.myChannelID = myChannelID;}公共字符串 getTimestamp() {返回时间戳;}公共无效setTimestamp(字符串时间戳){this.timestamp = 时间戳;}公共 int getTemplateType() {返回模板类型;}public void setTemplateType(int templateType) {this.templateType = templateType;}}

当我运行上面的代码时,出现以下错误:

16:55:46.843 [main] DEBUG osweb.client.RestTemplate - 编写 [{"MyNumber":"88","re​​ferenceID":"9I","myChannelID":"1","timestamp":"today","templateType":1}] 使用 [org.springframework.http.converter.StringHttpMessageConverter@7d31a3e2] 作为text/plain;charset=UTF-8"16:55:46.988 [main] DEBUG o.s.web.client.RestTemplate - http://localhost:8080/MyChannel_prj-1.0.0.BUILD-SNAPSHOT/receiveGateway"的 POST 请求导致 200 (OK)线程main" org.springframework.web.client.RestClientException 中的异常:无法提取响应:找不到适合响应类型 [class com.mycompany.MyChannel.model.FFSampleResponseHttp] 和内容类型 [text/plain;charset= 的 HttpMessageConverterUTF-8]在 org.springframework.web.client.HttpMessageConverterExtractor.

我使用了 spring 集成基本示例代码作为参考.请提供您的意见.我还尝试在配置文件中使用 spring 对象映射器和 JSON 到对象转换器,但随后我也遇到了 HttpMessageConverter 的类似问题.请帮助我提供您宝贵的意见/建议,如果我们对 Spring 集成 http 对象映射器有任何限制,请告诉我.

<小时>

你好阿尔乔姆,感谢您的回复.我仍然面临下面提到的一些挑战.我已经按照您的建议对我的配置文件进行了更改.但在使用 Jackson2JsonObjectMapper 时遇到问题,需要您的进一步帮助.请在下面找到问题描述.

我对文件进行了更改,现在文件如下所示:我的 Servlet-Config.xml 文件内容如下:

<int:channel id="channel2"/><int:channel id="channel3"/><int-http:inbound-gateway request-channel="channel1" supported-methods="POST" path="/receiveGateway"/>- <int:service-activator input-channel="channel2">- <bean class="com.myCompany.myChannel.serviceImpl.FFSampleHttpImpl"><constructor-arg ref="ffSampleRequestHttp"/></bean></int:service-activator><int:json-to-object-transformer input-channel="channel1" output-channel="channel2" type="com.myCompany.myChannel.model.FFSampleRequestHttp" object-mapper="jackson2JsonObjectMapper"/><bean id="jackson2JsonObjectMapper" class="org.springframework.integration.support.json.Jackson2JsonObjectMapper"/><bean id="ffSampleRequestHttp" class="com.myCompany.myChannel.model.FFSampleRequestHttp"/><bean id="ffSampleResponseHttp" class="com.myCompany.myChannel.model.FFSampleResponseHttp"/></豆类>

出站文件配置(负责向服务器发送消息的文件):

<int:channel id="requestChannel"/><int:channel id="requestChannel1"/><int:object-to-json-transformer input-channel="requestChannel" output-channel="requestChannel1" content-type="application/json"/><int-http:outbound-gateway request-channel="requestChannel1" reply-channel="channel4" url="http://localhost:8080/myChannel_prj-1.0.0.BUILD-SNAPSHOT/http/receiveGateway" http-方法=POST"/><bean id="FFSampleRequestHttp" class="com.myCompany.myChannel.model.FFSampleRequestHttp"/><int:json-to-object-transformer input-channel="channel4" output-channel="requestChannel" type="com.myCompany.myChannel.model.FFSampleResponseHttp" object-mapper="jackson2JsonObjectMapper"/><bean id="jackson2JsonObjectMapper" class="org.springframework.integration.support.json.Jackson2JsonObjectMapper"/></豆类>

我的impl类方法如下:

public FfSampleResponseHttp issueResponseFor(FfSampleRequestHttp request) {FfSampleResponseHttp ffSampleResponse2 = new FfSampleResponseHttp();ffSampleResponse2.setCifNumber("Yappi 我在方法中");log.info("issueResponseFor(FfSampleRequesthttp request)");返回 ffSampleResponse2;}

我可以从客户端调用我的服务方法 issueResponseFor 存在于服务器端,但是当这进一步处理时:

Caused by: java.lang.IllegalArgumentException: 'json' argument must be an instance of: [class java.lang.String, class [B, class java.io.File, class java.net.URL, 类 java.io.InputStream, 类 java.io.Reader]在 org.springframework.integration.support.json.Jackson2JsonObjectMapper.fromJson(Jackson2JsonObjectMapper.java:93)在 org.springframework.integration.support.json.Jackson2JsonObjectMapper.fromJson(Jackson2JsonObjectMapper.java:44)在 org.springframework.integration.support.json.AbstractJacksonJsonObjectMapper.fromJson(AbstractJacksonJsonObjectMapper.java:55)在 org.springframework.integration.json.JsonToObjectTransformer.doTransform(JsonToObjectTransformer.java:78)在 org.springframework.integration.transformer.AbstractTransformer.transform(AbstractTransformer.java:33)... 54 更多

我在调试时已验证,在成功漫游我的服务方法后,响应时的有效负载正文在 Jackson2JsonObjectMapper.fromJson(...) 的参数中的 json 对象中变为空白.我无法理解我在哪里做错了.请提供您的帮助/意见.如果我的配置文件中再次丢失某些内容,请再次告诉我.非常感谢您的支持.

解决方案

因为你返回到客户端只是 String 和它的 content type == 'text/plain',默认转换器没有任何机会确定如何将 String 响应转换为 FFSampleResponseHttp 对象.

修复它的简单方法:

  • 中删除 expected-response-type
  • 添加到replyChannel1

否则您应该编写自己的 HttpMessageConverter 将字符串转换为适当的对象.

要使其与 MappingJackson2HttpMessageConverter(默认转换器之一)和您的 expected-response-type 一起使用,您应该使用 content type = ' 发送回复application/json'.

如果有需要,只需在您的之后和发送回复之前添加;int-http:inbound-gateway>.

因此,选择哪种解决方案取决于您,但由于与默认配置不一致,您当前的状态不起作用.

更新

好的.由于您更改了服务器以将 FfSampleResponseHttp 对象作为 HTTP 响应返回,而不是 String,因此只需在发送 HTTP 和 contentType = 'application/json' 标头>MappingJackson2HttpMessageConverter 将为您完成这些工作 - 您的对象将被转换为 JSON 并带有正确的 contentType 标头.

从客户端你应该回到 expected-response-type="com.mycompany.MyChannel.model.FFSampleResponseHttp"MappingJackson2HttpMessageConverter 应该做的东西你又来了.

当然,您应该在 之后从消息流中删除 .>

I am new with spring integration and working in spring integration http module for my project requirement. I am sending request from outbound gateway as a http client. I am trying to initiate a request to the server and server should return me the message payload with my set values. I am converting object to JSON using to send to server I am sending a request to inbound gateway present on the server side from client(HttpClientDemo) shown below. For that purpose, I am converting my object into the JSON and then converting to JSON string to object on the client side, performing some simple operation there and sending it back to the client(HttpClientDemo) but before this, I am getting exception related to HttpMessageConverter as below:

Exception in thread "main" org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.mycompany.MyChannel.model.FFSampleResponseHttp] and content type [text/plain;charset=UTF-8]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:108)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:784)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:769)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:549)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:517)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:462)
    at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:421)
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:170)
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
    at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:101)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97)
    at org.springframework.integration.channel.AbstractSubscribablMyChannel.doSend(AbstractSubscribablMyChannel.java:77)
    at org.springframework.integration.channel.AbstractMessagMyChannel.send(AbstractMessagMyChannel.java:255)
    at org.springframework.integration.channel.AbstractMessagMyChannel.send(AbstractMessagMyChannel.java:223)
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:114)
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:44)
    at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:93)

Please find Below related code :

Client side code: HttpClientDemo.java

public class HttpClientDemo {

    private static Logger logger = Logger.getLogger(HttpClientDemo.class);
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration/http-outbound-config.xml");
RequestGateway requestGateway = context.getBean("requestGateway", RequestGateway.class);        
        FFSampleRequestHttp FFSampleRequesthttp = new FFSampleRequestHttp();
        FFSampleRequesthttp.setMyChannelID("1");
        FFSampleRequesthttp.setMyNumber("88");
        FFSampleRequesthttp.setReferenceID("9I");
        FFSampleRequesthttp.setTemplateType(1);
        FFSampleRequesthttp.setTimestamp("today");
        FFSampleResponseHttp  reply = requestGateway.FFSampleResponsegatway(FFSampleRequesthttp);
            logger.info("Replied with: " + reply);
    }

}

My Request Gateway is as follows: RequestGateway.java

public interface RequestGateway {


    FFSampleResponseHttp FFSampleResponsegatway(FFSampleRequestHttp request);

}

http-outbound-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:int-http="http://www.springframework.org/schema/integration/http"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">

    <int:gateway id="requestGateway" 
                 service-interface="com.mycompany.MyChannel.Common.RequestGateway"
                 default-request-channel="requestChannel"/>

    <int:channel id="requestChannel"/>
    <int:channel id="requestChannel1"/>


<!--    com.mycompany.MyChannel.model.FFSampleResponseHttp -->

    <int-http:outbound-gateway request-channel="requestChannel1" reply-channel="replyChannel1" url="http://localhost:8080/MyChannel_prj-1.0.0.BUILD-SNAPSHOT/receiveGateway"  http-method="POST"  extract-request-payload="true" expected-response-type="com.mycompany.MyChannel.model.FFSampleResponseHttp"/>

   <int:object-to-json-transformer  input-channel="requestChannel" output-channel="requestChannel1" content-type="application/json"  result-type="STRING"/>



   <bean id="FFSampleRequestHttp" class="com.mycompany.MyChannel.model.FFSampleRequestHttp"></bean>


</beans>

Web.xml

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<servlet>
    <servlet-name>MyChannel-http</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/servlet-config.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyChannel-http</servlet-name>
    <url-pattern>/receiveGateway</url-pattern>
   </servlet-mapping>


    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>

servlet-config.xml

    <int:channel id="receivMyChannel"/>

<int-http:inbound-gateway request-channel="receivMyChannel" path="/receiveGateway" supported-methods="POST"/>

    <int:service-activator input-channel="receivMyChannel">
        <bean class="com.mycompany.MyChannel.serviceImpl.FFSampleHttpImpl">
        <constructor-arg ref = "FFSampleRequestHttp"></constructor-arg>
        </bean>
        </int:service-activator>


     <bean id="FFSampleRequestHttp" class="com.mycompany.MyChannel.model.FFSampleRequestHttp"></bean>
     <bean id="FFSampleResponseHttp" class="com.mycompany.MyChannel.model.FFSampleResponseHttp"></bean>

    </beans> 



public class FFSampleHttpImpl{

    private static org.apache.log4j.Logger log = Logger
            .getLogger(FFSampleImpl.class);

    @Autowired
    FFSampleRequestHttp request;
    public FFSampleHttpImpl() {
    }

    public FFSampleHttpImpl(FFSampleRequestHttp request) {
        super();
        this.request = request;
    }





    public String issueResponseFor(String str) throws JsonParseException, JsonMappingException, IOException {

        ObjectMapper mapper = new ObjectMapper();

        FFSampleRequestHttp FFSampleRequestHttp = mapper.readValue(new String(str), FFSampleRequestHttp.class);

        FFSampleRequestHttp.setReferenceID("Hi My Number");

        String  strs = new String();

        strs = mapper.writeValueAsString(FFSampleRequestHttp);

            return strs;

        }

}

FFSampleRequestHttp.java

public class FFSampleRequestHttp {
    protected String MyNumber;  
    protected String referenceID;   
    protected String myChannelID;
    protected String timestamp;
    protected int templateType;
    public String getMyNumber() {
        return MyNumber;
    }
    public void setMyNumber(String MyNumber) {
        this.MyNumber = MyNumber;
    }
    public String getReferenceID() {
        return referenceID;
    }
    public void setReferenceID(String referenceID) {
        this.referenceID = referenceID;
    }
    public String getMyChannelID() {
        return myChannelID;
    }
    public void setMyChannelID(String myChannelID) {
        this.myChannelID = myChannelID;
    }
    public String getTimestamp() {
        return timestamp;
    }
    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }
    public int getTemplateType() {
        return templateType;
    }
    public void setTemplateType(int templateType) {
        this.templateType = templateType;
    }
    }

FFSampleResponseHttp.java

public class FFSampleResponseHttp {
    protected String MyNumber;
    protected String referenceID;
    protected String myChannelID;
    protected String timestamp;
    protected int templateType;

    public String getMyNumber() {
        return MyNumber;
    }
    public void setMyNumber(String MyNumber) {
        this.MyNumber = MyNumber;
    }
    public String getReferenceID() {
        return referenceID;
    }
    public void setReferenceID(String referenceID) {
        this.referenceID = referenceID;
    }
    public String getMyChannelID() {
        return myChannelID;
    }
    public void setMyChannelID(String myChannelID) {
        this.myChannelID = myChannelID;
    }
    public String getTimestamp() {
        return timestamp;
    }
    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }
    public int getTemplateType() {
        return templateType;
    }
    public void setTemplateType(int templateType) {
        this.templateType = templateType;
    }
    }

When I run the above code I get following error:

16:55:46.843 [main] DEBUG o.s.web.client.RestTemplate - Writing [{"MyNumber":"88","referenceID":"9I","myChannelID":"1","timestamp":"today","templateType":1}] as "text/plain;charset=UTF-8" using [org.springframework.http.converter.StringHttpMessageConverter@7d31a3e2]
16:55:46.988 [main] DEBUG o.s.web.client.RestTemplate - POST request for "http://localhost:8080/MyChannel_prj-1.0.0.BUILD-SNAPSHOT/receiveGateway" resulted in 200 (OK)
Exception in thread "main" org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.mycompany.MyChannel.model.FFSampleResponseHttp] and content type [text/plain;charset=UTF-8]
    at org.springframework.web.client.HttpMessageConverterExtractor. 

I have used spring integration basic sample code for reference. Please provide your input. I also tried by using the spring object mapper in the configuration files with JSON to object transformer but then also I am getting similer issues for HttpMessageConverter. Please help me with your valuable inputs/suggestion and let me know if we have any limitation with spring integration http object mapper.


Hi Artem, Thanks for your reply. I am still facing some challenges mentioned below. I have done the changes in my configuration files as per of your suggestion. but facing issue when using Jackson2JsonObjectMapper and need your further help. Please find below issue description.

I have done changes in my files and now files are like below: My Servlet-Config.xml file content is as below:

<int:channel id="channel1" /> 
<int:channel id="channel2" /> 
<int:channel id="channel3" /> 
<int-http:inbound-gateway request-channel="channel1" supported-methods="POST" path="/receiveGateway" /> 
- <int:service-activator input-channel="channel2"> 
- <bean class="com.myCompany.myChannel.serviceImpl.FFSampleHttpImpl"> 
<constructor-arg ref="ffSampleRequestHttp" /> 
</bean> 
</int:service-activator> 

<int:json-to-object-transformer input-channel="channel1" output-channel="channel2" type="com.myCompany.myChannel.model.FFSampleRequestHttp" object-mapper="jackson2JsonObjectMapper" /> 

<bean id="jackson2JsonObjectMapper" class="org.springframework.integration.support.json.Jackson2JsonObjectMapper" /> 
<bean id="ffSampleRequestHttp" class="com.myCompany.myChannel.model.FFSampleRequestHttp" /> 
<bean id="ffSampleResponseHttp" class="com.myCompany.myChannel.model.FFSampleResponseHttp" /> 
</beans>

Out bound file config(file which is responsible to sent message to server):

<int:gateway id="requestGateway" service-interface="com.myCompany.myChannel.Common.RequestGateway" default-request-channel="requestChannel" /> 
  <int:channel id="requestChannel" /> 
  <int:channel id="requestChannel1" /> 
  <int:object-to-json-transformer input-channel="requestChannel" output-channel="requestChannel1" content-type="application/json" /> 
  <int-http:outbound-gateway request-channel="requestChannel1" reply-channel="channel4" url="http://localhost:8080/myChannel_prj-1.0.0.BUILD-SNAPSHOT/http/receiveGateway" http-method="POST" /> 
  <bean id="FFSampleRequestHttp" class="com.myCompany.myChannel.model.FFSampleRequestHttp" /> 
  <int:json-to-object-transformer input-channel="channel4" output-channel="requestChannel" type="com.myCompany.myChannel.model.FFSampleResponseHttp" object-mapper="jackson2JsonObjectMapper" /> 
  <bean id="jackson2JsonObjectMapper" class="org.springframework.integration.support.json.Jackson2JsonObjectMapper" /> 
  </beans>

My impl class method is as below:

public  FfSampleResponseHttp issueResponseFor(FfSampleRequestHttp request) {

        FfSampleResponseHttp ffSampleResponse2 = new FfSampleResponseHttp();

        ffSampleResponse2.setCifNumber("Yappi I am in the method");
        log.info("issueResponseFor(FfSampleRequesthttp request)");

        return ffSampleResponse2;

    }

I am able to call my service method issueResponseFor present in server side from the client but when this is processing further:

Caused by: java.lang.IllegalArgumentException: 'json' argument must be an instance of: [class java.lang.String, class [B, class java.io.File, class java.net.URL, class java.io.InputStream, class java.io.Reader]
       at org.springframework.integration.support.json.Jackson2JsonObjectMapper.fromJson(Jackson2JsonObjectMapper.java:93)
       at org.springframework.integration.support.json.Jackson2JsonObjectMapper.fromJson(Jackson2JsonObjectMapper.java:44)
       at org.springframework.integration.support.json.AbstractJacksonJsonObjectMapper.fromJson(AbstractJacksonJsonObjectMapper.java:55)
       at org.springframework.integration.json.JsonToObjectTransformer.doTransform(JsonToObjectTransformer.java:78)
       at org.springframework.integration.transformer.AbstractTransformer.transform(AbstractTransformer.java:33)
       ... 54 more

I have verified while debugging that the payload body while in response is coming blank in json object in the parameter of Jackson2JsonObjectMapper.fromJson(…) after roaming through my service method successfully. I am not able to understand where am I doing the mistake. Please provide your help/input. Again let me know if I am again missing something in my config files. Thank you very much for your support.

解决方案

Since you return to the client just String and its content type == 'text/plain', there is no any chance for default converters to determine how to convert String response to the FFSampleResponseHttp object.

The simple way to fix it:

  • remove expected-response-type from <int-http:outbound-gateway>
  • add to the replyChannel1 <json-to-object-transformer>

Otherwise you should write your own HttpMessageConverter to convert the String to the appropriate object.

To make it work with MappingJackson2HttpMessageConverter (one of default converters) and your expected-response-type, you should send your reply with content type = 'application/json'.

If there is a need, just add <header-enricher> after your <service-activator> and before sending a reply to the <int-http:inbound-gateway>.

So, it's up to you which solution to select, but your current state doesn't work, because of inconsistency with default configuration.

UPDATE

OK. Since you changed your server to return FfSampleResponseHttp object as HTTP response, not String, just add contentType = 'application/json' header before sending the response for the HTTP and MappingJackson2HttpMessageConverter will do the stuff for you - your object will be converted to JSON and with correct contentType header.

From client side you should come back to the expected-response-type="com.mycompany.MyChannel.model.FFSampleResponseHttp" and MappingJackson2HttpMessageConverter should do the stuff for you again.

Of course you should remove <json-to-object-transformer> from you message flow after <int-http:outbound-gateway>.

这篇关于无法提取响应:找不到适合响应类型的 HttpMessageConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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