Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Converting Comma-Separated Strings to Arrays via Custom Java Interfaces in Weaver ECOLOGY9 ESB Center

Tech May 10 2

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

  1. 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;
        }
    }
  2. Request Data Configuration: The Java code expects an input parameter named rawIds (as defined by params.get("rawIds")). Ensure the request parameter configuration matches this exact name.
  3. Response Data Configuration: The Java code outputs the parameter identifier within the array structure named processedItems. Ensure the response configuration maps this correctly and check the "Detail Data" option for the processedItems array.
  4. Application Configuration: Integrate the newly created interface within the application management settings to trigger the subsequent API calls iteratively based on the returned array.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.