Java AngularJS-无web服务器的独立-调用CXF REST服务时出错
我在AngularJS中调用了一个cxf REST服务,如果我将AngularJS客户端部署为web应用程序,它就可以正常工作。但是,当我试图直接在web服务器外部访问html文件时,在从AngularJS调用CXF Rest服务时出现了问题 但是,如果我直接在浏览器上调用rest服务(GET),它可以正常工作。但不是在安格拉斯 我用下面的URL访问了浏览器中的html文件Java AngularJS-无web服务器的独立-调用CXF REST服务时出错,java,angularjs,rest,cxf,Java,Angularjs,Rest,Cxf,我在AngularJS中调用了一个cxf REST服务,如果我将AngularJS客户端部署为web应用程序,它就可以正常工作。但是,当我试图直接在web服务器外部访问html文件时,在从AngularJS调用CXF Rest服务时出现了问题 但是,如果我直接在浏览器上调用rest服务(GET),它可以正常工作。但不是在安格拉斯 我用下面的URL访问了浏览器中的html文件 file:///C:/angularjs/src/main/webapp/views/index.html 角度JS代码
file:///C:/angularjs/src/main/webapp/views/index.html
角度JS代码
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="../scripts/vendors/angular.js"></script>
<script type="text/javascript">
var jayApp = angular.module('jayApp', []);
jayApp.controller('jayController', function($scope, $http) {
$http.get("http://localhost:7001/myservice/services/entities").success(function(data) {
$scope.entities = data;
});
})
</script>
</head>
<body>
<div ng-app="jayApp" ng-controller="jayController">
<table>
<thead>
<tr>
<th>Entity Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="entity in entities">
<td>{{entity.entityName}}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
看起来您的服务器中没有启用CORS 为此,您需要在配置中包括cors过滤器
<beans>
<bean id="cors-filter" class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter"/>
<jaxrs:server id="service" address="/">
<jaxrs:serviceBeans>
<ref bean="cors-server" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="cors-filter" />
</jaxrs:providers>
</jaxrs:server>
<bean id="cors-server" scope="prototype"
class="org.apache.cxf.systest.jaxrs.cors.AnnotatedCorsServer" />
</beans>
如果您希望仅出于测试目的执行这些调用,那么还可以尝试在禁用web安全性的情况下运行浏览器实例。。。要执行此操作,请使用--禁用web安全性
标志启动chrome
顺便说一句,后端的错误看起来更像是客户端断开连接。它们与问题无关。非常有用。但是我在为org.apache.cxf.systest.jaxrs.cors.AnnotatedCorsServer添加依赖项时遇到了一个问题。您知道这个版本的兼容maven依赖项吗?我尝试了org.apache.cxf.systests cxf-systests jaxrs 3.1.2和org.apache.cxf-cxf-systests 2.2.3。所有这些都会产生错误。那些看起来不错。。。您会遇到哪种错误?我可以避免使用cxf systests工件并删除bean吗。我试着这样做,但这并没有添加所需的标题,仍然会出现错误。(原因:缺少CORS标头“访问控制允许原点”)。
<jaxrs:server id="mdmCxfServer" address="/">
<jaxrs:serviceBeans>
<ref bean="entityService" />
<ref bean="commonService" />
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="json" value="application/json" />
</jaxrs:extensionMappings>
<jaxrs:providers>
<bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
</jaxrs:providers>
<jaxrs:features>
<cxf:logging />
</jaxrs:features>
</jaxrs:server>
Error: Access is denied.
at Anonymous function (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:92:209)
at q (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:89:65)
at Anonymous function (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:86:214)
at Anonymous function (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:118:207)
at n.prototype.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:133:28)
at n.prototype.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:130:52)
at n.prototype.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:133:328)
at Anonymous function (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:19:477)
at e (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:39:87)
at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:19:398)
Error: [$compile:tpload] http://errors.angularjs.org/1.4.4/$compile/tpload?p0=partials%2Flist.html&p1=undefined&p2=undefined
at Anonymous function (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:144:42)
at Anonymous function (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:118:207)
at n.prototype.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:133:28)
at n.prototype.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:130:52)
at n.prototype.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:133:328)
at Anonymous function (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:19:477)
at e (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:39:87)
at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:19:398)
at yc (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js:20:172)
at Zd (https://aja
<beans>
<bean id="cors-filter" class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter"/>
<jaxrs:server id="service" address="/">
<jaxrs:serviceBeans>
<ref bean="cors-server" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="cors-filter" />
</jaxrs:providers>
</jaxrs:server>
<bean id="cors-server" scope="prototype"
class="org.apache.cxf.systest.jaxrs.cors.AnnotatedCorsServer" />
</beans>
@CrossOriginResourceSharing(allowOrigins = { "*" })