Restlet Complex Object to XML serializaton [英] Restlet Complex Object to XML serializaton

查看:83
本文介绍了Restlet Complex Object to XML serializaton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有restlet web服务,它将响应返回为xml。我用杰克逊作为活页夹。
以下是我正在返回的课程。

I have restlet web service which returns response as xml. I'm using Jackson as binder. below is class I'm returning.

 import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;

    public class ApiResponse<T> implements Serializable {

        /**
         * 
         */
        private static final long serialVersionUID = -2736991050157565598L;

        private int responseCode;
        private String reponseMessage;
        private List<T> body = new ArrayList<T>();

        public int getResponseCode() {
            return responseCode;
        }

        public void setResponseCode(int responseCode) {
            this.responseCode = responseCode;
        }

        public String getReponseMessage() {
            return reponseMessage;
        }

        public void setReponseMessage(String reponseMessage) {
            this.reponseMessage = reponseMessage;
        }

        public List<T> getBody() {
            return body;
        }

        public void setBody(List<T> body) {
            this.body = body;
        }

    }

以下是服务的回复。除了将嵌套对象的属性名称与父对象相同之外,一切都很好。它显示了嵌套标签名称的正文,但我希望它是T模板。有什么想法吗?

And below is response of the service. Everything is almost good except that it puts as property names for nested objects the same as parents. It shows body for nested tag names but I expect it to be T template. Any ideas?

<ApiResponse>
    <responseCode>1</responseCode>
    <reponseMessage />
     <body>
        <body>
          <reportId>1</reportId>
          <reportName>name1</reportName>
        </body>
        <body>
          <reportId>2</reportId>
          <reportName>name2</reportName>
        </body>
     </body>
</ApiResponse>


推荐答案

这是杰克逊的默认序列化。但是,您可以利用自定义序列化程序来改进这一点此功能允许您将杰克逊生成的内容放在特定课程中。您可以使用自己的策略覆盖默认策略,并以非常精细的方式配置将要创建的内容。

This is the default serialization with Jackson. However you can leverage custom serializer to improve this. This feature allows you to have the hand on the generated content within Jackson for a specific class. You can override the default strategy with your own and configure in a very fine manner what will be created.

下面为类生成内容的此类实体的示例 SomeBean

Below a sample of such entity that generates content for the class SomeBean:

public class SomeBeanSerializer extends JsonSerializer<SomeBean> {
    @Override
    public void serialize(SomeBean bean, JsonGenerator jgen,
            SerializerProvider provider) throws IOException,
                  JsonProcessingException {
        jgen.writeStartObject();

        // Fields
        jgen.writeNumberField("id", bean.getId());
        (...)

        // Link
        String href = (...)
        HypermediaLink linkToSelf = new HypermediaLink();
        linkToSelf.setHref(href + bean.getId());
        linkToSelf.setRel("self");
        jgen.writeObjectField("hypermediaLink", linkToSelf);

        jgen.writeEndObject();
    }
}

以下是在Restlet中配置它的方法: / p>

Here is the way to configure this within Restlet:

JacksonConverter jacksonConverter = getRegisteredJacksonConverter();

if (jacksonConverter != null) {
    ObjectMapper objectMapper = jacksonConverter.getObjectMapper();
    SimpleModule module = new SimpleModule("MyModule", new Version(1, 0, 0, null));
    module.addSerializer(SomeBean.class, new SomeBeanSerializer());
    objectMapper.registerModule(module);
}

此链接可以帮助您了解如何配置Restlet的Jackson转换器: https://templth.wordpress.com/2015/02/ 23 /优化-的Restlet服务器的应用程序/ 。它提供了方法的内容 getRegisteredJacksonConverter

This link could help you to see how to configure the Jackson converter of Restlet: https://templth.wordpress.com/2015/02/23/optimizing-restlet-server-applications/. It provides the content of the method getRegisteredJacksonConverter.

已编辑:版本2.3 Restlet,这个级别的东西发生了变化。对象映射器现在由 JacksonRepresentation 而不是 JacksonConverter 本身带来。现在,对于每种此类表示,实例化对象映射器。这意味着您需要对这两个元素进行子类化以配置自定义序列化程序。

Edited: with version 2.3 of Restlet, something changes at this level. The object mapper is now brought by the JacksonRepresentation instead of the JacksonConverter itself. The object mapper is now instantiated for each representation of this kind. This means that you need to sub class these two elements to configure the custom serializer.

这是类的代码 CustomJacksonRepresentation

public class CustomJacksonRepresentation<T>
                extends JacksonRepresentation<T> {
    @Override
    public ObjectMapper getObjectMapper() {
        if (this.objectMapper == null) {
            this.objectMapper = createObjectMapper();
            SimpleModule module = new SimpleModule("MyModule",
                                    new Version(1, 0, 0, null));
            module.addSerializer(SomeBean.class,
                               new SomeBeanSerializer());
            objectMapper.registerModule(module);
        }
        return this.objectMapper;
    }
}

这是类的代码 CustomJacksonConverter

public class CustomJacksonConverter
                extends JacksonConverter {
    protected <T> JacksonRepresentation<T> create(
                        MediaType mediaType, T source) {
        return new CustomJacksonRepresentation<T>(
                                 mediaType, source);
    }

    protected <T> JacksonRepresentation<T> create(
              Representation source, Class<T> objectClass) {
        return new CustomJacksonRepresentation<T>(
                                 source, objectClass);
    }
}

这个实现了,你需要更换现有的jackson转换器这是由Restlet自动注册的。以下是执行该操作的代码:

This implemented, you need to replace the existing jackson converter that is automatically registered by Restlet. Here is the code to do that:

// Looking for the registered jackson converter
JacksonConverter jacksonConverter = null;
List<ConverterHelper> converters
         = Engine.getInstance().getRegisteredConverters();
for (ConverterHelper converterHelper : converters) {
    if (converterHelper instanceof JacksonConverter) {
        jacksonConverter = (JacksonConverter) converterHelper;
        break;
    }
}

// converters
Engine.getInstance().getRegisteredConverters().remove(
                                       jacksonConverter);
CustomJacksonConverter customJacksonConverter
                          = new CustomJacksonConverter();
Engine.getInstance().getRegisteredConverters().add(
                                 customJacksonConverter);

你可以注意到,管理转换器的方式将在Restlet的第3版中重构以制作东西配置更方便! ; - )

You can notice that the way to manage converters will be refactored in the version 3 of Restlet to make things more convenient to configure! ;-)

希望它可以帮到你,
Thierry

Hope it helps you, Thierry

这篇关于Restlet Complex Object to XML serializaton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆