我正在嘗試使用 Java 的多型性。我用 Spring Boot 構建了一個簡單的 CRUD 應用程式,我想用 Postman 測驗它......
問題是,我得到了下一個例外:
WARN 4576 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Could not resolve subtype of [simple type, class com.shoppingprojectwithhibernate.PromotionsModule.Domain.PromotionSeason]: missing type id property 'promotionSeason' (for POJO property 'promotionSeason'); nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class com.balabasciuc.shoppingprojectwithhibernate.PromotionsModule.Domain.PromotionSeason]: missing type id property 'promotionSeason' (for POJO property 'promotionSeason')<EOL> at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 3, column: 25] (through reference chain: com.shoppingprojectwithhibernate.PromotionsModule.Domain.Promotion["promotionSeason"])]
當我嘗試向端點發出 POST 請求時,該端點回傳一個介面,并且在運行時可以是任何子型別,我在這里滾動查看解決方案,我使用了其他解決方案中使用的 jackson,但到目前為止沒有一個對我有用,請給我一個提示好嗎?
my interface:
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "promotionSeason")
@JsonSubTypes(
{
@JsonSubTypes.Type(value = PromotionChristmasSeason.class, name = "christmasPromotion"),
@JsonSubTypes.Type(value = PromotionEasterSeason.class, name = "easterPromotion"),
@JsonSubTypes.Type(value = NoPromotionForYouThisTimeMUHAHA.class, name = "noPromotion")
})
public interface PromotionSeason {
String isSeason();
Interface subtype:
@JsonTypeName(value = "noPromotion")
public class NoPromotionForYouThisTimeMUHAHA implements PromotionSeason {
Promotion class Entity constructor:
public Promotion(int numberOfProductsAtPromotion, PromotionSeason promotionSeason) {
this.numberOfProductsAtPromotion = numberOfProductsAtPromotion;
this.promotionSeason = promotionSeason;
}
Rest Controller class
@RestController
@RequestMapping(value = "/promotions")
public class PromotionController {
@PostMapping(value = "/createPromotion")
public ResponseEntity<String> createPromotion(@RequestBody Promotion promotion)
{
promotionService.createPromotion(promotion);
return ResponseEntity.status(HttpStatus.CREATED)
.body("Done");
}
Postman request:
{
"numberOfProductsAtPromotion" : 20,
"promotionSeason" : "noPromotion"
}
Postman response:
{
"timestamp": "2022-05-04T18:58:06.873 00:00",
"status": 400,
"error": "Bad Request",
"path": "/promotions/createPromotion"
}
Spring Response:
2022-05-04 21:19:44.374 WARN 4576 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Could not resolve subtype of [simple type, class com.shoppingprojectwithhibernate.PromotionsModule.Domain.PromotionSeason]: missing type id property 'promotionSeason' (for POJO property 'promotionSeason'); nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class com.balabasciuc.shoppingprojectwithhibernate.PromotionsModule.Domain.PromotionSeason]: missing type id property 'promotionSeason' (for POJO property 'promotionSeason')<EOL> at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 3, column: 25] (through reference chain: com.shoppingprojectwithhibernate.PromotionsModule.Domain.Promotion["promotionSeason"])]
uj5u.com熱心網友回復:
您在 NoPromotionForYouThisTimeMUHAHA 中有任何構造器嗎?如果您在所有實作中都有“型別”,則可以將介面更改為抽象類并在此處添加變數。
uj5u.com熱心網友回復:
這一直是我糟糕的 Post 請求,而不是:
郵遞員要求:
{
"numberOfProductsAtPromotion" : 20,
"promotionSeason" : "noPromotion" }
它應該是:
{
"numberOfProductsAtPromotion" : 20,
"promotionSeason" : { // "promotionSeason" is class instance var from Promotion Entity class
"promotionSeason" : "noPromotion" // "promotionSeason" is defined on interface level: `@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "promotionSeason")`
//"noPromotion" is defined on interface and subtype levels
//any more subtype(subclass) variables to init if any
}
檢查這個:https ://www.youtube.com/watch?v=IlLC3Yetil0
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/476775.html
上一篇:不支持內容型別multipart/mixed/415UNSUPPORTED_MEDIA_TYPE(Spring SpringBoot升級后)
