ラップ
問題の説明
SpringBoot、オブジェクトのマップを受け入れる必要性とコントローラを書くとき、オブジェクトは、特定のカテゴリに入れ、次の通り:
public boolean postArticle(@RequestBody Map<String, Object> map) {
ArticleInfo articleInfo = (ArticleInfo) map.get("articleInfo");
ArticleContent articleContent = (ArticleContent) map.get("articleContent");
System.out.println(articleInfo + " " + articleContent);
return true;
}
バースト例外後:
java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class
cn.zi10ng.blog.domain.ArticleInfo (java.util.LinkedHashMap is in module java.base of loader
'bootstrap'; cn.zi10ng.blog.domain.ArticleInfo is in unnamed module of loader
org.springframework.boot.devtools.restart.classloader.RestartClassLoader @19b54dc3)
問題の原因
マップは直接ではなく、特定のエンティティクラスのオブジェクトに、オブジェクトを引き抜かれます
ソリューション
中間媒体としてJSONの必要性:
public boolean postArticle(@RequestBody Map<String, Object> map) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String jsonInfo = objectMapper.writeValueAsString(map.get("articleInfo"));
String jsonContent = objectMapper.writeValueAsString(map.get("articleContent"));
ArticleInfo articleInfo = objectMapper.readValue(jsonInfo,ArticleInfo.class);
ArticleContent articleContent = objectMapper.readValue(jsonContent,ArticleContent.class);
System.out.println(articleContent + " " +articleInfo);
return articleService.insertArticle(articleInfo,articleContent);
}