java spring接收长打印不匹配的列表InputException

cuxqih21  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(285)

我使用以下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]]
rjzwgtxy

rjzwgtxy1#

post请求主体中的键应该是productid,而不是product。

piok6c0g

piok6c0g2#

而将每个请求键放在带有 @RequestBody ,建议为每个请求模型创建一个通用类。
e、 g.我的控制器:

ResponseEntity<TransactionLog> checkDiscount(HttpServletRequest request, 
                                             @RequestBody RequestCheckDiscount requestBody) {
    // Your code here
}

我的请求模型:

public class RequestCheckDiscount {
    private String username;
    private int amount;
    private long time;

    // Standard getters and setters.
}

相关问题