apache camel trimming trailing spaces in json using simple



如何使用apachecamel修剪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"
}
]
}       

如何使用apachecamel修剪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
}

Dedup

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

最新更新