使用 Jackson ObjectMapper 和 POJO 泛型代替 LinkedHashMap

发布于 2024-12-27 16:36:20 字数 1742 浏览 0 评论 0原文

使用 Jersey 我定义一个服务,例如:

@Path("/studentIds")
public void writeList(JsonArray<Long> studentIds){
 //iterate over studentIds and save them
}

Where JsonArray is:

public class JsonArray<T> extends ArrayList<T> {  
    public JsonArray(String v) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper(new MappingJsonFactory());
        TypeReference<ArrayList<T>> typeRef = new TypeReference<ArrayList<T>>() {};
        ArrayList<T> list = objectMapper.readValue(v, typeRef);
        for (T x : list) {
            this.add((T) x);
        }
    }
}

这工作得很好,但是当我做一些更复杂的事情时:

@Path("/studentIds")
public void writeList(JsonArray<TypeIdentifier> studentIds){
 //iterate over studentIds and save them by type
}

Where the Bean is a simple POJO such as

public class TypeIdentifier {
    private String type;
    private Long id;
//getters/setters 
}

整个事情就崩溃了。它将所有内容转换为 LinkedHashMap 而不是实际对象。如果我手动创建一个类,我可以让它工作:

public class JsonArrayTypeIdentifier extends ArrayList<TypeIdentifier> { 
    public JsonArrayTypeIdentifier(String v) throws IOException  {
        ObjectMapper objectMapper = new ObjectMapper(new MappingJsonFactory());
        TypeReference<ArrayList<TypeIdentifier>> typeRef = new TypeReference<ArrayList<TypeIdentifier>>(){};
        ArrayList<TypeIdentifier> list = objectMapper.readValue(v, typeRef);
        for(TypeIdentifier x : list){
            this.add((TypeIdentifier) x);
        }
    }
 }

但我试图保持这个良好和通用,而不添加额外的类。有什么线索可以解释为什么仅在通用版本中会发生这种情况吗?

Using Jersey I'm defining a service like:

@Path("/studentIds")
public void writeList(JsonArray<Long> studentIds){
 //iterate over studentIds and save them
}

Where JsonArray is:

public class JsonArray<T> extends ArrayList<T> {  
    public JsonArray(String v) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper(new MappingJsonFactory());
        TypeReference<ArrayList<T>> typeRef = new TypeReference<ArrayList<T>>() {};
        ArrayList<T> list = objectMapper.readValue(v, typeRef);
        for (T x : list) {
            this.add((T) x);
        }
    }
}

This works just fine, but when I do something more complicated:

@Path("/studentIds")
public void writeList(JsonArray<TypeIdentifier> studentIds){
 //iterate over studentIds and save them by type
}

Where the Bean is a simple POJO such as

public class TypeIdentifier {
    private String type;
    private Long id;
//getters/setters 
}

The whole thing breaks horribly. It converts everything to LinkedHashMap instead of the actual object. I can get it to work if I manually create a class like:

public class JsonArrayTypeIdentifier extends ArrayList<TypeIdentifier> { 
    public JsonArrayTypeIdentifier(String v) throws IOException  {
        ObjectMapper objectMapper = new ObjectMapper(new MappingJsonFactory());
        TypeReference<ArrayList<TypeIdentifier>> typeRef = new TypeReference<ArrayList<TypeIdentifier>>(){};
        ArrayList<TypeIdentifier> list = objectMapper.readValue(v, typeRef);
        for(TypeIdentifier x : list){
            this.add((TypeIdentifier) x);
        }
    }
 }

But I'm trying to keep this nice and generic without adding extra classes all over. Any leads on why this is happening with the generic version only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏见 2025-01-03 16:36:20

首先,它适用于 Long,因为它是一种本机类型,因此是 JSON 整数的默认绑定。

但至于为什么通用类型信息没有正确传递:这很可能是由于 JAX-RS API 将类型传递给 MessageBodyReaderMessageBodyWriter 的方式存在问题 -传递 java.lang.reflect.Type 不足以(不幸的是!)足以传递实际的泛型声明(有关这方面的更多信息,请阅读 此博客条目)。

一种简单的解决方法是创建辅助类型,例如:

class MyTypeIdentifierArray extends JsonArray<TypeIdentifier> { }

并使用该类型 - 事情将“正常工作”,因为始终保留超类型通用信息。

First of all, it works with Longs because that is sort of native type, and as such default binding for JSON integral numbers.

But as to why generic type information is not properly passed: this is most likely due to problems with the way JAX-RS API passes type to MessageBodyReaders and MessageBodyWriters -- passing java.lang.reflect.Type is not (unfortunately!) enough to pass actual generic declarations (for more info on this, read this blog entry).

One easy work-around is to create helper types like:

class MyTypeIdentifierArray extends JsonArray<TypeIdentifier> { }

and use that type -- things will "just work", since super-type generic information is always retained.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文