开发者

Webservice to output multiple formats

First, let me start with some background: I have a web service which accepts XML as input and outputs in XML. The output has 2 different possible schemas (with two more coming). Each request has a list of groups from which to poll for data. The database is polled, then XML of the correct format is generated from each group; the groups are compiled together and the XML is outputted.

So basically, there is a class for each group of data which handles polling the database and outputting either XML schema output.

The code itself is very bloated开发者_运维问答 and difficult to maintain because of the multiple output formats. So I am looking for a design pattern to use in order to make adding new output formats easier. I explored the option of using XSLT to transform between formats, but they differ significantly so that I believe this option isn't feasible.

tl;dr: What is an efficient design pattern for a restful XML webservice to generate XML in different formats based on data from a single database?

Please let me if I can provide more information!


So basically, there is a class for each group of data which handles polling the database and outputting either XML schema output. --> Can this class be broken down to just do polling of data and return the output as a java bean or something? If yes, I would change this class to just handle data retrieval. Create a controller to decide the output format. Use the command pattern around this controller, so that you can add more output formats easily in future. Create a different component for the XML output part. Use the Factory pattern and pass the identifier for the output format to this. If the output format is very complex, think of using JAXB. Otherwise reuse whatever code is present in the classes already present.

Let me know if this helps.


If your formats contain the same data, you should use XSLT to transform between them; however, if the output data set is so different that you cannot use XSLT, then your resultant data is different, and you need to be using a different endpoint to get the data, which implies a different codepath.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜