9

うまくいけば、JAXBの専門家にとって簡単なものです。

デフォルトの引数なしコンストラクターを定義しない不変クラスをマーシャリングしようとしています。実装を定義しましたXmlAdapterが、取り上げられていないようです。簡単な自己完結型の例をまとめましたが、まだ機能していません。誰かが私が間違っていることをアドバイスできますか?

不変クラス

@XmlJavaTypeAdapter(FooAdapter.class)
@XmlRootElement
public class Foo {
  private final String name;
  private final int age;

  public Foo(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public String getName() { return name; }
  public int getAge() { return age; }
}

アダプターと値のタイプ

public class FooAdapter extends XmlAdapter<AdaptedFoo, Foo> {
  public Foo unmarshal(AdaptedFoo af) throws Exception {
    return new Foo(af.getName(), af.getAge());
  }

  public AdaptedFoo marshal(Foo foo) throws Exception {
    return new AdaptedFoo(foo);
  }
}

class AdaptedFoo {
  private String name;
  private int age;

  public AdaptedFoo() {}

  public AdaptedFoo(Foo foo) {
    this.name = foo.getName();
    this.age = foo.getAge();
  }

  @XmlAttribute
  public String getName() { return name; }
  public void setName(String name) { this.name = name; }

  @XmlAttribute
  public int getAge() { return age; }
  public void setAge(int age) { this.age = age; }
}

マーシャラー

public class Marshal {
  public static void main(String[] args) {
    Foo foo = new Foo("Adam", 34);

    try {
      JAXBContext jaxbContext = JAXBContext.newInstance(Foo.class);
      Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

      // output pretty printed
      jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

      jaxbMarshaller.marshal(foo, System.out);              
    } catch (JAXBException e) {
      e.printStackTrace();
    }   
  }
}

スタックトレース

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Foo does not have a no-arg default constructor.
        this problem is related to the following location:
                at Foo

        at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:451)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:283)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:126)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1142)
        at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:130)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:248)
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:235)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:445)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:637)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
        at Marshal2.main(Marshal2.java:11)

JDK1.7.0_05を使用していることに注意してください。

4

2 に答える 2

8

以下が役立ちます。

ルート オブジェクトとしての FOO

が型レベルで指定されている場合@XmlJavaTypeAdapter、そのクラスを参照するフィールド/プロパティにのみ適用され、そのクラスのインスタンスが XML ツリーのルート オブジェクトである場合には適用されません。これは、自分自身に変換Fooし、 onと notAdaptedFooを作成する必要があることを意味します。JAXBContextAdaptedFooFoo

元帥

package forum11966714;

import javax.xml.bind.*;

public class Marshal {
    public static void main(String[] args) {
      Foo foo = new Foo("Adam", 34);

      try {
        JAXBContext jaxbContext = JAXBContext.newInstance(AdaptedFoo.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxbMarshaller.marshal(new AdaptedFoo(foo), System.out);              
      } catch (JAXBException e) {
        e.printStackTrace();
      }   
    }
  }

適応したFoo

クラスに@XmlRootElement注釈を追加する必要があります。クラスAdaptedFooから同じ注釈を削除できます。Foo

package forum11966714;

import javax.xml.bind.annotation.*;

@XmlRootElement
class AdaptedFoo {
    private String name;
    private int age;

    public AdaptedFoo() {
    }

    public AdaptedFoo(Foo foo) {
        this.name = foo.getName();
        this.age = foo.getAge();
    }

    @XmlAttribute
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlAttribute
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

ネストされたオブジェクトとしての FOO

ルート オブジェクトでない場合Fooは、マップしたとおりにすべてが機能します。これがどのように機能するかを示すために、モデルを拡張しました。

バー

package forum11966714;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Bar {

    private Foo foo;

    public Foo getFoo() {
        return foo;
    }

    public void setFoo(Foo foo) {
        this.foo = foo;
    }

}

デモ

FooJAXB 参照実装では、 をブートストラップするときにクラスを指定できないことに注意してくださいJAXBContext

package forum11966714;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {
    public static void main(String[] args) {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Bar.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            File xml = new File("src/forum11966714/input.xml");
            Bar bar = (Bar) jaxbUnmarshaller.unmarshal(xml);

            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(bar, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

input.xml/出力

<?xml version="1.0" encoding="UTF-8"?>
<bar>
    <foo name="Jane Doe" age="35"/>
</bar>
于 2012-08-15T10:08:18.597 に答える
0

これが当てはまらないことはわかっていますが、フィールドに @XmlJavaTypeAdapter を配置したときにこのようなエラーが発生した場合は、名前空間を指定したことを確認してください。あなたはそれを必要とするかもしれません。

私の場合、これはうまくいきませんでした:

@XmlElement(name = "Expiration")
@XmlJavaTypeAdapter(DateAdapter.class)
private Date expiration;

名前空間が指定されるまで:

@XmlElement(name = "Expiration", namespace="http://site/your.namespace")
@XmlJavaTypeAdapter(DateAdapter.class)
private Date expiration;
于 2019-12-11T15:24:31.893 に答える