Spring + Jackson +反序列化通用对象列表 [英] Spring + Jackson + Deserializing List of Generic Objects

查看:227
本文介绍了Spring + Jackson +反序列化通用对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器:

@RequestMapping(value = "/test/inst/", method = RequestMethod.POST)
public @ResponseBody ResponseDTO<InstDTO> 
        testPostREST(@RequestBody RequestDTO<InstDTO> instDTO) {

    RequestDTO<InstDTO> dto = new RequestDTO<InstDTO>();

    ResponseDTO<InstDTO> responseDto = new ResponseDTO<InstDTO>();
    responseDto.setPayload(instDTO.getPayload());
    return responseDto; 
}

包含以下请求对象:

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class RequestDTO<T> {

    private List<T> payload;

    public RequestDTO() {
        System.out.println("constructor");
    }

    public RequestDTO(List<T> payload) {
        this.payload = payload;
    }

    public List<T> getPayload() {
        return payload;
    }

    public void setPayload(List<T> payload) {
        this.payload = payload;
    }
}

当POST通过后我看对象我get,有效负载列表有LinkedHashMap对象而不是我的DTO类型的对象。

When the POST comes through and I look the object I get, the payload list has LinkedHashMap objects instead of objects of my DTO type.

如何让spring + jackson将JSON转换为我的DTO对象。请记住,我计划将包装器ResponseDTO重用于其他对象列表,这就是我使用通用列表(List)的原因。

How can I make spring+jackson convert the JSON into my DTO object. Bear in mind that I plan to reuse the wrapper ResponseDTO for other lists of objects and that's why I'm using a generic list (List).

这是JSON I' m尝试。

Here's the JSON I'm trying.

{
  "payload": [
    {
      "d": "Test 0",
      "id": "abcde",
      "c": "Test 0"
    },
    {
      "d": "Test 1",
      "id": "123",
      "c": "Test 1"
    }
  ]
}


推荐答案

由于类型擦除,您的代码无法正常工作。 Jackson运行时不知道您正在尝试将有效负载编组到 InstitutionDTO 对象中(因为此信息已在编译时擦除)。当Jackson查看有效负载时,它会在线路上看到有效的JSON对象,并在Java端看到 List< Object> 。它没有别的选择,只能将有效负载映射到 Map 实现,在您的情况下似乎是 LinkedHashMap

Your code does not work due to type erasure. Jackson runtime does not know that you are trying to marshal the payload into InstitutionDTO objects (since this information has been erased at compile time). When Jackson looks at the payload, it sees valid JSON objects on the wire and a List<Object> on the Java side. It has no choice but to map the payload to a Map implementation, which in your case seems to be LinkedHashMap.

您可能能够达到以下目的:

You may be able to achieve what you are looking for as follows:

class RequestDTO<T> {}
class ResponseDTO<T> {}
class InstitutionDTO {}
class InstitutionRequestDTO extends RequestDTO<InstitutionDTO>
class InstitutionResponseDTO extends ResponseDTO<InstitutionDTO>

@RequestMapping(value = "/test/institution/", method = RequestMethod.POST)
public @ResponseBody InstitutionResponseDTO
    testPostREST(@RequestBody InstitutionRequestDTO institutionDTO) { }

免责声明:我自己没有尝试过此代码,但我的大部分应用都有代码与此类似,它可以与Jackson,Castor,Atom等一起工作,没有任何故障。

Disclaimer: I haven't tried this code myself but most of my applications have code similar to this and it works with Jackson, Castor, Atom, etc. without a glitch.

这篇关于Spring + Jackson +反序列化通用对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆