2017-09-20 221 views
0

我有一个Camel端点,它返回属性为JSON,但没有正确的顺序。 return类有一个超类,它返回一些控制数据,这些数据在每个返回中都必须存在。Apache Camel - 在路由上使用GSON JsonSerializer

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 

而最终的返回类继承那一块。

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需要继承的类属性(LST),然后父类属性(主体,状态等),使这种结果的丝。

{ 
    "lst": [ 
    { 
     "rut": "XXXX-X", 
     "rzsoc": "XXXXXXx", 
     "res": 1, 
     "ema": "[email protected]" 
    } 
    ], 
    "subject": "facturadores", 
    "action": "listar", 
    "status": 0, 
    "description": "OK" 
} 

我写了一个GSON自定义JsonSerializer,它按顺序构建数据,但我不能在Camel DSL语法中使用。我试过,但没有结果:

.marshal().json(JsonLibrary.Gson,FacturadoresListarRspSerializer.class, true) 
.convertBodyTo(String.class, "UTF-8") 

是否存在被骆驼支持,能够使用这些类型的串行器来实现正确的顺序不迁移到杰克逊?

注意:序列化程序的代码(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; 
    } 
} 

回答

1

创建一个新的GSON实例:

Gson gson = new GsonBuilder().registerTypeAdapter(FacturadoresListarResponse.class, 
     new FacturadoresListarRspSerializer()).create(); 

创建一个新的GsonDataFormat通过指定以前创建Gson例如:

GsonDataFormat gsonDataFormat = new GsonDataFormat(gson, FacturadoresListarResponse.class); 

指定您RouteBuildermarshal(DataFormat dataFormat)以前的数据格式方法:

.marshal(gsonDataFormat) 
+0

谢谢!它按正确的顺序给出了预期的响应。 –