![](/img/trans.png)
[英]How do I use the ConsulDiscoveryClient with Zuul and Sidecar in Spring-Cloud
[英]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.