返回列表时没有得到正确的JSON

时间:2013-02-20 16:53:08

标签: java json spring jackson

以下是我用于以JSON

的形式撤销模型列表的方法
    @RequestMapping(value = "/fetchAddress.htm", method = RequestMethod.GET)
@ResponseBody   
public  GenericEntity<List<Address>> fetchAddress(@RequestParam(value="addressId", required=true) int addressId){   
    logger.debug("fetchQueryDetails called");
    List<Address> al =queryDao.fetchAddress(addressId);

    GenericEntity<List<Address>> gal= new GenericEntity<List<Address>>(al){};   
    return gal;      
}

}

以下是我的模型类

    @XmlRootElement(name = "Address")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class AddressImpl implements Address {
     properties, setter, getter.
    }

以下是我得到的JSON响应

    {"rawType":"java.util.ArrayList","type":{"actualTypeArguments":["com.mvp.Address"],"rawType":"java.util.List","ownerType":null},"entity":[{"flatno":"S-2","houseNo":"42","street":"mother dairy","sector":"sec-2A","city":"kashi","state":"U.P.","country":"India","pin":"200001"},{"flatno":"S-2222222","houseNo":"42","street":"mother dairrrrrrrry","sector":"sec-2AAaa","city":"varansi","state":"U.P.","country":"India","pin":"201101"}]}

现在在JSON响应中我不希望输出中的rawType,type,ownerType以及要更改的实体名称类似于addressList。还有什么想法我怎样才能避免bean类的某些属性不出现在JSON中。我正在使用Jackson库来创建JSON。以下是我正在使用的罐子。

     jackson-mapper-asl-1.9.2.jar,
     jackson-xc-1.9.2.jar,
     jackson-jaxrs-1.9.2.jar,
     jackson-core-asl-1.9.2.jar

任何人都面临同样的情况,请建议。

由于

1 个答案:

答案 0 :(得分:0)

您可以使用注释@JsonProperty自定义名称:

@JsonProperty("NameOfProperty")
public String getProperty() {
    return property;
}

如果您不需要属性,请使用以下命令忽略它:@JsonIgnoreProperties

@JsonIgnoreProperties
public String getPropertyToExclude() {
    return propertyToExclude;
}