【发布时间】:2019-12-14 21:54:21
【问题描述】:
我在调用 Angular 函数时收到此错误:
2019-08-07 11:13:37.444 WARN 16320 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.ecommerce.app.securedapp.model.viewModels.CartOwnerUsernameModel` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.ecommerce.app.securedapp.model.viewModels.CartOwnerUsernameModel` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 1, column: 2]]
Angular 功能:
getCartProducts() {
let username = sessionStorage.getItem(AUTHENTICATED_USER)
return this.http.post<any>(`${API_URL}/order/details/cart`, {username})
}
现在在 intelliJ 项目中,我有:
CartOwnerUsernameModel 类:
public class CartOwnerUsernameModel {
private String username;
public CartOwnerUsernameModel(String username) {
this.username = username;
}
//getters, setters and toString
控制器类中的方法是:
@PostMapping(
value = "/cart",
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
public ResponseEntity<List<CartModelView>> getCartItems(@RequestBody CartOwnerUsernameModel cartOwnerUsernameModel) {
System.out.println(cartOwnerUsernameModel);
return orderDetailsService.retrieveCartItems(cartOwnerUsernameModel);
}
注意,它甚至没有在控制台中打印 cartOwnerUsernameModel 实例。
当我在 chrome 浏览器中查看请求时,我看到以下内容:
Request URL: http://localhost:8080/order/details/cart
Request Method: POST
Status Code: 400
Remote Address: [::1]:8080
Referrer Policy: no-referrer-when-downgrade
请求有效载荷是:
{"username":"dan"}
如果这里的信息不完整,请告诉我您认为是什么问题。
【问题讨论】:
标签: json angular spring-boot