如何使用JAXBElement< String>在Web服务?

问题描述:

我正在使用WCF开发一个可互操作的Web服务,我正在从Java客户端使用它。当我创建代理类时,它生成了所有getter和setter方法以及 JAXBElement< String> 字段。我在JDK API中搜索了这个并找到了构造函数:

I am developing an interoperable web service using WCF which I am consuming from a Java client. When I created the proxy class it generated all the getter and setter methods as well as a JAXBElement<String> field. I searched for this in the JDK API and found the constructor:

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

我应该如何使用这个构造函数?请解释一下参数,如果互联网上有一个很好的教程描述它的使用,请告诉我。

How should I use this constructor? Please explain the parameters and let me know if there is a good tutorial on the Internet describing its use.

解决这个问题是的,您不需要创建单独的构造函数来创建 JAXBElement 。可以从 objectFactory.create ........()方法中检索受尊重的元素。假设您要在响应对象中创建并设置一些值,并且参数是 JAXBElement 类型,那么您需要这样做:

A solution this problem is, you do not need to create a seperate constructor for creating a JAXBElement. The respected element can be retrieved from objectFactory.create........() method. Suppose you want to create and set some value in response object, and argument is as of JAXBElement type, then you need to do this way:

someResponseObj.setMyValue(objectFactory.create.......()); 
/*method name that will be return a JAXBElement in setter()*/

注意:请检查 ObjectFactory 引用,因为生成的代码中可能有多个 ObjectFactory 类你需要引用与该包的类相关联的确切的一个。

Note: Please check the ObjectFactory reference because there can be multiple ObjectFactory classes in generated code so you need to refer the exact one which is associated to the class of that package.