JAXBElement<String>初始化 – Java Spring Boot 2

huangapple go评论62阅读模式
英文:

JAXBElement<String> initialization - Java Spring Boot 2

问题

我必须初始化一个类型为 JAXBElement &lt;String&gt; 的元素,我已经尝试如下:

JAXBElement&lt;String&gt; element = new JAXBElement&lt;&gt;(new QName(&quot;http://tempuri.org/&quot;, &quot;FieldName&quot;), String.class, &quot;FieldData&quot;);

但我不确定这是否是正确的方式。有人可以确认是否有另一种更简单的方法吗?

英文:

I have to initialize an element of type JAXBElement &lt;String&gt;, I have tried as follows:

JAXBElement&lt;String&gt; element = new JAXBElement&lt;&gt;(new QName(&quot;http://tempuri.org/&quot;, &quot;FieldName&quot;), String.class, &quot;FieldData&quot;);

But I am not sure if it is the correct way. Can someone confirm if there is another easier way?

答案1

得分: 1

你发布的是我所知道的初始化 JAXBElement 元素最简单的方法 - 也是正确的方法。

两个构造函数分别是:

JAXBElement(QName name, Class<T> declaredType, Class scope, T value)

以及(最简单的方法,你所使用的方法)

JAXBElement(QName name, Class<T> declaredType, T value)


另外,请记住,如果你所说的“简单”是指你不需要用 scope(第一个构造函数的第三个参数)来初始化对象,那么你的代码应该是正确的。

编辑:

我唯一有疑问的是 &quot;FieldName&quot;(提供给 QName 构造函数的第二个参数) - 我不知道它对你来说代表什么,但这应该是 QName 的本地部分。有关更多信息,请参见

public QName(String namespaceURI, String localPart)

英文:

What you posted is the simplest way of initializing a JAXBElement element that I know of - a correct one.

The two constructors are:

JAXBElement(QName name, Class&lt;T&gt; declaredType, Class scope, T value)

and (the simplest one, the one you used)

JAXBElement(QName name, Class&lt;T&gt; declaredType, T value)


Also, keep in mind that if by Simple you mean, you don't need to initialize the object with the scope (3rd argument of the first constructor), then your code should be fine.

Edit:

The only questionable thing I see is the &quot;FieldName&quot; (2nd argument provided to the QName constructor) - I'm not what it represents for you, but this should be the local part of the QName. For more info about this see

public QName(String namespaceURI, String localPart)

huangapple
  • 本文由 发表于 2020年8月11日 18:24:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63356211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定