使用Spring Cloud Open Feign,我如何读取另一种类型的嵌套JSON数据


@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。如果我去掉private Course CourseList,CourseList就是Course.class。

将其返回为List<Object> All有效,但我无法再访问getCourseList()方法。

所以我的问题是,如何访问另一种类型的嵌套JSON?

如果无法实现,那么我如何解析List All to Student和Course对象,以便对它们进行操作?

以下是我尝试使用的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
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;
}
}

最新更新