Reputation: 1
@FeignClient(name="service", url="http://localhost:80/")
public interface apiService {
@GetMapping(value = "/student")
@Headers(value = "Content-Type: application/json")
List<Student> getAll();
}
Blockquote
@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;
}
}
Blockquote
@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";
}
}
Blockquote
@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;
}
}
What I m getting syntax error when I try to do: apiC.getAll() , I get
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`)
so basically I m trying to use List<Student> all
, and to call this method all.getCourseList()
but got Exception above regardless. and CourseList is Course.class. if I remove private Course CourseList
though.
returning it as List<Object> All
, works but I can't access to the getCourseList()
method anymore.
so pretty much my question is, how do I access to nested JSON of another type?
If there is no way to pulling it off, then how do I parse the List All to Student and Course object where I can operate on them?
here is a sample JSON i m trying to consume
[
{
"id": 10,
"firstName": "Jan",
"lastName": "Cen",
"courseList": [
{
"id": 10,
"courseName": "Math",
"student": [
10
],
"teacher": {
"id": 2,
"firstName": "Albert",
"lastName": "Einstein",
"email": "[email protected]",
"course": [
10
]
}
}
]
}
]
Upvotes: 0
Views: 957
Reputation: 581
Your controller must return a list of students. For now, you are just returning a string "student". You can update as below.
@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;
}
}
Upvotes: 0