JAXB が XML スキーマのバインドされたクラスを生成する方法に問題があります (精度のために、これを変更することはできません)。xsd:date 型を Joda-time LocalDate オブジェクトにマップしたいので、ここ、ここ、ここを読んで、次のDateAdapterクラスを作成しました。
public class DateAdapter extends XmlAdapter<String,LocalDate> {
private static DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyyMMdd");
public LocalDate unmarshal(String v) throws Exception {
return fmt.parseLocalDate(v);
}
public String marshal(LocalDate v) throws Exception {
return v.toString("yyyyMMdd");
}
}
そして、グローバル バインディング ファイルに以下を追加しました。
<jaxb:globalBindings>
<jaxb:javaType name="org.joda.time.LocalDate" xmlType="xs:date"
parseMethod="my.classes.adapters.DateAdapter.unmarshal"
printMethod="my.classes.adapters.DateAdapter.marshal" />
</jaxb:globalBindings>
問題は、プロジェクトを Maven コンパイルしようとすると、次のエラーで失敗することです。
[ERROR] \My\Path\MyProject\target\generated-sources\xjc\my\classes\generated\Adapter1.java:[20,59] non-static method unmarshal(java.lang.String) cannot be referenced from a static context
[ERROR] \My\Path\MyProject\target\generated-sources\xjc\my\classes\generated\Adapter1.java:[24,59] non-static method marshal(org.joda.time.LocalDate) cannot be referenced from a static context
...そして、これは物事が奇妙になるところです。JAXB は、以下を含むクラス Adapter1 を生成します。
public class Adapter1
extends XmlAdapter<String, LocalDate>
{
public LocalDate unmarshal(String value) {
return (my.classes.adapters.DateAdapter.unmarshal(value));
}
public String marshal(LocalDate value) {
return (my.classes.adapters.DateAdapter.marshal(value));
}
}
....これがコンパイルエラーの原因です。
さて、私の質問は次のとおりです。
- アダプターが XmlAdapter をオーバーライドしているため、メソッドを静的にすることはできません....どうすればこれを回避できますか?
- Adapter1.class の生成を完全に回避できますか?? パッケージレベルの注釈 XmlJavaTypeAdapters を使用している可能性があります。(JAXB は独自の package-info.java を既に生成しています....)
私の状況を明確にしたことを願っています。
ありがとう