json-pluginを使用してstruts2のプロジェクトを作成しています:ActionSupportを拡張し、@JsonPropertyアノテーション。@JsonPropertyでマークされたクラスフィールドに格納されている値を取得する方法
public abstract class BaseAction extends ActionSupport {
@JsonProperty
private String commonField1;
@JsonProperty
private String commonField2;
public String execute() {
executeAction();
//some things to get the values in the JsonProperties
}
public abstract void executeAction();
}
このフレームワーク内のすべてのアクションはBaseActionを拡張し、@ JsonPropertyとマークされた特定のフィールドを宣言します。
public class SpecificAction extends BaseAction {
@JsonProperty
private String specificField1;
@JsonProperty
private String specificField2;
public void executeAction() {
//things
}
}
私はすべての@JsonPropertyフィールドに格納されているすべての値に、BaseActionで、アクセスする方法を探しています。
EDIT
は私が
ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(this);
を使用して、昨日みました。しかし、それは動作しませんでした。これは正しい方法ですか?
答えは、このように、言ったように私はまた、リフレクションを使用してみました:
Field[] fields = this.getClass().getDeclaredFields();
Map<String, Object> jsonProperties = new HashMap<String, Object>();
for (Field field : fields) {
logger.debug(methodName, "Field " + field.getName());
field.setAccessible(true);
try {
if(field.isAnnotationPresent(JsonProperty.class)) {
Object obj = new Object();
logger.debug(methodName, "Field: " + field.getName());
jsonProperties.put(field.getName(), field.get(obj));
}
} catch(Exception e) {
logger.error(methodName, "Errore", e);
}
}
しかし、それは私が解決することができないんだけど例外返さ:
、IllegalArgumentExceptionを:ネット設定できません。 .sf.json.JSONObjectフィールドからjava.lang.Object
ジャクソンはこれらのプロパティにアクセスできます。 –