有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在返回列表时未获得正确的JSON

下面是我用于以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) 个答案

  1. # 1 楼答案

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

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

    如果您不需要属性,请使用@JsonIgnoreProperties忽略它

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