Restangular getList()返回空数组

时间:2016-11-28 19:00:53

标签: javascript java angularjs spring

我的工厂使用Restangular getList()方法返回空数组,尽管从Spring数据库中获取记录来处理后端操作。我不知道这个问题的原因是在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

(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

(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

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

1 个答案:

答案 0 :(得分:0)

返回列表的方法的返回值应该绑定到Web响应主体,在您的情况下是JSON表示(注意 @ResponseBody )。

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

另外,为了使其工作,不要忘记将json提供程序设置为Spring端。

<!-- 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>