Reputation: 1120
I practicing angular and web services. My goal to make angular service which gets data from web service. Connection to server is made and server returns some data. Problem is that I receive error:
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)
I don't know exactly where is my mistake, or $resource is implemented wrong, or Spring controller made in bad way? Maybe someone can give any suggestions, what is the best way to make it work?
my code:
WebService controller:
@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);
}
}
Angular service:
(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
});
} ]);
})();
Upvotes: 0
Views: 62
Reputation: 948
What you want to resemble with your featured
action is the in-built query
action which definition is 'query': {method:'GET', isArray:true}
according to the documentation. You always have to do this when receiving arrays of objects.
Upvotes: 1