从Struts2的JSON处理中排除属性 [英] Excluding properties from JSON processing in Struts2

查看:87
本文介绍了从Struts2的JSON处理中排除属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下(完整的)实体类.

I have the following (full) entity class.

public class StateTable implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "state_id", nullable = false)
    private Long stateId;
    @Column(name = "state_name", length = 45)
    private String stateName;
    @OneToMany(mappedBy = "stateId", fetch = FetchType.LAZY)
    private Set<UserTable> userTableSet;
    @OneToMany(mappedBy = "stateId", fetch = FetchType.LAZY)
    private Set<City> citySet;
    @OneToMany(mappedBy = "stateId", fetch = FetchType.LAZY)
    private Set<Inquiry> inquirySet;
    @OneToMany(mappedBy = "shippingState", fetch = FetchType.LAZY)
    private Set<OrderTable> orderTableSet;
    @OneToMany(mappedBy = "paymentState", fetch = FetchType.LAZY)
    private Set<OrderTable> orderTableSet1;
    @JoinColumn(name = "country_id", referencedColumnName = "country_id")
    @ManyToOne(fetch = FetchType.LAZY)
    private Country countryId;

    public StateTable() {
    }

    public StateTable(Long stateId) {
        this.stateId = stateId;
    }

    public Long getStateId() {
        return stateId;
    }

    public void setStateId(Long stateId) {
        this.stateId = stateId;
    }

    public String getStateName() {
        return stateName;
    }

    public void setStateName(String stateName) {
        this.stateName = stateName;
    }

    @XmlTransient
    public Set<UserTable> getUserTableSet() {
        return userTableSet;
    }

    public void setUserTableSet(Set<UserTable> userTableSet) {
        this.userTableSet = userTableSet;
    }

    @XmlTransient
    public Set<City> getCitySet() {
        return citySet;
    }

    public void setCitySet(Set<City> citySet) {
        this.citySet = citySet;
    }

    @XmlTransient
    public Set<Inquiry> getInquirySet() {
        return inquirySet;
    }

    public void setInquirySet(Set<Inquiry> inquirySet) {
        this.inquirySet = inquirySet;
    }

    @XmlTransient
    public Set<OrderTable> getOrderTableSet() {
        return orderTableSet;
    }

    public void setOrderTableSet(Set<OrderTable> orderTableSet) {
        this.orderTableSet = orderTableSet;
    }

    @XmlTransient
    public Set<OrderTable> getOrderTableSet1() {
        return orderTableSet1;
    }

    public void setOrderTableSet1(Set<OrderTable> orderTableSet1) {
        this.orderTableSet1 = orderTableSet1;
    }

    public Country getCountryId() {
        return countryId;
    }

    public void setCountryId(Country countryId) {
        this.countryId = countryId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (stateId != null ? stateId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof StateTable)) {
            return false;
        }
        StateTable other = (StateTable) object;
        if ((this.stateId == null && other.stateId != null) || (this.stateId != null && !this.stateId.equals(other.stateId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "model.StateTable[ stateId=" + stateId + " ]";
    }
}

我仅需要此类中的两个属性作为JSON响应,即stateIdstateName.必须忽略其余属性,以免由JSON处理/序列化.

I need only two properties from this class as a JSON response namely, stateId and stateName. The rest of the properties must be ignored from being processed/serialized by JSON.

我尝试将json.excludeProperties设置为json拦截器,如下所示.

I have tried to set json.excludeProperties to the json interceptor as follows.

@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value="json-default")
public final class StateListAction extends ActionSupport implements Serializable, ValidationAware
{    
    @Autowired
    private final transient SharableService sharableService=null;
    private static final long serialVersionUID = 1L;

    private Long id;
    List<StateTable>stateTables=new ArrayList<StateTable>();

    public StateListAction() {}    

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @JSON(name="stateTables")
    public List<StateTable> getStateTables() {
        return stateTables;
    }

    public void setStateTables(List<StateTable> stateTables) {
        this.stateTables = stateTables;
    }

    @Action(value = "PopulateStateList",
            results = {
                @Result(type="json", name=ActionSupport.SUCCESS, params={"json.enableSMD", "true", "json.enableGZIP", "true", "json.excludeNullProperties", "true", "json.root", "stateTables", "json.excludeProperties", "userTableSet, citySet, inquirySet, orderTableSet, orderTableSet1, countryId", "validation.validateAnnotatedMethodOnly", "true"})})
    public String populateStateList() throws Exception
    {
        System.out.println("countryId = "+id);
        stateTables=sharableService.findStatesByCountryId(id);
        return ActionSupport.SUCCESS;
    }
}

执行完此操作后,其余的属性将被忽略,但似乎不起作用.生成了与所有实体类相关联的SQL语句的数量,这反过来又导致发生其他严重错误,例如,

The remaining properties are expected to be ignored after doing this but it doesn't seem to work. Number of SQL statements associated with all of the entity classes are generated which in turn causes other severe errors to occur like,

org.apache.struts2.json.JSONException: java.lang.IllegalAccessException: Class 
org.apache.struts2.json.JSONWriter can not access a member of class 
org.joda.time.tz.DateTimeZoneBuilder$PrecalculatedZone with modifiers "public"

我在这里想念什么?如何忽略除stateIdstateName之外的所有属性?

What am I missing here? How to ignore all the properties except stateId and stateName?

我正在使用Struts2-json-plugin-2.3.16.

I'm using Struts2-json-plugin-2.3.16.

推荐答案

您需要在json结果中配置includeProperties.例如

You need to configure includeProperties in the json result. For example

@Result(type="json", params = {"contentType", "text/javascript", "includeProperties",
  "stateTables\\[\\d+\\]\\.stateId,stateTables\\[\\d+\\]\\.stateName"}) 

这篇关于从Struts2的JSON处理中排除属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆