简体   繁体   中英

JAXBElement<String> initialization - Java Spring Boot 2

I have to initialize an element of type JAXBElement <String> , I have tried as follows:

JAXBElement<String> element = new JAXBElement<>(new QName("http://tempuri.org/", "FieldName"), String.class, "FieldData");

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

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<T> declaredType, Class scope, T value)

and (the simplest one, the one you used)

JAXBElement(QName name, Class<T> 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 "FieldName" (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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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