gpt4 book ai didi

java - @XmlAttribute 显示为 @XmlElement

转载 作者:行者123 更新时间:2023-12-02 08:12:10 26 4
gpt4 key购买 nike

在我的网络服务上,我将变量定义为@XmlAttribute:

@XmlAttribute
protected String domain;

但是当我通过 SoapUi 进行查询时,它显示为 XML 元素:

<ns:domain>domain</ns:domain>

我在代码中找不到任何错误..

如何解决这个问题?

最佳答案

您在字段上显示注释,但 JAXB 默认使用属性(getter/setter 方法)访问。您是否更改了 JAXB 的默认访问权限?尝试将注释放在 getter 方法上。

编辑:由于您似乎遇到了麻烦,这里有一个可执行示例:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.*;
import java.io.StringWriter;

public static void main(String[] args) throws Exception {
Foo foo = new Foo("my attribute value", "my element value");
Marshaller marshaller = JAXBContext.newInstance(Foo.class).createMarshaller();
StringWriter stringWriter = new StringWriter();
marshaller.marshal(foo, stringWriter);
System.out.println(stringWriter);
}

@XmlRootElement
static class Foo {
private String anAttribute;
private String anElement;

Foo() {}

public Foo(String anAttribute, String anElement) {
this.anAttribute = anAttribute;
this.anElement = anElement;
}

@XmlAttribute
public String getAnAttribute() { return anAttribute; }
@XmlElement
public String getAnElement() { return anElement; }
}

输出(格式化):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo anAttribute="my attribute value">
<anElement>my element value</anElement>
</foo>

关于java - @XmlAttribute 显示为 @XmlElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7192491/

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