简体   繁体   English

将缺少的xml属性解组到可选的Java对象

[英]Unmarshal missing xml attribute to an optional java object

I'm trying to unmarshal xml file which have some optional attributes (the attribute can be included in an element, but sometimes it's missing). 我正在尝试解组具有某些可选属性的xml文件(该属性可以包含在元素中,但有时会丢失)。 How can I create an optional java objects from those attributes? 如何从这些属性创建可选的Java对象?

XML: XML:

...
<example a="s"
     b="2.0"
/>
<example a="z"
/>
...

Java: Java的:

@XmlRootElement(name = "example")
public class ExampleDTO {

  private String a;
  private Optional<Float> b;

  public String getA() {
    return a;
  }

  @XmlAttribute
  public void setA(String a) {
    this.a = a;
  }

  public Optional<Float> getB() {
    return b;
  }

  @XmlAttribute
  @XmlJavaTypeAdapter(FloatOptionalAdapter.class)
  public void setB(Optional<Float> b) {
    this.b= b;
  }

Adapter: 适配器:

public class FloatOptionalAdapter extends XmlAdapter<Float, Optional<Float>> {

  @Override
  public Optional<Float> unmarshal(Float v) throws Exception {
    return Optional.ofNullable(v);
  }

  @Override
  public Float marshal(Optional<Float> v) throws Exception {
    if (v == null || !v.isPresent()) {
      return null;
    } else {
      return v.get();
    }
  }
}

When I run this code I get 2 objects of ExampleDTO. 当我运行此代码时,我得到了ExampleDTO的2个对象。 In the first one, I get Optional object with "2.0" value. 在第一个中,我得到带有“ 2.0”值的Optional对象。 In the second one, the value of b is "null". 在第二个中,b的值为“ null”。 How can I make the value to be an empty Optional object? 如何使值成为空的Optional对象?

Firstly, don't ever leave Optional<T> fields in a null state as it simply defeats the whole purpose of using the Optional<T> type. 首先,永远不要将Optional<T>字段保留为状态,因为这完全违背了使用Optional<T>类型的全部目的。

Ensure, they have a non-null value before allowing users of the API to access it and perform operations upon it, eg private Optional<Float> b; 确保它们具有非null值,然后再允许API用户访问它并对其执行操作,例如private Optional<Float> b; would be much better being declared as private Optional<Float> b = Optional.empty() (which actually might solve your current issue). 最好声明为private Optional<Float> b = Optional.empty() (实际上可能会解决您当前的问题)。

Further, I am not a fan of using Optional's as method parameters ( see here ). 此外,我不喜欢将Optional用作方法参数( 请参见此处 )。

The fact that your setB consumes an Optional<T> means that anyone can assign any value to the b field, and that means one can even do setB(null) , oops... setB使用Optional<T>的事实意味着任何人都可以向b字段分配任何值,这意味着甚至可以执行setB(null) ,哎呀。

As mentioned ideally you should avoid using optional's as parameters but if you want to keep it as is then I'd suggest performing some validation to prevent null being passed to the setB(...) method as again it defeats the purpose of using Optional. 理想情况下,您应该避免使用optional的参数,但是如果您希望保留它的原样,那么我建议您执行一些验证操作以防止将null传递给setB(...)方法,因为这样做再次setB(...)了使用Optional的目的。 。

Finally, by having the default value of b be Optional.empty() as well as preventing null being passed to the setB(...) method, it means b can never be null which should always be the case anyway in order to leverage the Optional<T> type. 最后,通过将b的默认值设置为Optional.empty()并防止将null传递给setB(...)方法,这意味着b永远不能为null ,无论如何,为了利用杠杆,总是应该如此Optional<T>类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM