Your Model
class is incorrect. Take another look at the JSON in the curl
command. Here, I've formatted it to better see:
{
"text": "#param#",
"port": [2, 3],
"param": [{
"number": "10086",
"text_param": ["bj"],
"user_id": 1
}, {
"number": "10086",
"text_param": ["ye"],
"user_id": 2
}]
}
As you can see, it consists of 2 objects, with fields:
text
, port
, param
number
, text_param
, user_id
Your Model
class fits neither:
So you needed 2 classes, e.g. like this:
class Request {
private final String text;
private final List<Integer> port;
private final List<Param> param;
// Constructor and getter methods here
}
class Param {
private final String number;
private final List<String> text_param;
private final int user_id;
// Constructor and getter methods here
}
You can of course use arrays instead of List
, but it's more common to use List
.
Notice how the Model
class in the question had declared port
as type String
with value "[0]"
. That would become JSON "port": "[0]"
, not the "port": [0]
that is needed.
Since you're using Jackson, you should use Databind, e.g. to generate the JSON used in the curl
command:
Request request = new Request(
"#param#",
Arrays.asList(2, 3),
Arrays.asList(new Param("10086", Arrays.asList("bj"), 1),
new Param("10086", Arrays.asList("ye"), 2))
);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(request);
System.out.println(json);
Output
{"text":"#param#","port":[2,3],"param":[{"number":"10086","text_param":["bj"],"user_id":1},{"number":"10086","text_param":["ye"],"user_id":2}]}
Now try that with the rest of your code. I didn't even look at the rest, given the big discrepancy in the JSON payload itself.