Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取用@JsonProperty标记的类字段中存储的值_Java_Json_Struts2_Subclass - Fatal编程技术网

Java 如何获取用@JsonProperty标记的类字段中存储的值

Java 如何获取用@JsonProperty标记的类字段中存储的值,java,json,struts2,subclass,Java,Json,Struts2,Subclass,我正在struts2中使用json插件处理一个项目:我有一个BaseAction,它扩展了ActionSupport,并声明了一些用@JsonProperty注释标记的公共字段 public abstract class BaseAction extends ActionSupport { @JsonProperty private String commonField1; @JsonProperty private String commonField2;

我正在struts2中使用json插件处理一个项目:我有一个BaseAction,它扩展了ActionSupport,并声明了一些用@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
    }
}
我正在寻找一种方法来访问BaseAction中存储在所有@JsonProperty字段中的所有值

编辑

我昨天试过使用

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);
                }

            }
Field[]fields=this.getClass().getDeclaredFields();
Map jsonProperties=newhashmap();
用于(字段:字段){
debug(methodName,“Field”+Field.getName());
字段。setAccessible(true);
试一试{
if(field.isAnnotationPresent(JsonProperty.class)){
Object obj=新对象();
debug(methodName,“Field:+Field.getName());
jsonProperties.put(field.getName(),field.get(obj));
}
}捕获(例外e){
logger.error(methodName,“error”,e);
}
}
但它返回了一个我无法解决的异常:


IllegalArgumentException:无法将net.sf.json.JSONObject字段设置为java.lang.Object

从超类访问属性的唯一方法是通过反射

在BaseAction.execute()方法中,您可以找到实际的对象类,然后遍历所有属性,检查属性是否具有@JsonProperty注释。如果找到注释,则可以获取名称和属性值


我建议您更改系统设计,这样您就不需要这样做。

从超类访问属性的唯一方法是通过反射

在BaseAction.execute()方法中,您可以找到实际的对象类,然后遍历所有属性,检查属性是否具有@JsonProperty注释。如果找到注释,则可以获取名称和属性值


我建议您更改系统设计,这样您就不需要这样做。

Jackson可以访问这些属性。Jackson可以访问这些属性。