0
我正在练习角度和Web服务。我的目标是使角度服务从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)
我并不确切哪里是我的错误,或$资源实现错了,还是春控制器坏的方式取得知道吗?也许有人可以提出任何建议,让它工作的最好方法是什么?
我的代码:
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
});
} ]);
})();
转到https://docs.angularjs.org/api/ngResource/service/$resource并查找“isArray”。此外,它是'方法',而不是'方法'。 –
'isArray'做到了诀窍吗?我没有在文档中找到一点点。当我从服务器收到List时,总是需要这个参数? – Edgar
显然,是的。我从不使用$资源。我更喜欢直接使用$ http。 –