2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Play Frameworkプロジェクト内でJAXBを使っているとなぜかうまく動かないときは GeneratedAccessor

Posted at

Java標準のXMLシリアライザであるJAXBをPlay Frameworkプロジェクト内で使っていると、こんなランタイムエラーが出てエンティティクラスを解析しそこねることがあります。

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
クラスに同じ名前"name"を持つ2つのプロパティがあります
	this problem is related to the following location:
		at public java.lang.String Hoge.getName()
		at Hoge
	this problem is related to the following location:
		at public java.lang.String Hoge.name
		at Hoge

もちろんHogeクラスにはnameフィールドを作っただけで、同名プロパティだと指摘されているgetName()メソッドなんか作った覚えはありません。
勝手にgetterが…? あ、まさかPlay Frameworkのしわざ?

そうなんです。Playは勝手に各クラスにgetter/setterアクセサをコンパイル後追加してくれるのです。

Play has been designed to generate getter/setter automatically, to ensure compatibility with libraries that expect them to be available at runtime (ORM, Databinder, JSON Binder, etc). If Play detects any user-written getter/setter in the Model, it will not generate getter/setter in order to avoid any conflict.
https://www.playframework.com/documentation/2.2.x/JavaEbean

これを抑止するにはどうすればいいか?
これはアンドキュメンテッドなのですが、

@PropertiesEnhancer.GeneratedAccessor

クラス宣言にこの「アクセサ追加加工済み」を意味するこのアノテーションを付けておくと無視してもらえます。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?