English 中文(简体)
模式:从参数到XML的输出
原标题:Patterns: Populate instance from Parameters and export it to XML

I m 建立一个简单的RESTFul Service;为了达到这一目的,我需要两项任务:

  • Get an instance of my resource (i.e Book) from request parameters, so I can get that instance to be persisted
  • Build an XML document from that instance to send the representation to the clients

如今,我马上在我的POJO级做两件事:

public class Book implements Serializable {

    private Long id;

    public Book(Form form) {
        //Initializing attributes
        id = Long.parseLong(form.getFirstValue(Book.CODE_ELEMENT));
    }

    public Element toXml(Document document) {
        // Getting an XML Representation of the Book
        Element bookElement = document.createElement(BOOK_ELEMENT);
    }

我铭记着一项办事处原则,即行为应当发生在数据的地方,但现在,我的公法协会依靠请求和XMLAPIC,并且没有感觉到权利(该类人有持续的说明)。

是否有解决这一问题的标准做法/主人?

EDIT: The libraries i m using are Restlets and Objectify.

最佳回答

我同意你的看法,即行为应当是数据所在。 但与此同时,正如你所说的那样,我并不感到可以想象的那样,对一个POJO的界面进行污染,使用特定方法进行序列化(视你想做的方式——JSON、XML等)可能会大大增长。

1) Build an XML document from that instance to send the representation to the clients

为了从序列化逻辑中分离出物体,我将通过。 战略模式:

interface BookSerializerStrategy {
    String serialize(Book book);
}

public class XmlBookSerializerStrategy implements BookSerializerStrategy {

    public String serialize(Book book) {
        // Do something to serialize your book.
    }

}

public class JsonBookSerializerStrategy implements BookSerializerStrategy {

    public String serialize(Book book) {
        // Do something to serialize your book.
    }

}

You You

public class Book implements Serializable {

    private Long id;
    private BookSerializerStrategy serializer

    public String serialize() {
        return serializer.serialize(this);
    }

    public void setSerializer(BookSerializerStrategy serializer) {
        this.serializer = serializer;
    }
}

采用这一方法,你将能够仅仅在一个地方孤立序列化逻辑,并将污染你的POJO。 此外,将POJO与Document Element等同起来,我赢得了一定需要。

2) 从请求参数中举出我的资源(即书)的例子,以便我能够坚持这一榜样。

我认为,找到一种处理帝国化的模式更为复杂。 我确实看不出比创建固定方法的工厂更好的办法,以便把这一逻辑从您的POJO中去除。

回答你的两个问题的另一个办法是日本宇宙航空研究开发机构使用两种不同的物体,一个是unmarshaller,负责航空化,另一个是Marshaller,用于序列化。 Java 1.6, JAXB与JDK发生违约。

最后,这些只是建议。 我确实对你的问题感兴趣,并对其他可能的解决办法感到奇怪。

问题回答

Are you using Spring, or any other framework, in your project? If you used Spring, it would take care of serialization for you, as well as assigning request params to method params (parsing as needed).





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...