使用简单的 json 中的 apache camel 修剪尾随空格

apache camel trimming trailing spaces in json using simple

如何使用 apache camel 在 JSON 中 trim 尾随 space。我在名称字段中输入了尾随 spaces。

我正在使用下面的行 trim 白色 spaces 但没有任何东西得到 trimmed。

                   <setBody>
                     <simple trim="true">${body}</simple>
                   </setBody>

正文

{
    "primaryRelationOfficer": 0,
    "dedupReq": true,
    "blackListReq": false,
    "dedup": [
        {
            "cif": "12345",
            "categoryCode": "RETAIL",
            "defaultBranch": "BQA",
            "firstName": "SARTHU  ",
            "lastName": " DEVA",
            "shortName ": "UTTAM MONDAL",
            "dateofBirth": "1980-10-20T00:00:00",
            "custPAN": "",
            "sector ": "MANF"
        },
        {
            "cif": "2345",
            "categoryCode": "RETAIL",
            "defaultBranch": "SID",
            "firstName": "GADDAM     ",
            "lastName": "DEVENDRA         ",
            "shortName ": "GADDAM DEVENDRA",
            "dateofBirth": "1980-10-20T00:00:00",
            "custPAN": "",
            "sector ": "MANF"
        }
        ]
}       

How do i trim trailing space in JSON using apache camel. I have below input with trailing spaces at the name fields.

你没有提到是否需要从值中删除尾随空格,所以我也添加了它以备不时之需。一种可能的解决方案是:

  • 将您的 json 字符串转换为 POJO
  • 从值中删除尾随空格
  • 将 POJO 转换回 json 字符串

这是简单的测试用例:

class CamelRouteTest extends CamelTestSupport {

private static final String JSON = "{\n" +
        "  \"primaryRelationOfficer\": 0,\n" +
        "  \"dedupReq\": true,\n" +
        "  \"blackListReq\": false,\n" +
        "  \"dedup\": [\n" +
        "    {\n" +
        "      \"cif\": \"12345\",\n" +
        "      \"categoryCode\": \"RETAIL\",\n" +
        "      \"defaultBranch\": \"BQA\",\n" +
        "      \"firstName\": \"SARTHU  \",\n" +
        "      \"lastName\": \" DEVA\",\n" +
        "      \"shortName \": \"UTTAM MONDAL\",\n" +
        "      \"dateofBirth\": \"1980-10-20T00:00:00\",\n" +
        "      \"custPAN\": \"\",\n" +
        "      \"sector \": \"MANF\"\n" +
        "    },\n" +
        "    {\n" +
        "      \"cif\": \"2345\",\n" +
        "      \"categoryCode\": \"RETAIL\",\n" +
        "      \"defaultBranch\": \"SID\",\n" +
        "      \"firstName\": \"GADDAM     \",\n" +
        "      \"lastName\": \"DEVENDRA         \",\n" +
        "      \"shortName \": \"GADDAM DEVENDRA\",\n" +
        "      \"dateofBirth\": \"1980-10-20T00:00:00\",\n" +
        "      \"custPAN\": \"\",\n" +
        "      \"sector \": \"MANF\"\n" +
        "    }\n" +
        "  ]\n" +
        "}";

@Produce("direct:start")
protected ProducerTemplate start;

@Test
void start() {
    start.sendBody(JSON);
}

@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
                    .unmarshal(new GsonDataFormat(GSON, Profile .class))
                    .marshal(new GsonDataFormat())
                    .log("Got ${body}");
        }
    };
}

//If trimming whitespaces in values is not needed - remove everything below
private static final Gson GSON = new GsonBuilder()
        .registerTypeAdapter(String.class, new StringTrimJsonDeserializer())
        .create();

private static class StringTrimJsonDeserializer implements JsonDeserializer<String> {
    @Override
    public String deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        final String value = jsonElement.getAsString();
        return value == null ? null : value.trim();
    }
}

}

简介

public class Profile {
  private int primaryRelationOfficer;
  private boolean dedupReq;
  private boolean blackListReq;
  private List<Dedup> dedup;
  //getters setters
}

去重

public class Dedup {
  private String cif;
  private String categoryCode;
  private String defaultBranch;
  private String firstName;
  private String lastName;
  private String shortName;
  private String dateofBirth;
  private String custPAN;
  private String sector;
  //getters setters
}

POJO转json后的输出:

Got {"primaryRelationOfficer":0,"dedupReq":true,"blackListReq":false,"dedup":[{"cif":"12345","categoryCode":"RETAIL","defaultBranch":"BQA","firstName":"SARTHU","lastName":"DEVA","dateofBirth":"1980-10-20T00:00:00","custPAN":""},{"cif":"2345","categoryCode":"RETAIL","defaultBranch":"SID","firstName":"GADDAM","lastName":"DEVENDRA","dateofBirth":"1980-10-20T00:00:00","custPAN":""}]}