2016-07-05 94 views
3

我有一個帶有@XmlAccessorType(XmlAccessType.FIELD)註釋的類,並且每個privateprotected字段用註釋。重寫帶有FIELD訪問器類型的@XmlElement註釋類型

挑戰:我可能想在稍後階段重命名其中一個xml元素名稱。這使我想到了這個問題。如果我創建一個子類,有沒有辦法來覆蓋/重新定義這些註釋?

+0

我的回答是你在追求什麼,或者你是在拼命尋找一種方法來做同樣的'功能@覆蓋'? – Dan

+0

是的,正在尋找@override方式。我想下一個最好的事情就是亞歷山大所提出的。如果它們不可覆蓋,那麼在xml中定義它們,並在必要時替換xml文件。 – bvdb

回答

1

雖然在java中,據我所知,覆蓋註釋更改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@XmlTransient和這些兩個其他相關的SO問題。

How to override JAXB @XMLAccessorType(XMLAccessType.FIELD) specified at a Class level with @XMLElement on a getter method for a property?

Jaxb - Overriding the XMLElement name attribute

1

我第一次嘗試與@XmlAccessorType(XmlAccessType.FIELD)@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