0

I have my spring data jpa resource and repositories are as shown below :

In my Student.java, I have

@ManyToOne
private Teacher teacher;

StudentResource.java

@RequestMapping(value = "students/byTeacherId/{id}",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public List<Student> getAllStudents(@PathVariable Long teacherId) {
                return studentRepository.findAllByTeacher(teacherId);
    }

StudentRepository.java

List<Student> findAllByTeacher(Long teacherId);

In Teacher.Java, I have

@OneToMany(mappedBy = "teacher")
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<Student> student = new HashSet<>();

In Angularjs the ui router state :

Student.js

.state('student.byteacher', {
                parent: 'entity',
                url: '/students/byTeacherId/{id}',
                data: {
                    roles: ['ROLE_USER'],
                    pageTitle: 'traceApp.student.detail.title'
                },
                views: {
                    'content@': {
                        templateUrl: 'scripts/app/entities/student/students.html',
                        controller: 'StudentController'
                    }
                },
                resolve: {
                    translatePartialLoader: ['$translate', '$translatePartialLoader', function ($translate, $translatePartialLoader) {
                        $translatePartialLoader.addPart('student');
                        return $translate.refresh();
                    }],
                    entity: ['$stateParams', 'Student', function($stateParams, Student) {
                        return Student.getAll({id : $stateParams.id});
                    }]
                }
            })

I have my student factory as :

.factory('Student', function ($resource, DateUtils) {
        return $resource('api/students/:id', {}, {
            'query': { method: 'GET', isArray: true},
            'get': {
                method: 'GET',
                transformResponse: function (data) {
                    data = angular.fromJson(data);
                    return data;
                }
            },
            'update': { method:'PUT' },
            'getAll': { method:'GET' }
        });
    });

So from teacher.html, I have link for each teacher as:

<a ui-sref="student.byteacher(teacher.id)">Get My Students</a>

So I have expected to get all the students of the teacher when I click 'Get My Students' link in for a particular Teacher, but I am getting the following error. Can any one help me in fixing this error.

Error: [$resource:badcfg] Error in resource configuration for action `getAll`. Expected response to contain an object but got an array (Request: GET api/students)
http://errors.angularjs.org/1.4.4/$resource/badcfg?p0=getAll&p1=object&p2=array&p3=GET&p4=api%2Fstudents
minErr/<@http://localhost:8080/bower_components/angular/angular.js:68:12
resourceFactory/</Resource[name]/promise<@http://localhost:8080/bower_components/angular-resource/angular-resource.js:588:1
processQueue@http://localhost:8080/bower_components/angular/angular.js:14634:28
scheduleProcessQueue/<@http://localhost:8080/bower_components/angular/angular.js:14650:27
$RootScopeProvider/this.$get</Scope.prototype.$eval@http://localhost:8080/bower_components/angular/angular.js:15916:16
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://localhost:8080/bower_components/angular/angular.js:15727:15
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://localhost:8080/bower_components/angular/angular.js:16024:13
done@http://localhost:8080/bower_components/angular/angular.js:10511:36
completeRequest@http://localhost:8080/bower_components/angular/angular.js:10683:7
requestLoaded@http://localhost:8080/bower_components/angular/angular.js:10624:1
Madasu K
  • 1,813
  • 2
  • 38
  • 72

1 Answers1

0

Can you try without transforming the data from json to array. just return the json response and check.

The error is because the service is expecting a json object but after transformation array is being returned.