Homology policy means that the protocol, host, and Port are the same. We can deploy 2 different ports of Tomcat on the local machine so that one Tomcat application accesses the rest service under another tomcat through Ajax, and there is a cross-domain access problem. Modify the three port numbers below Conf/server.xml:
<server port= "* * *" shutdown= "shutdown" > <connector port= "
* * * * *
protocol=" http/1.1 " connectiontimeout= "20000"
redirectport= "8443"/> <connector port= "
* * *" protocol= "ajp/1.3" redirectport= "8443"/>
On the basis of a previous blog, in order to solve CXF cross-domain problems, you need to:
1. Modify pom file to increase providers
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId> cxf-rt-rs-extension-providers</artifactid>
<version>${cxf.version}</version>
</ Dependency>
2. Modify Cxf-spring.xml Configuration Jsonp Interceptor
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs= "Http://cxf.apache.org/jaxrs" xsi:schemalocation= "
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd "> <jaxrs:server address="/rest "> < jaxrs:servicebeans> <ref bean= "Nameserviceimpl"/> </jaxrs:serviceBeans> <!--jsonp--> < jaxrs:providers> <bean class= "Org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor"/> </ jaxrs:providers> <jaxrs:inInterceptors> <bean class= " Org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor "/> </jaxrs:inInterceptors> <jaxrs:o utinterceptors> <bean class= "Org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor"/> </ Jaxrs:outintercepTors> </jaxrs:server> </beans>
3. Access via Ajax under another tomcat
$.ajax ({
type: ' Get ',
URL: ' http://127.0.0.1:8080/aty-rest/rest/rest/welcome ',
dataType: ' Jsonp ',
jsonp: ' _jsonp ',
jsonpcallback: ' Callback ',
success:function (data) {
alert (json.stringify (data)) ;
}
});
This article is a reference to the Web Service (4)--A good primer for using CXF to develop rest services.