Jackson JSON、REST 数据绑定和HashMap 问题

发布于 2024-10-31 18:40:22 字数 288 浏览 1 评论 0原文

我使用 Spring 实现了 RESTful Web 服务,并使用 Jackson JSON 作为 JSON 对象的序列化器/反序列化器。

然而,当要反序列化的对象包含 HashMap 时,我遇到了错误 415:

private Map<String, String> requestMap = new HashMap<String, String>();

如果我删除它,一切都会完美运行。这是一个已知问题吗?有任何修复吗?

谢谢, 斯里

I implemented a RESTful web service with Spring and am using Jackson JSON as the serializer / deserializer for JSON objects.

However I run into Error 415's when the object that is to be deserialized contains a HashMap:

private Map<String, String> requestMap = new HashMap<String, String>();

If I remove this, everything works perfectly. Is this a known issue? Are there any fixes?

Thanks,
Sri

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

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

发布评论

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

评论(2

递刀给你 2024-11-07 18:40:22

严格来说,Jackson 从接口类型引用进行序列化就很好。下面就这一点进行论证。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper();

    Map<String, String> requestMap = new HashMap<String, String>();
    requestMap.put("one", "1");
    requestMap.put("two", "2");

    System.out.println(mapper.writeValueAsString(requestMap));
    // output: {"two":"2","one":"1"}

    List<UserPermission> userPermissions = new ArrayList<UserPermission>();
    userPermissions.add(new UserPermissionImpl("domain1"));
    userPermissions.add(new UserPermissionImpl("domain2"));

    System.out.println(mapper.writeValueAsString(userPermissions));
    // output: [{"scope":"domain1"},{"scope":"domain2"}]

    Container container = new ContainerImpl(requestMap, userPermissions);

    // From an Interface-type reference, where the implementation is an object with two Interface-type references:
    System.out.println(mapper.writeValueAsString(container));
    // {"requestMap":{"two":"2","one":"1"},"userPermissions":[{"scope":"domain1"},{"scope":"domain2"}]}
  }
}

interface UserPermission {}

class UserPermissionImpl implements UserPermission
{
  public String scope;
  UserPermissionImpl(String scope) { this.scope = scope; }
}

interface Container {}

class ContainerImpl implements Container
{
  public Map<String, String> requestMap;
  public List<UserPermission> userPermissions;

  ContainerImpl(Map<String, String> requestMap, List<UserPermission> userPermissions)
  { this.requestMap = requestMap; this.userPermissions = userPermissions; }
}

您使用的系统还存在其他问题。

Strictly speaking, Jackson serializes from Interface-type references just fine. The following demonstrates this point.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper();

    Map<String, String> requestMap = new HashMap<String, String>();
    requestMap.put("one", "1");
    requestMap.put("two", "2");

    System.out.println(mapper.writeValueAsString(requestMap));
    // output: {"two":"2","one":"1"}

    List<UserPermission> userPermissions = new ArrayList<UserPermission>();
    userPermissions.add(new UserPermissionImpl("domain1"));
    userPermissions.add(new UserPermissionImpl("domain2"));

    System.out.println(mapper.writeValueAsString(userPermissions));
    // output: [{"scope":"domain1"},{"scope":"domain2"}]

    Container container = new ContainerImpl(requestMap, userPermissions);

    // From an Interface-type reference, where the implementation is an object with two Interface-type references:
    System.out.println(mapper.writeValueAsString(container));
    // {"requestMap":{"two":"2","one":"1"},"userPermissions":[{"scope":"domain1"},{"scope":"domain2"}]}
  }
}

interface UserPermission {}

class UserPermissionImpl implements UserPermission
{
  public String scope;
  UserPermissionImpl(String scope) { this.scope = scope; }
}

interface Container {}

class ContainerImpl implements Container
{
  public Map<String, String> requestMap;
  public List<UserPermission> userPermissions;

  ContainerImpl(Map<String, String> requestMap, List<UserPermission> userPermissions)
  { this.requestMap = requestMap; this.userPermissions = userPermissions; }
}

There's some other problem in the system you're using.

鹿港小镇 2024-11-07 18:40:22

发现了问题。 Jackson JSON 在接口方面有困难。因此,在定义中,使用 HashMap 和 ArrayList 而不是 Map 和 List。不确定这是否是一个完美的解决方案,但它对我有用。

Discovered the problem. Jackson JSON has difficulties with Interfaces. So in the definitions, use HashMaps and ArrayLists instead of Maps and Lists. Not sure if this is a perfect solution, but it works for me.

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