2017-10-11 87 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 –