CXF Publishing restful webservice that support Ajax cross-domain access

Source: Internet
Author: User

Built with Apache CXF a toy-type restful webservice, built-in jetty, plus Gradle, released with unparalleled ease.

apply plugin:  ' Java ' apply plugin:  ' application ' repositories {maven {url  ' Http://maven.oschina.net/content/groups/public "}}[compilejava,compiletestjava,javadoc]*.options*.encoding  =  "UTF-8" dependencies {compile  ' commons-lang:commons-lang:2.6 ' compile  ' javax.ws.rs: javax.ws.rs-api:2.0.1 ' compile  ' org.apache.cxf:cxf-rt-frontend-jaxrs:3.1.2 ' compile  ' ORG.APACHE.CXF: cxf-rt-transports-http-jetty:3.1.2 ' compile  ' org.apache.cxf:cxf-rt-rs-security-cors:3.1.3 ' compile  ' com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.6.1 ' compile  ' org.hsqldb:hsqldb:2.3.3 ' compile   ' mysql:mysql-connector-java:5.1.35 ' compile  ' org.hibernate:hibernate:3.5.4-final ' compile  ' Org.hibernate:hibernate-tools:4.3.1.final ' compile  ' commons-httpclient:commons-httpclient:3.1 ' testCompile  group:  ' JUnit ', name:  ' junit ', version:  ' 4.+ '}task  ' create-dirs '  < < {   soUrcesets*.java.srcdirs*.each { it.mkdirs ()  }   sourcesets*.resources.srcdirs*. Each { it.mkdirs ()  }}mainClassName =  ' Cn.edu.hdu.grs.tdlab.Server ' jar {version  =  "0.1.20150908-snapshot"}

Then, in order to build some Web pages on this basis, just the recent learning of the ANGULARJS to move out of use.

There is a problem here, my ANGULARJS application is deployed on Apache httpd, so it creates an Ajax cross-domain access problem. There are two ways to solve this: JSONP and cors, which I prefer to cors.

So is the CXF framework, adding the response header, the most straightforward idea is to implement a outinterceptor. But inadvertently found that CXF has given the solution: Crossoriginresourcesharingfilter. But most Chinese blogs only mention the need to configure filter to provider, and then there is no. (again, can you be more reliable?) )。 Tried, did not succeed, so decisive officer net to find information, the official website gave an example,crossoriginresourcesharing annotation to the place to need, and then the filter configuration to the server, suddenly dawned.

The interface is as follows:

@CrossOriginResourceSharing (        allowOrigins = {             "http://127.0.0.1"          },         allowcredentials = true,          maxage = 1) @Path (value= "/demand") @Produces (" Application/json ") @Consumes (" Application/json ") public interface demandservice {@GET @path (value= "/{id}") Public demand get (@PathParam ("id") int id), @GET @path (value= "/{id}/expert/") public  List getrecommendexpert (@PathParam ("id") int id), @POST @path (value= "/") Public map create ( Demand demand, @HeaderParam ("token") final string token), @GET @path (value= "/") public list  list (@QueryParam ("U") integer uid,  @QueryParam ("s") Int start, @QueryParam ("L") int  limit); @DELETE @path (value= "/{iD} ") Public demand delete (@PathParam (" id ") int id);} 

The service-side section code is as follows:

Jaxrsserverfactorybean Factorybean = new Jaxrsserverfactorybean (); Factorybean.setresourceclasses ( Demandserviceimpl.class); Jacksonjaxbjsonprovider Jsonprovider = new Jacksonjaxbjsonprovider (); Crossoriginresourcesharingfilter crossfilter = new Crossoriginresourcesharingfilter (); FactoryBean.setProviders (( Arrays.aslist (Crossfilter,jsonprovider));


Resources:

"1" jax-rs:cors Project official website http://cxf.apache.org/docs/jax-rs-cors.html

"2" uses CORS to solve AJAX cross-domain issues http://www.open-open.com/lib/view/open1423107683623.html

CXF Publishing restful webservice that support Ajax cross-domain access

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.