简体   繁体   中英

Spring Boot @RequestBody parses only 0/null values?

I have a POJO "Problem" object:

import lombok.Data;

@Data
public class Problem {
    int capacity;
    int weights[];
    int values[];
}

Which I am trying to parse in a Rest controller in Spring Boot:

@RequestMapping(value = "/calculate", method = RequestMethod.POST)
public SolutionResponse calculateSolution(@RequestBody Problem problem) {
// problem = Problem(capacity=0, weights=null, values=null) ??
// Goes on ..
}

I am posting the following with cURL:

curl -d '{"problem": {"capacity": 60, "weights": [5, 2, 22], "values": [2, 5, 30]}}' -H "Content-Type: application/json" -X POST http://localhost:8088/endpoint/calculate

Looking into debug mode, it is indeed parsed, but as:

problem = Problem(capacity=0, weights=null, values=null)

So, it's wrong as they should have the values that I posted.

What could be the issue?

Json you are sending from curl is not correct.
It should be

{"capacity": 60, "weights": [5, 2, 22], "values": [2, 5, 30]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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