gpt4 book ai didi

java - 如何命名xs :elements in the generated wsdl?

转载 作者:搜寻专家 更新时间:2023-11-01 03:42:06 24 4
gpt4 key购买 nike

我最近在我的 Eclipse 中启动了我的 WebServicesExplorer,我刚刚意识到 JAXB 生成的 xs:element 名称并不那么冗长。这是其中一个序列:

<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string" />
<xs:element minOccurs="0" name="arg1" type="xs:string" />
<xs:element name="arg2" type="xs:int" />
</xs:sequence>

从这个文件生成:

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class User {
private String firstName;
private String lastName;
private int age;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

我还不是 JAXB 专家,所以我想知道我是否可以将那些 arg* 命名元素更改为合理的东西,比如在 POJO 类中?

最佳答案

您没有提到您使用的是什么 Web 服务框架,但行为听起来类似于 JAX-WS 规范,例如The parts in my generated wsdl have names of the form "arg0", "arg1", ... Why don't the parts (and Java generated from them) use the nice parameter names I typed into the interface definition?

Official answer: The JAX-WS spec (specifically section 3.6.1) mandates that it be generated this way. To customize the name, you have to use an @WebParam(name = "blah") annotation to specify better names. (You can use @WebResult for the return value, but you'll only see the results if you look at the XML.)

Reason: One of the mysteries of java is that abstract methods (and thus interface methods) do NOT get their parameter names compiled into them even with debug info. Thus, when the service model is built from an interface, there is no way to determine the names that were using in the original code.

If the service is built from a concrete class (instead of an interface) AND the class was compiled with debug info, we can get the parameter names. The simple frontend does this. However, this could cause potential problems. For example, when you go from developement to production, you may turn off debug information (remove -g from javac flags) and suddenly the application may break since the generated wsdl (and thus expect soap messages) would change. Thus, the JAX-WS spec writers went the safe route and mandate that you have to use the @WebParam annotations to specify the more descriptive names.

您可以尝试添加 XmlElement注释每个字段并指定一个名称,但您确定这是由 JAXB 引起的吗?如果我使用您的 POJO 代码运行 schemagen,这就是我得到的结果(正确选择了名称):

<xs:complexType name="user">
<xs:sequence>
<xs:element name="age" type="xs:int"/>
<xs:element name="firstName" type="xs:string" minOccurs="0"/>
<xs:element name="lastName" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>

根据您的评论,如果您将 POJO“强制”为以下代码,是否还会收到关于重名的投诉?

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlAccessType;

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
public class User {
@XmlElement(name = "firstName")
private String firstName;

@XmlElement(name = "lastName")
private String lastName;

@XmlElement(name = "age")
private int age;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

关于java - 如何命名xs :elements in the generated wsdl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11818102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com