0

我有我的 spring data jpa 资源和存储库如下所示:

在我的 Student.java 中,我有

@ManyToOne
private Teacher teacher;

学生资源.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);

在 Teacher.Java 中,我有

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

在 Angularjs 中,ui 路由器状态:

学生.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});
                    }]
                }
            })

我有我的学生工厂:

.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' }
        });
    });

所以从teacher.html,我有每个老师的链接:

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

因此,当我单击特定教师的“获取我的学生”链接时,我希望得到该教师的所有学生,但我收到以下错误。任何人都可以帮助我解决此错误。

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
4

1 回答 1

0

您可以尝试不将数据从 json 转换为数组吗?只需返回 json 响应并检查。

该错误是因为服务需要一个 json 对象,但在返回转换数组之后。

于 2015-10-02T07:03:47.203 回答