繁体   English   中英

restAPI json 数组中的 Object 提取

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM