1

我正在开发球衣网络服务。 下面这样是我在我的球衣客户端收到的 json 字符串

{"license":"12345","dimRequirement":[{"alternateId":"AlternateID","classificati‌​on":"1"}] }

但是当我解析时它给出了错误。我使用杰克逊进行解析

    Unrecognized field "dimRequirement" (Class com.vxl.AppObject), not marked as ignorable
at [Source: java.io.StringReader@7be6f06c; line: 1, column: 49] (through reference chain: com.vxl.appanalytix.AppObject["dimRequirement"])
    at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:244)
    at org.codehaus.jackson.map.deser.StdDeserializer.reportUnknownProperty(StdDeserializer.java:589)
    at org.codehaus.jackson.map.deser.StdDeserializer.handleUnknownProperty(StdDeserializer.java:575)
    at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:684)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:515)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:351)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2131)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1402)
    at com.vxl.CheckJersy.main(CheckJersy.java:56)

我尝试通过添加类型参考进行解析

new ObjectMapper().readValue(
                output,new TypeReference<AppObject<DimRequirement>>() { } );

泛型类

    @XmlRootElement
@XmlSeeAlso({ DimRequirement.class})
    public class AppObject<T> implements Serializable {

        private List<T> list;
        private String license;

        public AppObject() {
            list = new ArrayList<T>();

        }

        public AppObject(List<T> list) {
            this.list = list;
        }

        @XmlAnyElement(lax = true)
        public List<T> getList() {
            return list;
        }

        public void setList(List<T> list) {
            this.list = list;
        }

        public String getLicense() {
            return license;
        }

        public void setLicense(String license) {
            this.license = license;
        }

    }

服务 我想为多个返回的 POST 请求使用相同的服务 AppObject<xxx>,AppObject<yyy>

@POST
    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    @Path("/getreq")
    @Override
    public AppObject getAllByClientNIsCurrent(
            AppObject<DimRequirement> appObject) {
        List<DimRequirement> dimreqlist = dimRequirementDao
                .getAllByClientNIsCurrent(appObject.getClientKey());
        AppObject<DimRequirement> appObject = new AppObject<DimRequirement>();
        appObject.setList(dimreqlist);
        return appObject;
    }
4

2 回答 2

0

您的 AppObject 类具有 JSON 中不存在的字段。您需要使用杰克逊注解来制作类,以说明它可以被忽略。

于 2013-10-03T10:32:09.110 回答
0

对不起,关于我的其他回答,请忽略。

我让你的代码按原样工作。所需要的只是变量“list”需要命名为“dimRequirement”,以便自动映射工作。您还需要为此修改 getter 和 setter,因为某些 Jackson 设置可以使库使用它们。

于 2013-10-03T12:19:53.207 回答