【问题标题】:Apache Camel - GSON JsonSerializer use on routesApache Camel - GSON JsonSerializer 在路由上使用
【发布时间】:2017-09-20 19:41:57
【问题描述】:

我有一个带有 Camel 的端点,它以 JSON 形式返回属性,但顺序不正确。返回类有一个超类,它返回一些控制数据,这些数据必须存在于每个返回中。

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

【问题讨论】:

    标签: java json apache-camel gson dsl


    【解决方案1】:

    创建一个新的 GSON 实例:

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

    通过指定之前创建的Gson 实例来创建一个新的GsonDataFormat

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

    在你的RouteBuildermarshal(DataFormat dataFormat)方法中指定之前的数据格式:

    .marshal(gsonDataFormat)
    

    【讨论】:

    • 谢谢!它以正确的顺序给出了预期的响应。
    猜你喜欢
    • 2016-09-29
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 2014-05-23
    • 1970-01-01
    相关资源
    最近更新 更多