Java Spring接收到长整型(long)列表时会打印出” MismatchedInputException”。

huangapple go评论68阅读模式
英文:

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 = &quot;/createJobs&quot;,
         consumes=&quot;application/json&quot;,
         produces=&quot;application/json&quot;)
public @ResponseBody ResponseEntity&lt;HttpStatus&gt; createJobs(
		@RequestBody ArrayList&lt;Long&gt; sizes,
		@RequestBody Long accounts,
		@RequestBody Long productId
) {
	
	System.out.println(sizes + &quot; &quot; + accounts + &quot; &quot; + 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:

{
   &quot;productId&quot;:715,
   &quot;sizes&quot;:[3,5],
   &quot;accounts&quot;: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&lt;java.lang.Long&gt;` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList&lt;java.lang.Long&gt;` 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&lt;TransactionLog&gt; 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.

huangapple
  • 本文由 发表于 2020年8月23日 00:09:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63538306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定