简体   繁体   English

Restangular getList()返回空数组

[英]Restangular getList() returns empty array

My factory that uses Restangular getList() method returns empty array despite getting records from database in spring controller which handles backend side of operation. 我的工厂使用Restangular getList()方法返回空数组,尽管在处理操作后端的spring控制器中从数据库获取记录。 I don't know if the cause of this problem is in java or angular side of application. 我不知道此问题的原因是在Java还是应用程序的角度方面。 Why is this happening? 为什么会这样呢?

FlightController.java FlightController.java

package regularmike.airline.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import regularmike.airline.entity.Flight;
import regularmike.airline.service.FlightService;

@RestController
@RequestMapping("/flight")
public class FlightController {

    @Autowired
    FlightService flightService;

    @RequestMapping(value = "/getFlights", method = RequestMethod.GET, produces = "application/json")
    public List<Flight> getAllFlights() {
        List<Flight> allFlights = flightService.getAllFlights();    

        return allFlights;

    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json")
    public Flight getSeats(@PathVariable long id) {
        Flight result = flightService.getByFlightid(id);

        return result;
    }

}

FlightFactory.js FlightFactory.js

(function() {
    'use strict'

    angular.module('airline').factory('FlightFactory', FlightFactory);

    function FlightFactory(Restangular) {
        function getAllFlights() {
            return Restangular.all('flight/getFlights').getList();
        }
        return {
            getAllFlights: getAllFlights
        };  
    }
})();

DashboardController.js DashboardController.js

(function() {
    'use strict'

    angular.module('airline').controller('DashboardController', ['FlightFactory', DashboardController]);

    function DashboardController(FlightFactory) {
        var dashCtrl = this;

        FlightFactory
        .getAllFlights()
        .then(getFlights)

        function getFlights(response)
        {
            dashCtrl.flights = response;
            console.log(dashCtrl.flights);
        }
    }

})();

config.js config.js

(function() {
    angular.module('airline').config(
            [ 'RestangularProvider', restangularConfig ]);

    function restangularConfig(RestangularProvider) {
        RestangularProvider.setBaseUrl('/');

        RestangularProvider.setDefaultHeaders(
                {"Accept": 'application/json'},
        {"Content-Type": "application/json+hal"});

        RestangularProvider.setResponseInterceptor(function (data, operation, what) {
            var resp;
            if (operation === 'get') {
                if (_.has(data, '_embedded')) {
                    resp = data._embedded;
                } else {
                    resp = data;
                }
                if (_.has(data, '_links')) {
                    resp._links = data._links;
                }
                return resp;
            }
            if (operation === 'getList') {
                if (_.has(data, '_embedded')) {
                    resp = data._embedded[what];
                } else {
                    resp = [];
                }
                if (_.has(data, '_links')) {
                    resp._links = data._links;
                }
                return resp;
            }
            return data;
        });

        RestangularProvider.setRequestInterceptor(function (element, operation, route, url) {
            if (operation === 'put' || operation === 'post') {
                if (angular.isDefined(element._links)) {
                    element._links.self.href = element._links.self.href.split('{?projection}')[0];
                }
                return element;
            }
        });

        RestangularProvider.setRestangularFields({
            selfLink: '_links.self.href'
        });
    }
})();

The return value of the method that returning the list should be bound to the web respond body, which is in your case JSON representation (Notice that @ResponseBody ). 返回列表的方法的返回值应绑定到Web响应正文,在您的情况下为JSON表示(请注意@ResponseBody )。

@RequestMapping(value = "/getFlights", method = RequestMethod.GET, produces = "application/json")
    public  @ResponseBody List<Flight> getAllFlights() {
        List<Flight> allFlights = flightService.getAllFlights();    
        return allFlights;   
    }

Also, to make it work, don't forget to set up json provider to the Spring side. 另外,要使其工作,请不要忘记在Spring端设置json provider。

<!-- Jackson JSON Mapper -->
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.7</version>
</dependency>

<!--jackson-core-asl -->
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.7</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM