[英]restAPI json array in Object extract
我收到一個錯誤: com.fasterxml.jackson.databind.exc.MismatchedInputException:無法反序列化類型的值`java.util.ArrayList<com.hatach.backend.models。
可能是因為我在 Main 中的ObjectMapper或 Entity 中的ArrayList<>期間的數據類型錯誤...我不知道如何 go 修復它,因為我似乎不明白錯誤具體在哪里,或者我是否應該 go關於它的不同方式(即:不在 main 中執行業務邏輯並創建一個 serviceImpl ,也許還有一個 dto package。
我想這個問題有兩個方面:(1)如何解決這個錯誤。 (2)關於在restAPIs中標記json數據的go的良好標准化最佳方法是什么,然后我們可以通過弄亂url對其進行排序。 即 localhost:8080/recipes?name=scrambledEggs
1) JSON 文件
"recipes": [
{
"name": "scrambledEggs",
"ingredients": [
"1 tsp oil",
"2 eggs",
"salt"
],
"instructions": [
"Beat eggs with salt",
"Heat oil in pan",
"Add eggs to pan when hot",
"Gather eggs into curds, remove when cooked",
"Salt to taste and enjoy"
]
},
{
"name": "garlicPasta",
"ingredients": [
"500mL water",
"100g spaghetti",
"25mL olive oil",
"4 cloves garlic",
"Salt"
],
"instructions": [
"Heat garlic in olive oil",
"Boil water in pot",
"Add pasta to boiling water",
"Remove pasta from water and mix with garlic olive oil",
"Salt to taste and enjoy"
]
2) 實體
public class RecipesInfo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private ArrayList<RecipesFile> recipes;
//get + set
public class RecipesFile {
private String name;
private ArrayList<String> ingredients;
private ArrayList<String> instructions;
//get + set
3) 主文件
ublic class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
@Bean
CommandLineRunner runner(RecipesService recipesService) {
return args -> {
ObjectMapper mapper = new ObjectMapper();
TypeReference<ArrayList<RecipesInfo>> typeReference = new TypeReference<ArrayList<RecipesInfo>>() {};
InputStream inputStream =TypeReference.class.getResourceAsStream("/json/data.json");
try {
ArrayList<RecipesInfo> recipes = mapper.readValue(inputStream, typeReference);
recipesService.save(recipes);
System.out.println("data saved: recipes");
} catch (IOException e) {
System.out.println("unable to save data: recipes");
System.out.println(e);
}
};
JSON 應該包含一個數組或一個 JSON object。 在您的情況下,根據您的實體,您應該在recipes
數組周圍添加大括號
{
"recipes": [
{
"name": "scrambledEggs",
"ingredients": [
"1 tsp oil",
"2 eggs",
"salt"
],
"instructions": [
"Beat eggs with salt",
"Heat oil in pan",
"Add eggs to pan when hot",
"Gather eggs into curds, remove when cooked",
"Salt to taste and enjoy"
]
},
{
"name": "garlicPasta",
"ingredients": [
"500mL water",
"100g spaghetti",
"25mL olive oil",
"4 cloves garlic",
"Salt"
],
"instructions": [
"Heat garlic in olive oil",
"Boil water in pot",
"Add pasta to boiling water",
"Remove pasta from water and mix with garlic olive oil",
"Salt to taste and enjoy"
]
]
}
之后,您可以簡單地反序列化單個RecipesInfo
而不是它的集合
RecipesInfo recipes = mapper.readValue(inputStream, RecipesInfo.class);
另外,我注意到您的內部RecipesFile
class 不是static
。 Object 映射器將無法創建RecipesFile
class 的實例
public class RecipesInfo {
...
public static class RecipesFile {
private String name;
private ArrayList<String> ingredients;
private ArrayList<String> instructions;
//get + set
...
關於第二個問題,可以查看下面的Stackoverflow 文章。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.