Apache Camel - GSON JsonSerializer 在路由上的使用
Apache Camel - GSON JsonSerializer use on routes
我有一个 Camel 端点,returns 属性与 JSON 相同,但顺序不正确。 return class 有一个 superclass returns 一些控制数据,这些数据必须存在于每个 return.
public class Respuesta implements Serializable {
@SerializedName("subject")
@Expose
private String subject;
@SerializedName("action")
@Expose
private String action;
@SerializedName("status")
@Expose
private Integer status;
@SerializedName("description")
@Expose
private String description;
...getter/setter
最后的 return class 继承了那个片段。
public class FacturadoresListarResponse extends Respuesta implements Serializable {
@SerializedName("lst")
@Expose
private List<Facturador> listaProveedores;
public FacturadoresListarResponse(List<Facturador> listaProveedores) {
super();
this.listaProveedores = listaProveedores;
}
public FacturadoresListarResponse() {
}
public void setRespuesta(Respuesta rsp) {
super.setAction(rsp.getAction());
super.setDescription(rsp.getDescription());
super.setStatus(rsp.getStatus());
super.setSubject(rsp.getSubject());
}
getter/setter...
}
因此,Gson 的 Marshaller 首先采用继承的 class 属性 (lst),然后是父级 class 属性(主题、状态等),给出这种在线结果。
{
"lst": [
{
"rut": "XXXX-X",
"rzsoc": "XXXXXXx",
"res": 1,
"ema": "a@a.cl"
}
],
"subject": "facturadores",
"action": "listar",
"status": 0,
"description": "OK"
}
我写了一个按顺序构建数据的 GSON 自定义 JsonSerializer,但我不能在 Camel DSL 语法中使用。我试过了,但没有结果:
.marshal().json(JsonLibrary.Gson,FacturadoresListarRspSerializer.class, true)
.convertBodyTo(String.class, "UTF-8")
Camel 是否支持在不迁移到 Jackson 的情况下使用这些序列化程序来实现正确的顺序?
注:序列化器的代码(FacturadoresListarRspSerializer.class)。
public class FacturadoresListarRspSerializer implements JsonSerializer<FacturadoresListarResponse> {
@Override
public JsonElement serialize(FacturadoresListarResponse src, Type typeOfSrc, JsonSerializationContext context) {
final JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("subject", src.getSubject());
jsonObject.addProperty("action", src.getAction());
jsonObject.addProperty("status", src.getStatus());
jsonObject.addProperty("description", src.getDescription());
final JsonArray jsarrFacturadores = new JsonArray();
for (final Facturador fact : src.getListaProveedores()) {
JsonObject jsobFacturadores = new JsonObject();
jsobFacturadores.addProperty("rut", fact.getRutCompleto());
jsobFacturadores.addProperty("rzsoc", fact.getRazonSocial());
jsobFacturadores.addProperty("res", fact.getResolucion());
jsobFacturadores.addProperty("ema", fact.getCorreoEnvio());
jsarrFacturadores.add(jsobFacturadores);
}
jsonObject.add("lst", jsarrFacturadores);
return jsonObject;
}
}
创建一个新的 GSON 实例:
Gson gson = new GsonBuilder().registerTypeAdapter(FacturadoresListarResponse.class,
new FacturadoresListarRspSerializer()).create();
通过指定先前创建的 Gson
实例来创建新的 GsonDataFormat
:
GsonDataFormat gsonDataFormat = new GsonDataFormat(gson, FacturadoresListarResponse.class);
在您的 RouteBuilder
的 marshal(DataFormat dataFormat)
方法中指定以前的数据格式:
.marshal(gsonDataFormat)
我有一个 Camel 端点,returns 属性与 JSON 相同,但顺序不正确。 return class 有一个 superclass returns 一些控制数据,这些数据必须存在于每个 return.
public class Respuesta implements Serializable {
@SerializedName("subject")
@Expose
private String subject;
@SerializedName("action")
@Expose
private String action;
@SerializedName("status")
@Expose
private Integer status;
@SerializedName("description")
@Expose
private String description;
...getter/setter
最后的 return class 继承了那个片段。
public class FacturadoresListarResponse extends Respuesta implements Serializable {
@SerializedName("lst")
@Expose
private List<Facturador> listaProveedores;
public FacturadoresListarResponse(List<Facturador> listaProveedores) {
super();
this.listaProveedores = listaProveedores;
}
public FacturadoresListarResponse() {
}
public void setRespuesta(Respuesta rsp) {
super.setAction(rsp.getAction());
super.setDescription(rsp.getDescription());
super.setStatus(rsp.getStatus());
super.setSubject(rsp.getSubject());
}
getter/setter...
}
因此,Gson 的 Marshaller 首先采用继承的 class 属性 (lst),然后是父级 class 属性(主题、状态等),给出这种在线结果。
{
"lst": [
{
"rut": "XXXX-X",
"rzsoc": "XXXXXXx",
"res": 1,
"ema": "a@a.cl"
}
],
"subject": "facturadores",
"action": "listar",
"status": 0,
"description": "OK"
}
我写了一个按顺序构建数据的 GSON 自定义 JsonSerializer,但我不能在 Camel DSL 语法中使用。我试过了,但没有结果:
.marshal().json(JsonLibrary.Gson,FacturadoresListarRspSerializer.class, true)
.convertBodyTo(String.class, "UTF-8")
Camel 是否支持在不迁移到 Jackson 的情况下使用这些序列化程序来实现正确的顺序?
注:序列化器的代码(FacturadoresListarRspSerializer.class)。
public class FacturadoresListarRspSerializer implements JsonSerializer<FacturadoresListarResponse> {
@Override
public JsonElement serialize(FacturadoresListarResponse src, Type typeOfSrc, JsonSerializationContext context) {
final JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("subject", src.getSubject());
jsonObject.addProperty("action", src.getAction());
jsonObject.addProperty("status", src.getStatus());
jsonObject.addProperty("description", src.getDescription());
final JsonArray jsarrFacturadores = new JsonArray();
for (final Facturador fact : src.getListaProveedores()) {
JsonObject jsobFacturadores = new JsonObject();
jsobFacturadores.addProperty("rut", fact.getRutCompleto());
jsobFacturadores.addProperty("rzsoc", fact.getRazonSocial());
jsobFacturadores.addProperty("res", fact.getResolucion());
jsobFacturadores.addProperty("ema", fact.getCorreoEnvio());
jsarrFacturadores.add(jsobFacturadores);
}
jsonObject.add("lst", jsarrFacturadores);
return jsonObject;
}
}
创建一个新的 GSON 实例:
Gson gson = new GsonBuilder().registerTypeAdapter(FacturadoresListarResponse.class,
new FacturadoresListarRspSerializer()).create();
通过指定先前创建的 Gson
实例来创建新的 GsonDataFormat
:
GsonDataFormat gsonDataFormat = new GsonDataFormat(gson, FacturadoresListarResponse.class);
在您的 RouteBuilder
的 marshal(DataFormat dataFormat)
方法中指定以前的数据格式:
.marshal(gsonDataFormat)