0

我有如下所示我的春天JPA的數據資源和存儲庫:Angularjs,UI狀態路由訪問數據春天休息URI是怎麼了?

在我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: { 
        '[email protected]': { 
         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 
[email protected]://localhost:8080/bower_components/angular/angular.js:14634:28 
scheduleProcessQueue/<@http://localhost:8080/bower_components/angular/angular.js:14650:27 
$RootScopeProvider/this.$get</[email protected]://localhost:8080/bower_components/angular/angular.js:15916:16 
$RootScopeProvider/this.$get</[email protected]://localhost:8080/bower_components/angular/angular.js:15727:15 
$RootScopeProvider/this.$get</[email protected]://localhost:8080/bower_components/angular/angular.js:16024:13 
[email protected]://localhost:8080/bower_components/angular/angular.js:10511:36 
[email protected]://localhost:8080/bower_components/angular/angular.js:10683:7 
[email protected]://localhost:8080/bower_components/angular/angular.js:10624:1 

回答

0

您可以嘗試不將數據從json轉換爲數組。只需返回json響應並檢查。

錯誤是因爲服務期待json對象,但在轉換數組被返回之後。

+0

我沒有對getAll進行任何轉換,('getAll':{method:'GET'})。你能告訴我你想要我刪除哪一行代碼嗎? –

+0

在變換塊中嘗試在angular.fromJson上進行一些更改 –

+0

嘗試向getAll添加變換並返回Json –