本文介绍了如何使用Moxy和Jersey从HashMap返回JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Moxy的Jersey 2.17,我的功能如下:

I am using Jersey 2.17 with Moxy and I have functions like :

@Produces(APPLICATION_JSON)
@Restricted
public List<User> getFriends(
        @PathParam("user") String user
) {
    return userDAO.getFriends(user);
}

User.preferences是 HashMap

User.preferences is a HashMap.

几乎所有对象都可以正常工作,除了 HashMap ,它被翻译成:

It works fine for almost all Objects except for a HashMap which gets translated into:

但是我会真的很想返回只是一个javascript对象,如:

but what I would really like to return is just a javascript object like:

我该怎么做?

推荐答案

是的MOXy和地图不能很好地运作。这很难过,因为JSON只不过是映射键/值对。如果你想使用MOXy,你需要使用。在这种情况下,您希望JSON的方式,您将需要创建一个具有所有可能的首选项的名称的类型(类)。任意键值对采用您需要的形式,但由于MOXy无法执行地图,因此您需要将其映射到您自己的类型。例如

Yeah MOXy and Maps don't work well. It's sad, as JSON is nothing more than mapped key/value pairs. If you want to use MOXy, you will need to use an XmlAdapter. In which case, the way you want the JSON, you will need to create a type (class) that has the name of all possible preferences. Arbitrary key value pairs should be in the form you require, but since MOXy can't do maps, you'll need to map it to your own type. For instance

public class PreferencesAdapter extends XmlAdapter<Preference, HashMap<String, String>> {

    @XmlRootElement
    public static class Preference {
        public String language;
        public String country;
    }

    @Override
    public HashMap<String, String> unmarshal(Preference p) throws Exception {
        HashMap<String, String> map = new HashMap<>();
        map.put("language", p.language);
        map.put("country", p.country);
        return map;
    }


    @Override
    public Preference marshal(HashMap<String, String> v) throws Exception {
        Preference p = new Preference();
        p.language = v.get("language");
        p.country = v.get("country");
        return p;
    }
}

您的DTO

@XmlRootElement
public class User {
    @XmlJavaTypeAdapter(PreferencesAdapter.class)
    public HashMap<String, String> preferences;
}

但如果我们要这么做,我们为什么不呢?只需首先使用首选项对象而不是 Map ?这是一个修辞问题。我完全明白你为什么不这样做。但这是MOXy的局限之一,因为它让我们对JAXB有了很大的帮助,而JAXB从来没有与Map很好地合作,这很可悲,就像我说的那样,JSON实际上只不过是一个关键值的映射。

But if we're doing to do all this, why don't we just use a Preferences object in the first place instead of a Map? That was a rhetorical question. I totally understand why you wouldn't. But this is one of the limitations of MOXy, as it makes heavy us of JAXB under the hood, and JAXB has never played nicely with Map, which is sad, as like I said, JSON is really nothing more than a Map of key values.

由于这个原因,以及我过去遇到的其他许多其他人,我建议不要使用MOXy,即使是泽西岛推荐的。相反,使用杰克逊。杰克逊已成为JSON处理的事实Java goto一段时间了。对于杰克逊,只需使用此依赖关系

For this reason, and actually many others I have encountered in the past, I do no recommend using MOXy even though it is recommended by Jersey. Instead, use Jackson. Jackson has been the defacto Java goto for JSON processing for a while. For Jackson, just use this dependency

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey.version}</version>
</dependency>

如果你取出MOXy依赖项,这个Jackson模块应该可以解决问题。否则,如果您保留MOXy依赖关系,则需要注册 JacksonFeature

If you take out the MOXy dependency, this Jackson module should work out the box. Otherwise if you leave the MOXy dependency, you will need to register the JacksonFeature

这篇关于如何使用Moxy和Jersey从HashMap返回JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 10:19