- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我是 spring 集成的新手,并且在 spring 集成 http 模块中工作以满足我的项目需求。我作为 http 客户端从出站网关发送请求。我正在尝试向服务器发起请求,服务器应该使用我的设置值向我返回消息有效负载。我正在将对象转换为 JSON 用于发送到服务器我正在从客户端(HttpClientDemo)向服务器端的入站网关发送请求,如下所示。为此,我将我的对象转换为 JSON,然后在客户端将 JSON 字符串转换为对象,在那里执行一些简单的操作并将其发送回客户端(HttpClientDemo),但在此之前,我遇到了与HttpMessageConverter如下:
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)
请在下面找到相关代码:
客户端代码: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);
}
}
我的请求网关如下: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;
}
}
当我运行上面的代码时,我得到以下错误:
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.
我使用了spring集成基本示例代码供引用。请提供您的意见。我还尝试在配置文件中使用带有 JSON 到对象转换器的 spring 对象映射器,但我也遇到了 HttpMessageConverter 的类似问题。请帮助我提供宝贵的意见/建议,如果我们对 Spring 集成 http 对象映射器有任何限制,请告诉我。
嗨,Artem,感谢您的回复。我仍然面临下面提到的一些挑战。根据您的建议,我已对配置文件进行了更改。但在使用 Jackson2JsonObjectMapper 时遇到问题,需要您的进一步帮助。请在下面找到问题描述。
我已经对我的文件进行了更改,现在文件如下所示:我的 Servlet-Config.xml 文件内容如下:
<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>
出站文件配置(负责向服务器发送消息的文件):
<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>
我的 impl 类方法如下:
public FfSampleResponseHttp issueResponseFor(FfSampleRequestHttp request) {
FfSampleResponseHttp ffSampleResponse2 = new FfSampleResponseHttp();
ffSampleResponse2.setCifNumber("Yappi I am in the method");
log.info("issueResponseFor(FfSampleRequesthttp request)");
return 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, 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
我在调试时验证,在成功漫游我的服务方法后,响应中的有效负载主体在 Jackson2JsonObjectMapper.fromJson(...) 的参数中的 json 对象中变为空白。我无法理解我在哪里做错了。请提供您的帮助/意见。再次让我知道我是否在我的配置文件中再次丢失了某些内容。非常感谢您的支持。
最佳答案
正如 Artem Bilan 所说,出现此问题是因为 MappingJackson2HttpMessageConverter
仅支持使用 application/json 内容类型的响应。如果您不能更改服务器代码,但可以更改客户端代码(我有这种情况),您可以使用拦截器更改内容类型 header :
restTemplate.getInterceptors().add((request, body, execution) -> {
ClientHttpResponse response = execution.execute(request,body);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
return response;
});
关于spring - 无法提取响应 : no suitable HttpMessageConverter found for response type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24723394/
我一直遇到 mySQL 数据库连接问题。我收到一个错误: No suitable driver found for jdbc:mysql://127.0.0.1/sakila. 我已经安装了 mySQ
我正在尝试编写一个带有嵌入式 Derby 的程序,但是当我运行它时,它显示: run: Jul 14, 2017 9:19:54 PM gfdh.Login Doconnect SEVERE: nul
我正在尝试在转换 String 的 List 后对 Integer 的 List 进行排序转换为Integer List。它给了我这个错误: no suitable method found for
我能够很好地部署我的 war ,当我尝试运行它时,我在控制台日志中收到一些运行时错误 Cannot create JDBC driver of class 'oracle.jdbc.OracleDri
我正在开始 Android 编程。我无法编译我的程序,因为出现以下错误: 错误:没有找到适合 ArrayAdapter() 的构造函数 这是我的个人适配器: public class StationL
我正在尝试解码 Base64 图像并将其放入 WPF 图像源中。但是,我使用的代码有一个错误: No imaging component suitable to complete this opera
我在客户端和服务器上都设置了使用 Jaxb2 的 MarshallingMessageConverter 关注 this问题。 这是在服务器端: @Configuration @EnableWebMv
我们正在将 Struts 1.2 项目从 JDK 1.6 迁移到 1.8,并将应用程序服务器 jboss5.1 迁移到 tomcat 8.5。所以我们开始将 JNDI 数据源链接到 tomcat,但似
我是 java 新手,正在尝试为 Minecraft 制作模组,但我不知道如何修复此错误: src\minecraft\net\minecraft\src\ThreadConnectToServer.
我最近一直在做一个项目,在这个项目中我最终使用了一个扩展另一个类(即连接和传输)的类。我收到的错误是“错误:没有找到适合 Connection 的构造函数(无参数)”。错误是在 Transfer 中构
我正在尝试使用 JDBC 接收器连接器将 Kafka 与 Postgres 接收器结合使用。 异常: INFO Unable to connect to database on attempt 1/3
这个问题已经有答案了: Connect Java to a MySQL database (14 个回答) 已关闭 6 年前。 java.sql.SQLException: No suitable d
我正在制作一个简单的注册程序,当我单击“提交”到我的 IDE 日志时,我收到此错误: java.sql.SQLException: No suitable driver found for jdbc:
我在使用 java 和 jdbc 时遇到了一些问题。特别是,虽然我的代码在 NetBeans 项目中完美运行,但当我尝试在终端或 ubuntu vps(这是我需要它工作的地方)上执行它时,我总是遇到此
我有一个带有图像 uploader 的网站,每当用户尝试上传图像时,他们都会收到此错误消息: “没有合适的节点可以满足您的请求。” 我已经联系了托管公司(mosso),他们说这对他们来说没什么。知道导
美好的一天! 我知道有很多关于此类问题的帖子,但由于我使用的是嵌入式 Derby,所以我查看了其中一些帖子但找不到问题的答案。 我收到这个错误: ##THIS IS GENERATED BY THE
我正在编写一个必须调用休息服务的 spring mvc 应用程序(Spring 新手)。我在我的 VM(Linux 中的 weblogic 10.3.6)中部署了其余服务,我正在编写的应用程序在我的本
我正在使用 GUICE 进行依赖项注入(inject),以使用 Dropwizard 构建 RESTful API。这是我收到的错误: com.google.inject.ConfigurationE
您好!终于开始学习 C,我认为是时候开始使用调试器了。此时我使用 Gvim 作为编辑器,使用命令行进行编译。在尝试了几个调试器(KDbg、ddd、insight)之后,运行 gdb 似乎是目前最简单和
我正在实现他们文档中给出的 firebase 示例。我面临这个错误: com.fasterxml.jackson.databind.JsonMappingException: No suitable
我是一名优秀的程序员,十分优秀!