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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1393920/
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-12 11:37:19  来源:igfitidea点击:

How to use JAXBElement<String> in Web Service?

javaxmlwcfweb-services

提问by Chintan Shah

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:

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

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.

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

回答by Andrey Adamovich

The first parameter nameis a qualified name (local name + name space) of the XML element you are mirroring.

第一个参数名称是您要镜像的 XML 元素的限定名称(本地名称 + 名称空间)。

declaredTypeis class object of the class you are binding your XML element to.

声明类型是您将 XML 元素绑定到的类的类对象。

scopeis usage scope of the XML Element, but you can set it to null.

scope是 XML 元素的使用范围,但您可以将其设置为 null。

valueis an instance of declaredTypeclass i.e. the actual Java object bound to actual XML element instance.

valueclarifiedType类的实例,即绑定到实际XML 元素实例的实际Java 对象。

回答by question

So where is the usage.. and example. I want it..

那么用法在哪里......和例子。我要它..

I found it. So you maybe do that.. It will work.

我找到了。所以你可能会这样做..它会起作用。

Text t = new Text();
t.setValue("I need limo transportation");
JAXBElement<Text> text = new JAXBElement(SERVICE, Text.class, t);

回答by Tesnep

I stumbled upon this question while I was looking for the same answer. I posted an answer but found a few problems. Here is a way to do it:

我在寻找相同答案时偶然发现了这个问题。我发布了一个答案,但发现了一些问题。这是一种方法:

new javax.xml.bind.JAXBElement(
        new javax.xml.namespace.QName("http://locationOfURI", "nameOfElement"),
        javax.xml.bind.JAXBElement.class, 
        null, what your object's value is );

The last means the type parameter of JAXBElement.

最后一个表示 的类型参数JAXBElement

Hope this works.

希望这有效。

回答by Luis Rosa

Just in case someone ends up here looking for a solution: Instead of using JAXBElement one can use only the Type by setting the generateElementProperty to false in a bindings file.

以防万一有人最终在这里寻找解决方案:除了使用 JAXBElement,还可以通过在绑定文件中将 generateElementProperty 设置为 false 来仅使用 Type。

In my case Im using maven to generate the stub files from wsdl.

在我的情况下,我使用 maven 从 wsdl 生成存根文件。

partial pom file and bindings file (in this config is called javabindings.xml)

部分 pom 文件和绑定文件(在此配置中称为 javabindings.xml)

    <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-codegen-plugin</artifactId>
        <version>2.2.9</version>
        <executions>
        <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>
            <sourceRoot>${project.build.directory}/wsdl2java/generated-sources/src/main/java</sourceRoot>
            <wsdlOptions>
                <wsdlOption>
                    <wsdl>${basedir}/src/main/resources/yourWsdlFileOrURL.wsdl</wsdl>

                    <extraargs>
                        <extraarg>-verbose</extraarg>
                        <extraarg>-b</extraarg>
                        <extraarg>${basedir}/src/main/resources/javabindings.xml</extraarg>
                    </extraargs>                        
                </wsdlOption>
            </wsdlOptions>

            </configuration>
            <goals>
                <goal>wsdl2java</goal>
            </goals>
        </execution>
        </executions>
    </plugin>
<jaxb:bindings version="2.0" 
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<jaxb:bindings schemaLocation="http://localhost:15002/MiddlewareDataServiceConversion?xsd=xsd0"> 
    <jaxb:bindings node="/xs:schema"> 
        <jaxb:globalBindings generateElementProperty="false"/> 
    </jaxb:bindings> 
</jaxb:bindings> 

回答by Mukul Kumar

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 JAXBElementtype, then you need to do this way:

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

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

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

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

回答by Mircea D.

Try to use minOccurs="1" instead of minOccurs="0" in your wsdl. For some (good) reason, the code generator needs this info in order to convert parameters to basic java type parameters.

尝试在 wsdl 中使用 minOccurs="1" 而不是 minOccurs="0"。出于某些(好的)原因,代码生成器需要此信息才能将参数转换为基本的 Java 类型参数。

回答by digital_age

I'm just doing the exact same thing, using a Java Client to comsume WCF web service. I'm using the jaxws maven plugin to generate the client side code.

我只是在做完全相同的事情,使用 Java 客户端来使用 WCF Web 服务。我正在使用 jaxws maven 插件来生成客户端代码。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <configuration>
        <packageName>com.package1</packageName>
        <sourceDestDir>src/main/java</sourceDestDir>
        <wsdlDirectory>src/main/resources</wsdlDirectory>
        <wsdlFiles>
            <wsdlFile>wcf_webservice.wsdl</wsdlFile>
        </wsdlFiles>
    </configuration>
</plugin>

You should have an ObjectFactory class in your com.package1which you can use to create your JAXBElements for you eg:

您应该有一个 ObjectFactory 类com.package1,您可以使用它来为您创建 JAXBElements,例如:

ObjectFactory factory = new ObjectFactory();
MyObject myObject = new MyObject();
JAXBElement<MyObject> elem = factory.createMyObject(myObject);

Just as a side note, you'll need to flatten the wcf wsdl before jaxws can use it.

顺便提一下,在 jaxws 可以使用它之前,您需要将 wcf wsdl 展平。

回答by James K

I had problems trying the above methods using JAXBElement as per the documentation and I found this instead, which worked perfectly for me.

根据文档,我在尝试使用 JAXBElement 的上述方法时遇到了问题,但我发现了这一点,这对我来说非常有效。

        import javax.xml.bind.*;

        JAXBContext jc = JAXBContext.newInstance(YOURCLASS.class);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(yourInstantiatedClass,  System.out);