Java 从JAXB(WebService)生成XML时出错
我有以下代码返回Java 从JAXB(WebService)生成XML时出错,java,web-services,jaxb,marshalling,Java,Web Services,Jaxb,Marshalling,我有以下代码返回Foo @GET @Produces (MediaType.APPLICATION_XML) public Foo getXML (){ System.out.println ("getXML Request"); Foo f = new Foo(); d.setA("test"); d.setB("xyxyx"); return f; } 我的Foo课程是 @XmlRootElement public class Foo{ p
Foo
@GET
@Produces (MediaType.APPLICATION_XML)
public Foo getXML (){
System.out.println ("getXML Request");
Foo f = new Foo();
d.setA("test");
d.setB("xyxyx");
return f;
}
我的Foo
课程是
@XmlRootElement
public class Foo{
public void setA(String a) {
this.a = a;
}
public void setB(String b) {
this.b = b;
}
public String getB (){
return b;
}
public String getA (){
return a;
}
@XmlAttribute(name="atrribB")
private String b;
@XmlElement(name="elementA")
private String a;
}
在执行此操作时,我在Foo
上得到一个错误,即类有两个同名的属性“A”
,而B
也有相同的属性
当我删除这两个属性的
getters
方法时,一切都很好。我是否应该不创建getter setter并将字段保留为public 您需要对get方法进行注释
@XmlRootElement
public class Foo{
public void setA(String a) {
this.a = a;
}
public void setB(String b) {
this.b = b;
}
@XmlAttribute(name="atrribB")
public String getB (){
return b;
}
@XmlElement(name="elementA")
public String getA (){
return a;
}
private String b;
private String a;
}
或者指定@xmlacessortype(xmlacesstype.FIELD)
了解更多信息
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo{
public void setA(String a) {
this.a = a;
}
public void setB(String b) {
this.b = b;
}
public String getB (){
return b;
}
public String getA (){
return a;
}
@XmlAttribute(name="atrribB")
private String b;
@XmlElement(name="elementA")
private String a;
}