英文:
Java Spring receiving list of longs prints MismatchedInputException
问题
@PostMapping注解用于接收三个参数:
@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]]
英文:
I use the following PostMapping to receive 3 parameters:
@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();
}
}
I use Angular to send the post request. The data I send looks like this:
{
"productId":715,
"sizes":[3,5],
"accounts":3
}
But after sending the post-request, I receive the following error:
.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]]
答案1
得分: 2
虽然通过 @RequestBody
将每个请求键放在方法参数上是方便的,但建议为每个请求模型创建一个通用的类。
例如,我的控制器:
ResponseEntity<TransactionLog> checkDiscount(HttpServletRequest request,
@RequestBody RequestCheckDiscount requestBody) {
// 在此处编写您的代码
}
我的请求模型:
public class RequestCheckDiscount {
private String username;
private int amount;
private long time;
// 标准的获取器和设置器。
}
英文:
While it is convenient to put every Request Key on method arguments with @RequestBody
, it is advisable to make a generalized class for each Request Model.
e.g. My Controller:
ResponseEntity<TransactionLog> checkDiscount(HttpServletRequest request,
@RequestBody RequestCheckDiscount requestBody) {
// Your code here
}
My Request Model:
public class RequestCheckDiscount {
private String username;
private int amount;
private long time;
// Standard getters and setters.
}
答案2
得分: 0
在POST请求的正文中,关键字应为productId,而不是product。
英文:
The key in the body of the POST request should be productId and not product.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论