我正在使用Restful webservices练习AngularJs。我使用的是Spring 4.2.4 对我来说,获取所有记录和删除记录都有效。但是如果我想插入一些数据,则会抛出404 Not Found。
的错误错误是
POST http://localhost:8080/admin/saveCategory 404 (Not Found)
这是我的AngularJs服务和控制器
Service.js
App.factory('categoryService',['$http','$q',function($http,$q){
return {
fetchAllCategory: function(){
return $http.get('http://localhost:8080/admin/getCategories').then(
function(response){
console.log(response.data)
return response.data;
}, function(errResponse){
console.error('Error while fetching users');
return $q.reject(errResponse);
}
);
},
addCategory: function(category){
if(category == null){
console.log("Category is null");
return $q.reject("Category is null");
}else{
return $http.post('http://localhost:8080/admin/saveCategory',category).then(
function(response){
return response.data;
}, function(errResponse){
return $q.reject(errResponse);
}
);
}
},
deleteCategory: function(categoryName){
return $http.get('http://localhost:8080/admin/deleteCategory/'+categoryName).then(
function(response){
return response.data;
}, function(errResponse){
return $q.reject(errResponse);
}
);
}
};
}]);
Controller.js
App.controller('categoryController',['$scope','categoryService', function($scope,categoryService){
var self=this;
$scope.category={catId:null,catName:''};
self.categories=[];
self.getAllCategories=function(){
categoryService.fetchAllCategory().then(
function(categories){
self.categories=categories;
},
function(errResponse){
console.error('Error while fetching Categories');
}
);
}
self.addCategory= function(){
categoryService.addCategory($scope.category).then(
self.getAllCategories,
self.reset(),
function(errResponse){
console.error('Error while Adding Category.');
}
);
}
self.updateCategory = function(categoryName){
for(var i = 0; i < self.categories.length; i++){
if(self.categories[i].catName == categoryName) {
$scope.category.catId = self.categories[i].catId;
$scope.category.catName = self.categories[i].catName;
break;
}
}
};
self.getAllCategories();
self.submit= function(){
self.addCategory(self.category);
self.reset();
}
self.deleteCategoryCtr=function(categoryName){
categoryService.deleteCategory(categoryName).then(
self.getAllCategories,
function(errResponse){
console.error('Error while deleting User.');
}
);
}
self.reset = function(){
$scope.category={catId:null,catName:''};
};
}]);
我的休息控制器
@RestController
@RequestMapping(value="/admin")
public class CategoryController {
@Autowired
private CategoryService categoryService;
@RequestMapping(value = "/saveCategory",method = RequestMethod.POST)
public ResponseEntity<Void> saveCategory(@RequestBody CategoryHelper categoryHelper){
if(categoryHelper.getCatId() == 0){
categoryService.addCategory(convertToCategory(categoryHelper));
}else{
categoryService.updateCategory(convertToCategory(categoryHelper));
}
return new ResponseEntity<Void>(HttpStatus.OK);
}
@RequestMapping(value = "/deleteCategory/{catName}")
public ResponseEntity<Void> deleteCategory(@PathVariable("catName") String catName){
categoryService.deleteCategory(catName);
return new ResponseEntity<Void>(HttpStatus.OK);
}
@RequestMapping(value = "/getCategories")
public ResponseEntity<List<Category>> getAllCategories() {
List<Category> categories = categoryService.getCategories();
if (categories == null || categories.isEmpty()) {
return new ResponseEntity<List<Category>>(HttpStatus.NOT_FOUND);
} else {
return new ResponseEntity<List<Category>>(categories,HttpStatus.OK);
}
}
private Category convertToCategory(CategoryHelper categoryHelper) {
Category category = new Category();
category.setCatId(categoryHelper.getCatId());
category.setCatName(categoryHelper.getCatName());
return category;
}
}
我也写了一个插入控制器的方法,如下所示也没有用。
@RequestMapping(value = "/saveCategory",method = RequestMethod.POST, consumes = "application/json")
public ResponseEntity<Void> saveCategory(@RequestBody String jsonObject){
ObjectMapper objectMapper = new ObjectMapper();
try {
categoryHelper = objectMapper.readValue(categoryJson,CategoryHelper.class);
} catch (IOException e) {
e.printStackTrace();
}
if(categoryHelper.getCatId() == 0){
categoryService.addCategory(convertToCategory(categoryHelper));
}else{
categoryService.updateCategory(convertToCategory(categoryHelper));
}
return new ResponseEntity<Void>(HttpStatus.OK);
}
类别助手类
public class CategoryHelper{
private int catId;
private String catName;
public int getCatId() {
return catId;
}
public void setCatId(int catId) {
this.catId = catId;
}
public String getCatName() {
return catName;
}
public void setCatName(String catName) {
this.catName = catName;
}
}
在Rest Web Service Client中,它就像邮递员一样,这就是它显示的内容
Request:
POST http://localhost:8080/admin/saveCategory
Accept: application/json
Content-Type: application/json
{"catId":null,"catName":"Black Berry"}
回应(Summery)
404, Not Found
Pragma: no-cache
Date: Mon, 14 Mar 2016 07:33:23 GMT
X-Content-Type-Options: nosniff
Server: Apache-Coyote/1.1
X-Frame-Options: DENY
Content-Language: en
Content-Type: text/html;charset=ISO-8859-1
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Length: 992
X-XSS-Protection: 1; mode=block
Expires: 0
Time taken (in milliseconds): 38