2017-10-11 140 views
0

我在将我的Spring集成入站网关中的CSV格式的查询参数转换为java.util.Set时遇到问题。将查询参数转换为设置

我的出站网关添加了像?ids=[ID_1, ID_2]这样的参数。

我的入站网关正在将整个参数作为单个元素读入一个像["ID_1, ID_2"]这样的Set。

我已经创建了自己的静态帮助器方法来将字符串转换为集合,但是想知道是否有办法在Spring集成中隐式执行转换?

感谢您的帮助!

我的代码如下。

出站网关:

​​

站网关:

<int:service-activator 
input-channel="myChannel" 
expression="@'myService'.getStuff(payload.ids, headers.foo)"/> 

<int-http:inbound-gateway 
id="myGateway" 
request-channel="myChannel" 
path="MY_URL" 
message-converters="myConverter" 
header-mapper="myMapper" 
supported-methods="GET"> 
    <int-http:header name="foo" expression="#requestParams.foo"/> 
</int-http:inbound-gateway> 

编辑

这看起来将解决我的问题:https://docs.spring.io/spring-integration/docs/4.3.12.RELEASE/reference/html/messaging-endpoints-chapter.html#payload-type-conversion

回答

0

考虑使用org.springframework.util.StringUtils.commaDelimitedListToSet()

/** 
* Convert a comma delimited list (e.g., a row from a CSV file) into a set. 
* <p>Note that this will suppress duplicates, and as of 4.2, the elements in 
* the returned set will preserve the original order in a {@link LinkedHashSet}. 
* @param str the input {@code String} 
* @return a set of {@code String} entries in the list 
* @see #removeDuplicateStrings(String[]) 
*/ 
public static Set<String> commaDelimitedListToSet(@Nullable String str) { 
+0

谢谢。这当然比我的自定义静态方法更好。尽管如此,我仍然觉得在Spring Integration中必须有一种方法可以自动完成。 – Craig

+0

不,这不是Spring集成责任。没错,有一个'Converter'抽象,我们绝对可以开发一些'StringToSetConverter',但是在其他地方可能会有危险。无论如何:https://docs.spring.io/spring-integration/docs/4.3.12.RELEASE/reference/html/messaging-endpoints-chapter.html#payload-type-conversion –