簡體   English   中英

使用 Spring-Cloud Open Feign,我如何讀取另一種類型的嵌套 JSON 數據?

[英]using Spring-Cloud Open Feign, how do i read nested JSON data of another type?

@FeignClient(name="service", url="http://localhost:80/")
public interface apiService {

@GetMapping(value = "/student")
@Headers(value = "Content-Type: application/json")
List<Student> getAll();


} 

塊引用

@RestController
    @RequestMapping("/apiController")
    @Primary
    public class apiController implements apiService{
    
        @Autowired
        private apiService proxy;
    
        @Override
        public List<Student> getAll() {
            List<Student> all = proxy.getAll();
            return all;
        }
    }

       

塊引用

    @Controller
public class mvcController  {
    @Autowired
    apiController apiC;

    @GetMapping("/student")
    public String getAll(Model m) {
        List<Student> student = apiC.getAll();
        System.out.println(student.get(0).getCourseList());
        return "student";
    }
}

塊引用

@NoArgsConstructor
@Getter
@Setter
@AllArgsConstructor

public class Student {

private int id;

private String firstName;

private String lastName;

 private List<Course> courseList;


    public Student(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

當我嘗試做時遇到語法錯誤:apiC.getAll(),我得到了

Error while extracting response for type [java.util.List<com.example.restconsume.Entity.Student>] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `com.example.restconsume.Entity.Student` from Array value (token `JsonToken.START_ARRAY`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `com.example.restconsume.Entity.Student` from Array value (token `JsonToken.START_ARRAY`) at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 99] (through reference chain: java.util.ArrayList[0]->com.example.restconsume.Entity.Student["courseList"]->java.util.ArrayList[0]->com.example.restconsume.Entity.Course["student"])
feign.codec.DecodeException: Error while extracting response for type [java.util.List<com.example.restconsume.Entity.Student>] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `com.example.restconsume.Entity.Student` from Array value (token `JsonToken.START_ARRAY`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `com.example.restconsume.Entity.Student` from Array value (token `JsonToken.START_ARRAY`)

所以基本上我正在嘗試使用List<Student> all ,並調用這個方法all.getCourseList()但無論如何都得到了 Exception 。 CourseList 是 Course.class。 如果我刪除private Course CourseList

將其作為List<Object> All返回,可以,但我無法再訪問getCourseList()方法。

所以幾乎我的問題是,我如何訪問另一種類型的嵌套 JSON?

如果沒有辦法將其關閉,那么如何將 List All 解析為 Student 和 Course object 我可以對它們進行操作?

這是我嘗試使用的示例 JSON

[
    {
        "id": 10,
        "firstName": "Jan",
        "lastName": "Cen",
        "courseList": [
            {
                "id": 10,
                "courseName": "Math",
                "student": [
                    10
                ],
                "teacher": {
                    "id": 2,
                    "firstName": "Albert",
                    "lastName": "Einstein",
                    "email": "Einstein@gmail.com",
                    "course": [
                        10
                    ]
                }
            }
        ]
    }
]

您的 controller 必須返回學生列表。 現在,您只是返回一個字符串“student”。 您可以如下更新。

@Controller
public class mvcController  {
    @Autowired
    apiController apiC;

    @GetMapping("/student")
    public List<Student>getAll(Model m) {
        List<Student> student = apiC.getAll();
        System.out.println(student.get(0).getCourseList());
        return student;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM