Java 带Restful路由和AngularJS路由的SpringMVC4.0应用程序的问题
我有一个SpringMVC4.0AngularJS应用程序,在同一个实例上运行时,Restful接口路由和AngularJS路由之间似乎存在冲突。它会导致404个错误。我认为我的应用程序设置不正确 该应用程序被捆绑到UserManager.war文件中,并复制到独立运行的Tomcat 7.0实例(未嵌入IDE)上的webapps目录中 应用程序在以下位置运行:Java 带Restful路由和AngularJS路由的SpringMVC4.0应用程序的问题,java,angularjs,rest,spring-mvc,tomcat7,Java,Angularjs,Rest,Spring Mvc,Tomcat7,我有一个SpringMVC4.0AngularJS应用程序,在同一个实例上运行时,Restful接口路由和AngularJS路由之间似乎存在冲突。它会导致404个错误。我认为我的应用程序设置不正确 该应用程序被捆绑到UserManager.war文件中,并复制到独立运行的Tomcat 7.0实例(未嵌入IDE)上的webapps目录中 应用程序在以下位置运行: http://localhost:8080/UserManager/main http://localhost:8080/UserMan
http://localhost:8080/UserManager/main
http://localhost:8080/UserManager/update
http://localhost:8080/UserManager/insert
restful接口指定以下URL:
http://localhost:8080/UserManager/services/users/list
http://localhost:8080/UserManager/services/users/{id}
http://localhost:8080/UserManager/services/users/insert
http://localhost:8080/UserManager/services/users/update/{id}
http://localhost:8080/UserManager/services/users/delete/{id}
这是我的web.xml:
控制器在controller.js文件中设置,对我的restful Web服务的调用在services.js文件中执行
services.js:
我可以让Web服务正常工作,但在应用程序上出现404错误。如果我调整我的web.xml,我可以启动应用程序,但随后对web服务的调用失败,出现404错误。尝试将您的var更改为:
url = '/{webapp-name}/services/users/insert'
尝试使用rest测试工具,如:
检查RESTful端点是否可访问并返回Mime类型application/json
此外,出于调试目的,它可能有助于放置错误处理块,如:
$http.get(url)
.then(function (response) {
return response.data;
}).error(function () {
// error happened
});
还要检查浏览器的控制台和服务器的日志文件,以查看是否显示任何错误消息
另外,既然您正在处理RESTful端点,为什么不试试$resource而不是$http,如示例中所示:
服务:
services.factory('ProductService', ['$resource',
function($resource){
return $resource('/mywebapp/rest/product/:id',
{
id: "@id"
}
);
}]);
控制器:
controllers.controller('ProductListCtrl', ['$scope', 'ProductService',
function($scope, ProductService) {
ProductService.query(function(data) {
$scope.products = data;
$scope.productListNotEmpty = ($scope.products.length > 0);
});
}]);
您可以发布调用webservice的服务定义吗?为什么不使用$resource而不是$http?由于您处理的是REST endpointsIm,所以除了我查看的各种代码示例之外,我不确定使用的是$http,而不是$resource。@MLefebvre-您找到解决方案了吗?即使我也面临同样的问题。上面的代码适用于Web服务,但ANgularJS路由不适用于404。如果我将web.xml文件从/*更改为任何其他路径,则AngularJS路由可以工作,但RESTService得到404。返回的错误是404错误。当我尝试直接使用SoapUI访问restful服务时,我也得到了404。另外,在浏览器中,如果我直接点击它,我会得到一个404错误。我猜您的Web服务路径可能是错误的,您确定您的REST URL是可访问的,并且返回应用程序/json Mime类型吗?检查我的更新应答进行了更改,Web服务仍然运行良好,返回JSON值,状态为200,但应用程序路由仍然不工作,并给出404。目前不工作。我也试过了,但运气不好。春天的网址对你有用吗?angular试图访问的URL是什么?您可以在网络选项卡下进行验证吗?如上所述:应用程序在运行,restful接口指定以下URL:{id}{id}{id}
(function () {
var userService = function ($http, $window, $q, $log, $rootScope) {
var factory = {};
factory.createUser = function (userId, last, first, sex, age, ethnicity, rank, dob) {
var url = '/UserManager/services/users/insert';
var parameters = {
"userId" : userId,
"last" : last,
"first" : first,
"sex" : sex,
"age" : age,
"ethnicity" : ethnicity,
"rank" : rank,
"dob" : dob
};
return $http.put(url, parameters)
.then(function (data, status) {
return data;
});
};
factory.deleteUser = function(userId) {
var url = '/UserManager/services/users/delete/' + userId;
return $http.delete(url)
.then(function (data, status) {
return data;
});
};
factory.updateUser = function (userId, last, first, sex, age, ethnicity, rank, dob) {
var url = '/UserManager/services/users/update/' + userId;
var parameters = {
"userId" : userId,
"last" : last,
"first" : first,
"sex" : sex,
"age" : age,
"ethnicity" : ethnicity,
"rank" : rank,
"dob" : dob
};
return $http.post(url, parameters)
.then(function (data, status) {
return data;
});
};
factory.getUsers = function () {
var url = '/UserManager/services/users/list';
return $http.get(url)
.then(function (response) {
return response.data;
});
};
factory.getUser = function (userId) {
var url = '/UserManager/services/users/' + userId;
return $http.get(url)
.then(function (response) {
return response.data;
});
};
return factory;
}
var app = angular.module("PMApp.services", ['ngResource']);
app.factory('userService', userService);
}());
url = '/{webapp-name}/services/users/insert'
$http.get(url)
.then(function (response) {
return response.data;
}).error(function () {
// error happened
});
services.factory('ProductService', ['$resource',
function($resource){
return $resource('/mywebapp/rest/product/:id',
{
id: "@id"
}
);
}]);
controllers.controller('ProductListCtrl', ['$scope', 'ProductService',
function($scope, ProductService) {
ProductService.query(function(data) {
$scope.products = data;
$scope.productListNotEmpty = ($scope.products.length > 0);
});
}]);