使用 XStream 反序列化 XML 响应并获得单独的“成功”结果。和“失败”形式?

发布于 2024-09-01 21:45:37 字数 1366 浏览 4 评论 0原文

我计划使用 XStream 与 Java 在对象和 XML 请求以及 XML 响应和对象之间进行转换,其中 XML 通过 HTTP/HTTPS 流动。在响应方面,我可以获得“成功”响应,这似乎会映射到一个 Java 类,或者“失败”响应,这似乎会映射到另一个 Java 类。

例如,对于“文件列表”请求,我可能会得到肯定的响应,例如,

<?xml version="1.0" encoding="UTF-8"?>
<response>
  <success>true</success>
  <files>
    <file>[...]</file>
    <file>[...]</file>
    <file>[...]</file>
  </files>
</response>

或者我可能会得到否定的响应,例如,

<?xml version="1.0" encoding="UTF-8"?>
<response>
  <success>false</success>
  <error>
    <errorCode>-502</errorCode>
    <systemMessage>[...]AuthenticationException</systemMessage>
    <userMessage>Not authenticated</userMessage>
  </error>
</response>

要处理这个问题,我应该在这两种情况下在一个类中包含字段,还是应该以某种方式使用 XStream 来“有条件地“创建两个潜在类别之一?

同一对象中两个响应案例的字段的情况看起来像这样:

Class Response {
  boolean success;
  ArrayList<File> files;
  ResponseError error;
  [...]
}

Class File {
  String name;
  long size;
  [...]
}

Class ResponseError {
  int errorCode;
  String systemMessage;
  String userMessage;
  [...]
}

我不知道“使用 XStream 并在成功或错误的情况下创建不同的对象”是什么样子。有可能以某种方式做到这一点吗?这是更好还是更坏的方法?

无论如何,任何关于如何使用 XStream 处理这种成功与失败响应案例的建议将不胜感激。提前致谢!

I am planning on using XStream with Java to convert between objects and XML requests and XML responses and objects, where the XML is flowing over HTTP/HTTPS. On the response side, I can get a "successful" response, which seems like it would map to one Java class, or a "failure" response, which seems like it would map to another Java class.

For example, for a "file list" request, I could get an affirmative response e.g.,

<?xml version="1.0" encoding="UTF-8"?>
<response>
  <success>true</success>
  <files>
    <file>[...]</file>
    <file>[...]</file>
    <file>[...]</file>
  </files>
</response>

or I could get a negative response e.g.,

<?xml version="1.0" encoding="UTF-8"?>
<response>
  <success>false</success>
  <error>
    <errorCode>-502</errorCode>
    <systemMessage>[...]AuthenticationException</systemMessage>
    <userMessage>Not authenticated</userMessage>
  </error>
</response>

To handle this, should I include fields in one class for both cases or should I somehow use XStream to "conditionally" create one of the two potential classes?

The case with fields from both response cases in the same object would look something like this:

Class Response {
  boolean success;
  ArrayList<File> files;
  ResponseError error;
  [...]
}

Class File {
  String name;
  long size;
  [...]
}

Class ResponseError {
  int errorCode;
  String systemMessage;
  String userMessage;
  [...]
}

I don't know what the "use XStream and create different objects in case of success or error" looks like. Is it possible to do that somehow? Is it better or worse way to go?

Anyway, any advice on how to handle using XStream to deal with this success vs. failure response case would be appreciated. Thanks in advance!

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

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

发布评论

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

评论(2

谎言 2024-09-08 21:45:37

几年前我只短暂使用过 XStream,但是如果您想要两个不同的类,为什么不为这两种情况使用两个不同的根标签(例如成功和失败)呢?

如果您也使用 XStream 生成 XML,则这些类可以具有公共基类或接口(如果需要使事情在服务器端工作)。

I've only used XStream briefly, and quite a few years ago, but if you want two different classes why not have two different root tags (e.g. success and failure) for the two cases?

If you're generating the XML with XStream as well, the classes could have a common base class or interface if that's necessary to make things work on the server side.

极致的悲 2024-09-08 21:45:37

在实际处理这个问题时,我发现第一种情况(在两种情况下都包括一个类中的字段)似乎工作得很好,所以也许这就是要走的路。

在这种情况下,Response 的成功和不成功形式都会设置 success 字段,如果不成功,则会设置 ResponseError也被返回。

Class Response {
  boolean success;
  ArrayList<File> files;
  ResponseError error;
  [...]
}

Class File {
  String name;
  long size;
  [...]
}

Class ResponseError {
  int errorCode;
  String systemMessage;
  String userMessage;
  [...]
}

In actually working with this I am finding that the first case (include fields in one class for both cases) seems to be working just fine, so maybe that's the way to go.

That being this case where both the successful and unsuccessful forms of Response set the success field and, in the case of an unsuccessful one, the ResponseError is also returned.

Class Response {
  boolean success;
  ArrayList<File> files;
  ResponseError error;
  [...]
}

Class File {
  String name;
  long size;
  [...]
}

Class ResponseError {
  int errorCode;
  String systemMessage;
  String userMessage;
  [...]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文