我正在将tcp-client-server
的Spring Integration示例转换为使用Java DSL和Kotlin。
在我的入站TCP服务器中,我需要将来自通道的byte[]
转换为String
用于我的回显服务。
这是我当前的流程定义:
@EnableIntegration
@IntegrationComponentScan
@Configuration
class ClientServerDemoConfig {
@MessagingGateway(defaultRequestChannel = "input", name = "gw")
interface SimpleGateway {
fun send(text: String): String
}
@Bean
fun outboundTCP() =
IntegrationFlows.from("input")
.handle(Tcp.outboundGateway(TcpNetClientConnectionFactory("127.0.0.1", 7777)))
.get()
@Bean
fun inboundTCP() =
IntegrationFlows.from(Tcp.inboundGateway(TcpNetServerConnectionFactory(7777)))
.transform<Array<Byte>, String> { p -> String(p.toByteArray()) }
.handle(EchoService(), "test")
.get()
在intellij中运行时,结果为:
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'byte[]' to required type 'java.lang.String': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:299) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
奇怪的是,在调试时,我可以看到正在执行的转换器,但是随后我迷失了细节,试图找出为什么它不起作用。
有人可以就我没有做或做错的事情提供一些意见吗?
谢谢!
dependencies {
compile('org.springframework.boot:spring-boot-starter-integration')
compile('org.springframework.integration:spring-integration-ip')
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
compile("org.jetbrains.kotlin:kotlin-reflect")
runtime('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Stacktrace:
2018-07-10 16:53:59.726 ERROR 20187 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:781) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at alwyn.tcpclientserver.ClientServerDemoApplicationKt.main(ClientServerDemoApplication.kt:29) [main/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.3.RELEASE.jar:2.0.3.RELEASE]
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'byte[]' to required type 'java.lang.String'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'byte[]' to required type 'java.lang.String': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:77) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:44) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.integration.gateway.GatewayProxyFactoryBean.convert(GatewayProxyFactoryBean.java:763) ~[spring-integration-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.integration.gateway.GatewayProxyFactoryBean.invokeGatewayMethod(GatewayProxyFactoryBean.java:532) ~[spring-integration-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.integration.gateway.GatewayProxyFactoryBean.doInvoke(GatewayProxyFactoryBean.java:469) ~[spring-integration-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.integration.gateway.GatewayProxyFactoryBean.invoke(GatewayProxyFactoryBean.java:460) ~[spring-integration-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at com.sun.proxy.$Proxy60.send(Unknown Source) ~[na:na]
at alwyn.tcpclientserver.ClientServerDemoApplication.run(ClientServerDemoApplication.kt:19) ~[main/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:797) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
... 10 common frames omitted
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'byte[]' to required type 'java.lang.String': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:299) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:99) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:73) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
... 20 common frames omitted
答案 0 :(得分:0)
TCP / IP实际上是一个byte[]
协议,因此完全自然可以将这种类型发送到出站并从入站接收。
对于您的fun send(text: String): String
网关合同,您确实需要在通过byte[] -> String
收到回复后添加Tcp.outboundGateway()
转换/转换。因此,要为您解决这种情况,您需要在客户端执行以下操作:
@Bean
fun outboundTCP() =
IntegrationFlows.from("input")
.handle(Tcp.outboundGateway(TcpNetClientConnectionFactory("127.0.0.1", 7777)))
.transform(Transformers.objectToString())
.get()