我使用以下postmapping来接收3个参数:
@PostMapping(value = "/createJobs",
consumes="application/json",
produces="application/json")
public @ResponseBody ResponseEntity<HttpStatus> createJobs(
@RequestBody ArrayList<Long> sizes,
@RequestBody Long accounts,
@RequestBody Long productId
) {
System.out.println(sizes + " " + accounts + " " + productId);
try {
jobService.createJobs(productId, sizes, accounts);
return ResponseEntity.status(HttpStatus.OK).build();
}
catch (final Exception e) {
LOGGER.error(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
我用angular发送post请求。我发送的数据如下所示:
{
"productId":715,
"sizes":[3,5],
"accounts":3
}
但在发送post请求后,我收到以下错误:
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.util.ArrayList<java.lang.Long>` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<java.lang.Long>` out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 1]]
2条答案
按热度按时间rjzwgtxy1#
post请求主体中的键应该是productid,而不是product。
piok6c0g2#
而将每个请求键放在带有
@RequestBody
,建议为每个请求模型创建一个通用类。e、 g.我的控制器:
我的请求模型: