【问题标题】:Angular and rest web service causes: "Expected response to contain an object but got an array"Angular 和 REST Web 服务导致:“预期的响应包含一个对象但得到了一个数组”
【发布时间】:2015-11-01 18:20:23
【问题描述】:

我练习 Angular 和 Web 服务。我的目标是制作从 Web 服务获取数据的角度服务。与服务器建立连接,服务器返回一些数据。问题是我收到错误:

    Error: $resource:badcfg
Response does not match configured parameter
Error in resource configuration for action `featured`. Expected response to contain an object but got an array (Request: undefined products/featured)

我不知道我的错误到底在哪里,或者 $resource 实现错误,或者 Spring 控制器的制作方式不好?也许有人可以提出任何建议,让它发挥作用的最佳方法是什么?

我的代码:

WebService 控制器:

    @RestController
@RequestMapping("/products")
public class ProductManagementController {

    @Autowired
    ProductManagementService productService;

    @RequestMapping(value="/featured")
    public ResponseEntity<List<ProductModel>> getFeaturedProducts() {
        List<ProductModel> products = productService.getFeaturedProducts();
        if (products.isEmpty()) {
            return new ResponseEntity<List<ProductModel>>(HttpStatus.NO_CONTENT);
        }
        return new ResponseEntity<List<ProductModel>>(products, HttpStatus.OK);
    }

    @RequestMapping(value="/recommended")
    public ResponseEntity<List<ProductModel>> getRecommendedProducts(){
        List<ProductModel> products = productService.getRecommendedProducts();
        if(products.isEmpty()){
            return new ResponseEntity<List<ProductModel>>(HttpStatus.NO_CONTENT);
        }
        return new ResponseEntity<List<ProductModel>>(products,HttpStatus.OK);
    }

}

角度服务:

    (function() {
    'use strict';

    var mainApp = angular.module('mainApp');
    mainApp.factory('ProductService', [ '$resource', function($resource) {
        return $resource('products/:action/:sub', {}, {
            'featured' : {
                mothod : "GET",
                params : {
                    action : 'featured',
                    sub : ''
                }
            }
        });
    } ]);

    mainApp.controller('featuredItems', [ '$scope', 'ProductService',
            function($scope, ProductService) {
                ProductService.featured(function(responseData) {
                    debugger; //This breake point is not colled 
                });
            } ]);
})();

【问题讨论】:

  • 转到docs.angularjs.org/api/ngResource/service/$resource 并查找isArray。另外,它是method,而不是mothod
  • isArray 成功了吗?我没有在文档中抓住一点点。当我从服务器接收列表时总是需要这个参数?
  • 显然,是的。我从不使用 $resource。我更喜欢直接使用 $http。
  • hmm,$http$resource 有任何优势,因为正如我在一些解释对服务器的角度请求的来源中注意到的那样,对于休息服务,最好使用 $resource。
  • $resource 是 $http 之上的抽象层。由于 $resource 使用 $http,您可以使用 $http 完成 $resource 所做的一切。但我发现使用 $http 的代码更具可读性和直观性。这是一个见仁见智的问题,但那是我的。

标签: angularjs spring web-services rest resources


【解决方案1】:

根据文档,您希望 featured 操作类似于内置的 query 操作,其定义为 'query': {method:'GET', isArray:true}。在接收对象数组时,您总是必须这样做。

【讨论】:

    猜你喜欢
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 2015-03-11
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多