2016-07-05 22 views
3

私は@XmlAccessorType(XmlAccessType.FIELD)アノテーションを持つクラスを持ち、それぞれprivateprotectedフィールドにはというアノテーションが付けられています。@ XmlElementアノテーションとFIELDアクセサ型のオーバーライド

挑戦:私は、後の段階でxml要素名の1つの名前を変更したいかもしれません。これは私に質問につながります。サブクラスを作成すると、これらのアノテーションをオーバーライド/再定義する方法はありますか?

+0

私の答えはあなたの後だったのですか、あなたは必然的に 'function @ override'に相当する方法を探していましたか? – Dan

+0

はい、@overrideの方法を探していました。私は次の一番良いことは、アレクサンダーが提案したものだと思います。オーバーライドできない場合は、xmlで定義し、必要に応じてxmlファイルを置き換えます。 – bvdb

答えて

1

を:

また、あなたはMOXYを使用してカスタムバインディングを指定することもできます。この場合、実際には可能です。ここでは、これを行う方法を説明するEclipslinkの記事ですhttp://www.eclipse.org/eclipselink/documentation/2.4/solutions/jpatoxml004.htm

私の意見では、オーバーライドするJaxBファイルのXML設定を作成することができます。

1

私の知る限りでは、nameプロパティを変更するには、という注釈を無効にすることはできません。コード内にグローバル変数を作成し、の後にクラスまたは関数を渡すことができます。 I以下のコードで

は、単一のクラスを作成していますが、別のクラスにそれを通過したい場合は、それが必要な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; 
    } 

} 

私は@XmlTransient及びこれらのためにJava Docs見てreccomendう2つの他の関連する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

Iは、@XmlAccessorType(XmlAccessType.FIELD)と最初の試みと@XmlTransientで非表示にします。これは、スーパークラスと子クラスのフィールドに@XmlTransientというフィールドを付けた場合にのみ機能します。しかし、私はこれがあなたが望むものではないと思います。

2番目のアプローチとして、スーパークラスではより限定的な@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"とマークし、プリミティブ型は使用しないでください。

http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html:この場合、フィールドホイールを使用すると、マーシャリングのための親クラスを使用しないように、あなたが唯一の子クラスを使用しているため、それが可能なら、あなたはここで説明したアプローチを使用することができ<wheels xsi:nil="true"/>

に整列化されます私はJAXBのいくつかの実装は、注釈をオーバーライドするXML構成を可能にすることを信じてい http://www.eclipse.org/eclipselink/documentation/2.4/moxy/runtime003.htm

関連する問題