【问题标题】:Angularjs, ui state routing to access spring data rest uri is going wrongAngularjs,ui状态路由访问spring数据rest uri出错了
【发布时间】:2015-10-02 05:30:54
【问题描述】:

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

在我的 Student.java 中,我有

@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);

在 Teacher.Java 中,我有

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

Angularjs中的ui路由状态:

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

我的学生工厂是:

.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

【问题讨论】:

    标签: angularjs spring-data-jpa spring-data-rest angular-ui-router angularjs-ng-resource


    【解决方案1】:

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

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

    【讨论】:

    • 我没有对 getAll 进行任何转换('getAll': { method:'GET' })。你能告诉我你想让我删除哪一行代码吗?
    • 在转换块中尝试对 angular.fromJson 进行一些更改
    • 尝试向 getAll 添加转换并返回 Json
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2016-02-15
    • 2017-10-05
    相关资源
    最近更新 更多