我在AngularJS中调用了一个cxf REST服务,如果我将AngularJS客户端部署为Web应用程序,它可以正常工作。但是当我尝试直接访问Web服务器外部的html文件时,从AngularJS调用CXF Rest服务时出现问题。
但是,如果我直接在浏览器上调用,则其余服务(GET)可以正常工作。但不是在AngularJS中。
我在浏览器中使用以下网址
访问了html文件file:///C:/angularjs/src/main/webapp/views/index.html
Angular 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>
错误
Sep 08, 2015 11:47:02 AM org.apache.cxf.jaxrs.utils.JAXRSUtils logMessageHandlerProblem
SEVERE: Problem with writing the data, class java.util.ArrayList, ContentType: application/json
<Sep 8, 2015 11:47:02 AM BST> <Error> <org.apache.cxf.jaxrs.utils.JAXRSUtils> <BEA-000000> <Problem with writing the data, class java.util.ArrayList,
ContentType: application/json>
Sep 08, 2015 11:47:02 AM org.apache.cxf.phase.PhaseInterceptorChain doDefaultLogging
WARNING: Interceptor for {http://impl.service.abc.com/}EntityServiceImpl has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: An established connection was aborted by the software in your host machine
at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.handleWriteException(JAXRSOutInterceptor.java:371)
at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.serializeMessage(JAXRSOutInterceptor.java:272)
at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.processResponse(JAXRSOutInterceptor.java:118)
at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.handleMessage(JAXRSOutInterceptor.java:81)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:83)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:251)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:171)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:293)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:217)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:268)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:346)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:243)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3432)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3402)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2285)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2201)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1572)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:255)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:263)
Caused by: java.io.IOException: An established connection was aborted by the software in your host machine
at sun.nio.ch.SocketDispatcher.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
at sun.nio.ch.IOUtil.write(IOUtil.java:65)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
at weblogic.socket.NIOSocketMuxer$NIOOutputStream.writeInternal(NIOSocketMuxer.java:1064)
at weblogic.socket.NIOSocketMuxer$NIOOutputStream.write(NIOSocketMuxer.java:983)
at weblogic.servlet.internal.ChunkOutput.writeChunkTransfer(ChunkOutput.java:622)
at weblogic.servlet.internal.ChunkOutput.writeChunks(ChunkOutput.java:587)
at weblogic.servlet.internal.ChunkOutput.flush(ChunkOutput.java:472)
at weblogic.servlet.internal.ChunkOutput$3.checkForFlush(ChunkOutput.java:727)
at weblogic.servlet.internal.ChunkOutput.write(ChunkOutput.java:378)
at weblogic.servlet.internal.ChunkOutputWrapper.write(ChunkOutputWrapper.java:153)
at weblogic.servlet.internal.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:164)
at org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:51)
at com.fasterxml.jackson.core.json.UTF8JsonGenerator._flushBuffer(UTF8JsonGenerator.java:2003)
at com.fasterxml.jackson.core.json.UTF8JsonGenerator.writeString(UTF8JsonGenerator.java:436)
at com.fasterxml.jackson.databind.ser.std.SqlDateSerializer.serialize(SqlDateSerializer.java:51)
at com.fasterxml.jackson.databind.ser.std.SqlDateSerializer.serialize(SqlDateSerializer.java:18)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:656)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:111)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:24)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:253)
at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1384)
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:889)
at com.fasterxml.jackson.jaxrs.base.ProviderBase.writeTo(ProviderBase.java:635)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.writeMessageBody(JAXRSUtils.java:1372)
at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.serializeMessage(JAXRSOutInterceptor.java:250)
... 32 more
<Sep 8, 2015 11:47:02 AM BST> <Warning> <org.apache.cxf.phase.PhaseInterceptorChain> <BEA-000000> <Interceptor for {http://impl.service.abc.com/}EntityServiceImpl has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: An established connection was aborted by the software in your host machine
at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.handleWriteException(JAXRSOutInterceptor.java:371)
at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.serializeMessage(JAXRSOutInterceptor.java:272)
at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.processResponse(JAXRSOutInterceptor.java:118)
at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.handleMessage(JAXRSOutInterceptor.java:81)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
Truncated. see log file for complete stacktrace
Caused By: java.io.IOException: An established connection was aborted by the software in your host machine
at sun.nio.ch.SocketDispatcher.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
at sun.nio.ch.IOUtil.write(IOUtil.java:65)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
Truncated. see log file for complete stacktrace
>
CXF Spring配置
<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>
浏览器日志(F12)
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
答案 0 :(得分:2)
您的服务器似乎未启用CORS。
为此,您需要在配置中包含cors-filter。
<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
注释您的端点,如下所示:
@CrossOriginResourceSharing(allowOrigins = { "*" })
如果您只是出于测试目的而进行这些调用,那么您也可以尝试在禁用Web安全的情况下运行浏览器实例...为此,请使用--disable-web-security
标志启动chrome
顺便说一句。后端的错误看起来更像是客户端的错误。它们与问题无关。