apache camel使用simple删除json中的尾随空格

qyzbxkaa  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(238)

我如何使用apache camel修剪JSON中的尾随空格。我有下面的名称字段中的尾随空格输入。
我正在使用下面的行来修剪白色,但没有得到修剪。

<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"
        }
        ]
}
rsaldnfx

rsaldnfx1#

我如何使用apache camel修剪JSON中的尾随空格。我有下面的名称字段中的尾随空格输入。
您没有提到是否需要删除值中的尾随空格,所以我也添加了这一点,以备您需要。一个可能的解决方案是:

  • 将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":""}]}

相关问题