2
@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": "Einstein@gmail.com",
                    "course": [
                        10
                    ]
                }
            }
        ]
    }
]
Cen Jake
  • 21
  • 4

1 Answers1

0

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;
    }
}
cuneytykaya
  • 579
  • 1
  • 5
  • 14
  • sorry but i think you confused this with REST Controller, this is a REST API consumer. this method is trying to fetch data from the data base through REST API. which is done through apiC.getAll() will call: List getAll(); , which it ll send a get request to the REST API – Cen Jake Aug 29 '22 at 08:08
  • i managed to solved the bugs on my own, the problem was, there is a nested exception, aka a cycle-loop inside the entity classes (on the REST Consumer project), which student.courseList call course.java, and course.student call student.java. i just have to break the bidirectional relationship, and problem solved – Cen Jake Aug 29 '22 at 08:15