Java 如何实例化 JAXBElement<String> 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/975099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:45:57  来源:igfitidea点击:

How do i instantiate a JAXBElement<String> object?

javajaxb

提问by miguel

I need to create one of these as the interface requires it...can someone please let me know how to create one, as there doesnt seem to be a c'tor defined?

我需要创建其中一个,因为界面需要它...有人可以让我知道如何创建一个,因为似乎没有定义 c'tor 吗?

采纳答案by Gaurav

When you imported the WSDL, you should have an ObjectFactoryclass which should have bunch of methods for creating various input parameters.

当您导入 WSDL 时,您应该有一个ObjectFactory类,该类应该有一堆方法来创建各种输入参数。

ObjectFactory factory = new ObjectFactory();
JAXBElement<String> createMessageDescription = factory.createMessageDescription("description");
message.setDescription(createMessageDescription);

回答by Matthew Flaschen

I don't know why you think there's no constructor. See the API.

我不知道你为什么认为没有构造函数。请参阅API

回答by JC.

Here is how I do it. You will need to get the namespace URL and the element name from your generated code.

这是我如何做到的。您需要从生成的代码中获取命名空间 URL 和元素名称。

new JAXBElement(new QName("http://www.novell.com/role/service","userDN"),
                new String("").getClass(),testDN);

回答by vik

ObjectFactory fact = new ObjectFactory();   
JAXBElement<String> str = fact.createCompositeTypeStringValue("vik");    
comp.setStringValue(str);
CompositeType retcomp = service.getDataUsingDataContract(comp);
System.out.println(retcomp.getStringValue().getValue());

回答by alditis

Other alternative:

其他选择:

JAXBElement<String> element = new JAXBElement<>(new QName("Your localPart"),
                                                String.class, "Your message");

Then:

然后:

System.out.println(element.getValue()); // Result: Your message