简体   繁体   English

Java - Object Mapper - 要列出的数字的JSON数组<Long>

[英]Java - Object Mapper - JSON Array of Number to List<Long>

In my front end I send this JSON: 在我的前端,我发送这个JSON:

"ids": [ 123421, 15643, 51243],
"user": {
   "name": "John",
   "email": "john@sovfw.com.br" 
}

To my Spring Endpoint below: 到我的Spring Endpoint:

@PostMapping(value = "/sendToOficial")
public ResponseEntity<?> sendToOficial(@RequestBody Map<String, Object> payload) {

ObjectMapper mapper = new ObjectMapper();
List<Long> pointsIds = mapper.convertValue( payload.get("pointsIds"), List.class );
UsuarioDTO autorAlteracao = mapper.convertValue(payload.get("user"), UsuarioDTO.class);

for (Long idPoint : pointsIds) { ... }

But I'm getting a Cast Exception in the for saying that it can't cast Integer to Long. 但我得到一个Cast Exception,因为它说它不能将Integer强制转换为Long。

I can't receive the "ids" numbers as Integer, I want to receive as Long. 我不能收到整数的“ids”数字,我希望收到Long。 Please, how could I do this? 拜托,我怎么能这样做?

First, define POJOs for mapping your request object: 首先,定义POJO以映射您的请求对象:

public class RequestObj implements Serializable{

    private List<Long> ids;

    private UsuarioDTO user;

    /* getters and setters here */

}

public class UsuarioDTO implements Serializable{

    private String name;
    private String email;

    /* getters and setters here */

}

And then modify your endpoint: 然后修改您的端点:

@PostMapping(value = "/sendToOficial")
public ResponseEntity<?> sendToOficial(@RequestBody RequestObj payload) {

In this way you also do not need to use an ObjectMapper . 这样您也不需要使用ObjectMapper Just call payload.getIds() . 只需调用payload.getIds()

Consider also that in this way if payload changes you'll need only to change RequestObj definition, while using ObjectMapper would force you to update also your endpoint in an important way. 还要考虑这样,如果有效负载发生变化,您只需要更改RequestObj定义,而使用ObjectMapper会强制您以一种重要的方式更新您的终端。 It's better and safer to separate payload representation from control logic. 将有效负载表示与控制逻辑分开会更好,更安全。

In jackson-databind-2.6.x and onward versions you can configure the ObjectMapper to serialize low typed int values (values that fit in 32 bits) as long values using the DeserializationFeature#USE_LONG_FOR_INTS configuration feature: jackson-databind-2.6.x及更高版本中,您可以使用DeserializationFeature#USE_LONG_FOR_INTS配置功能将ObjectMapper配置为将低类型int值(适合32位的值) DeserializationFeature#USE_LONG_FOR_INTS化为long值:

@PostMapping(value = "/sendToOficial")
public ResponseEntity<?> sendToOficial(@RequestBody Map<String, Object> payload) {

    ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature .USE_LONG_FOR_INTS, true);
    List<Long> pointsIds = mapper.convertValue( payload.get("pointsIds"), List.class );
    UsuarioDTO autorAlteracao = mapper.convertValue(payload.get("user"), UsuarioDTO.class);

    for (Long idPoint : pointsIds) { // ... }

}

If you just want your mapper to read into a List<Long> , use this trick for obtaining full generics type information by sub-classing. 如果您只是希望映射器读入List<Long> ,请使用此技巧通过子类化获取完整的泛型类型信息。

Example

ObjectMapper mapper = new ObjectMapper();
List<Long>listOfLong=mapper.readValue("[ 123421, 15643, 51243]" ,
                    new TypeReference<List<Long>>() {
                    });
System.out.println(listOfLong);

Prints 打印

[123421, 15643, 51243]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM