用于包装模型和/或转换模型 (POJO) 的良好 JAXB 设计模式?

发布于 2024-12-12 02:19:18 字数 1156 浏览 0 评论 0原文

我有以下模型,

class MyClass {
    id
    someRandomString
}

我想将此 POJO 返回给客户端...

<Root>
    <random>if + randomstring</random>
</Root>

基本上我正在处理一个非常面向消息的服务,所以我需要将模型包装并转换为出站 XML 格式...同样继续输入端

我有这个XML...

<Root>
    <Username>
    <Password>
    <Action> <-- Some action or service to perform
    <SomeModel1>
    <SomeModel2>
</Root>

所以root、用户名和密码是常量元素,而SomeModel可以根据“服务”进行更改

基本上我正在寻找一种XML与模型不匹配的设计模式需要有一个转变……

或者如果我做了...

class MyClass {
    id
    someRandomString
    random

    @XMLTransient
    getId()


    @XMLTransient
    getSomeRandomString()


    getRandom() {
        return id + someRandomString
    }
}

所以JAXB只会映射getRandom...

我的另一个想法是创建一堆代表最终输出的类,并将值设置为这些...

所以

class MyResponse {
    randomString
    status
    someOtherFieldRequired in response
}    

然后我可以做...

myResponse.setRandomString(myClass.getId() + myClass.getSomeRandomString());

I have the following model

class MyClass {
    id
    someRandomString
}

I would like to return this POJO to the client as...

<Root>
    <random>if + randomstring</random>
</Root>

Basically I'm dealing with a very message oriented service so I need to wrap and transform the model to the outbound XML format... Same goes on the input side

I have this XML...

<Root>
    <Username>
    <Password>
    <Action> <-- Some action or service to perform
    <SomeModel1>
    <SomeModel2>
</Root>

So the root and username and password are constant elements while the SomeModel can change based on the "service"

Bassically i'm looking for a design-pattern where the XML doesn't match the model and there needs to be a transformation to and from...

Or what if I did...

class MyClass {
    id
    someRandomString
    random

    @XMLTransient
    getId()


    @XMLTransient
    getSomeRandomString()


    getRandom() {
        return id + someRandomString
    }
}

So JAXB will only map getRandom...

Another idea I have is create a bunch of classes that will represent the final output and just set the values to thoses...

So

class MyResponse {
    randomString
    status
    someOtherFieldRequired in response
}    

and then I can do...

myResponse.setRandomString(myClass.getId() + myClass.getSomeRandomString());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星星的軌跡 2024-12-19 02:19:18

您可以使用 XmlAdapterXmlJavaTypeAdapter,但我认为它可能需要您将 id 和 someRandomString 属性封装到一个单独的类中。如果可能的话,我不知道如何直接将元素值分散到两个单独的 bean 属性上。

You could use XmlAdapter in conjunction with XmlJavaTypeAdapter, but I think it might require you to encapsulate the id and someRandomString properties into a separate class. Spreading out an element value over two separate bean properties directly is not something I'd know how to do, if it is at all possible.

北座城市 2024-12-19 02:19:18

您可以使用 idsomeRandomString 字段来分隔元素,并使用样式表将元素合并为一个。 JAXB 提供了与 javax.xml.transform API 相匹配的 JAXBSource 类:

MyClass myClass = new MyClass();
myClass.setId(123);
myClass.setSomeRandomString("FOO");

TransformerFactory tf = TransformerFactory.newInstance();
StreamSource xslt = new StreamSource(new FileInputStream("my-xslt.xml"));
Transformer t = tf.newTransformer(xslt);

JAXBContext jc = JAXBContext.newInstance(MyClass.class);
JAXBSource source = new JAXBSource(jc, myClass);

StreamResult result = new StreamResult(System.out);

t.transform(source, result);

You could the id and someRandomString fields to separate elements and use a style sheet to combine the elements into one. JAXB offers the JAXBSource class that fits it with the javax.xml.transform APIs:

MyClass myClass = new MyClass();
myClass.setId(123);
myClass.setSomeRandomString("FOO");

TransformerFactory tf = TransformerFactory.newInstance();
StreamSource xslt = new StreamSource(new FileInputStream("my-xslt.xml"));
Transformer t = tf.newTransformer(xslt);

JAXBContext jc = JAXBContext.newInstance(MyClass.class);
JAXBSource source = new JAXBSource(jc, myClass);

StreamResult result = new StreamResult(System.out);

t.transform(source, result);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文