[英]override @XmlElement annotation with FIELD accessor type
我有一個帶有@XmlAccessorType(XmlAccessType.FIELD)
注釋的類,每個private
和protected
字段都用@XmlElement(name='...')
注釋。
挑戰:我可能想在稍后階段重命名一個xml元素名稱。 這引出了我的問題。 如果我創建一個子類,有沒有辦法覆蓋/重新定義這些注釋?
雖然在java中,據我所知,重寫注釋@XmlElement(name='...')
來更改name
屬性是不可能的; 您可以在代碼中創建一個全局變量,並將其傳遞給您的類或@XmlElement(name='...')
之后的函數。
在下面的代碼中,我創建了一個單獨的類,但如果你想將它傳遞給另一個類,它包含所需的setter和getter方法
@XMLAccessorType(XMLAccessType.FIELD)
public class YourClass {
@XmlTransient
private String string = ""; //This can be replaced with whatever variable you are manipulating
//That could be an int or a file or anything really
@XmlElement(name = "your_name")
private void doSomething() {
String temp = getString(); //This variable is normally used to pass between different
//classes but may as well use it if you have one
//Your code which manipulates the String
setString(temp); //This variable is normally used to pass between different classes but
//may as well use it if you have one
}
@XmlElement(name = "your_other_name")
private void doSomethingElse() {
String temp = getString();
//Your code which manipulates the String
setString(temp);
}
public void getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
}
我會建議查看Java Docs for @XmlTransient
以及這兩個相關的SO問題。
如何在屬性的getter方法上使用@XMLElement覆蓋在類級別指定的JAXB @XMLAccessorType(XMLAccessType.FIELD)?
我首先嘗試使用@XmlAccessorType(XmlAccessType.FIELD)
並使用@XmlTransient
隱藏。 只有在使用@XmlTransient
標記超類和子類中的字段時, @XmlTransient
。 但我想,這不是你想要的。
作為第二種方法,我嘗試在超類中使用更嚴格的@XmlAccessorType(XmlAccessType.PROPERTY)
,在子類中使用@XmlAccessorType(XmlAccessType.NONE)
。 看到我的例子:
package com.so.example;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/myresource")
public class MyResource {
@GET
@Path("/car")
@Produces(MediaType.APPLICATION_XML)
public Car getCar() {
Car car = new Car();
car.setWheels(4);
return car;
}
@GET
@Path("/suv")
@Produces(MediaType.APPLICATION_XML)
public Suv getSuv() {
Suv suv = new Suv();
List<String> bigWheels = new ArrayList<>();
bigWheels.add("left front wheel");
bigWheels.add("right front wheel");
bigWheels.add("left rear wheel");
bigWheels.add("right rear wheel");
suv.setBigWheels(bigWheels);
return suv;
}
}
類車:
package com.so.example;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement
public class Car {
protected Integer wheels;
public Car() {
}
@XmlElement(name = "wheels", nillable = true)
public Integer getWheels() {
return wheels;
}
public void setWheels(Integer wheels) {
this.wheels = wheels;
}
}
Suv(兒童)班:
package com.so.example;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Suv extends Car {
@XmlTransient
private Integer wheels;
private List<String> bigWheels;
public Suv() {
}
@Override
@XmlTransient
public Integer getWheels() {
return wheels;
}
@Override
public void setWheels(Integer wheels) {
this.wheels = wheels;
}
@XmlElement
public List<String> getBigWheels() {
return bigWheels;
}
public void setBigWheels(List<String> bigWheels) {
this.bigWheels = bigWheels;
}
}
“隱藏”超類的元素輪的一種方法是將其標記為“nillable = true”而不使用基本類型。 在這種情況下,現場車輪將被編組為<wheels xsi:nil="true"/>
如果您可以不使用父類進行編組,並且您只使用子類,則可以使用此處描述的方法:
http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html
您也可以使用moxy並指定自定義綁定: http : //www.eclipse.org/eclipselink/documentation/2.4/moxy/runtime003.htm
我相信JaxB的某些實現允許XML配置覆蓋注釋。 在這種情況下,這實際上是可能的。 以下是Eclipslink的一篇文章,解釋了如何做到這一點http://www.eclipse.org/eclipselink/documentation/2.4/solutions/jpatoxml004.htm
在我看來,您可以為要覆蓋的JaxB文件構建XML配置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.