Converting Comma-Separated Strings to Arrays via Custom Java Interfaces in Weaver ECOLOGY9 ESB Center
Business Requirement
When a REST API returns a response where specific parameters are comma-separated strings, it becomes necessary to parse these strings into an array format to trigger subsequent API calls for each individual element.
For instance, a typical REST API payload might look like this:
{
"body": {
"code": "0",
"msg": "",
"data": [
{
"apply_id": "651fb75f65114533cea0f7e1,651fb75f65114533cea0f7e1,652208c4dc9d343005a1b2ed,6522086e07a5f86b5e110296,652204786cca6359a11c7ada,65220403dc9d343005a19878"
}
],
"timestamp": "1720853015096",
"postToken": ""
},
"statusCode": "200"
}Requirement Analysis
The standard functionalities of the ESB Center do not natively support splitting strings into arrays for iterative processing. Achieving this requires a custom interface utilizing Java code to perform the string manipulation and output a structured list.
Implementation Strategy
Navigate to the ESB Center > Interface Management and create a new interface of type "Execute JAVA Code". This interface will handle the transformation of the comma-delimited string into an array of objects.
Solution Steps
- Create the Interface: In ESB Center > Interface Management, add a new interface. Set the interface type to "Execute JAVA Code". Leave the required resources blank to use local OA resources, and opt for the online editor for the service invocation class.
Enter the following Java code in the online editor:package com.weaver.esb.custom; import java.util.*; import java.util.stream.Collectors; public class StringToArrayProcessor { /** * @param: params (Map collections) * Parameter names must not contain special characters like +,.[]!"#$%&'()*:;<=>?@\^`{}|~/ * or Chinese characters, punctuation, U+007F, U+0000 to U+001F. */ public Map<String, Object> execute(Map<String, Object> params) { // Retrieve the input string parameter String rawIds = (String) params.get("rawIds"); // Handle potential null input if (rawIds == null || rawIds.trim().isEmpty()) { Map<String, Object> emptyResult = new HashMap<>(); emptyResult.put("processedItems", Collections.emptyList()); return emptyResult; } // Split the string by comma and transform into a List of Maps List<Map<String, String>> mappedItems = Arrays.stream(rawIds.split(",")) .map(id -> { Map<String, String> entry = new HashMap<>(); entry.put("identifier", id.trim()); return entry; }) .collect(Collectors.toList()); // Construct the response Map Map<String, Object> response = new HashMap<>(); response.put("processedItems", mappedItems); return response; } } - Request Data Configuration: The Java code expects an input parameter named
rawIds(as defined byparams.get("rawIds")). Ensure the request parameter configuration matches this exact name. - Response Data Configuration: The Java code outputs the parameter
identifierwithin the array structure namedprocessedItems. Ensure the response configuration maps this correctly and check the "Detail Data" option for theprocessedItemsarray. - Application Configuration: Integrate the newly created interface within the application management settings to trigger the subsequent API calls iteratively based on the returned array.