Create a restful app with AngularJS/Grails(4)

Standalone AngularJS application

In the real world applications, it is usually required to integrate the third party APIs into the project. Most of case, the third party service is provided as flexible REST APIs for developers.

Imagine the REST API in this sample could sever other applications, such as a mobile app or other website in future, it is ideally designated as a API centric application.

The AngularJS codes can be moved to a standalone application, and consume the REST web service remotely, like other applications.

Split the original codes into two projects.

/
    /client
    /server

The client is an AngularJS based application, I reused the AngularJS Seed sandbox, and copy the app folder in the former Grails application.

The server is nearly no difference from the original Grails application, but the app folder in the web-app folder is moved to client. Additionally, you have to configure CORS in the server side.

Configure CORS

There is a good post from the Spring community to help you understand CORS(Cross-Origin Resource Sharing).

https://spring.io/understanding/cors

The post provides a solution to resolve the limitation of cross domain resource accessing.

But I would like use a CORS filter from Ebay, I have used it in projects and it has some configuration options.

By default, the Grails application does not includes a web.xml file. Use the following command to install the templates for this projects.

grails install-templates

Open the web.xml file under the src/templates/war folder.

Add the configuration of CORS filter.

 
    
 
  
 
  
    CORS Filter 
  
    
 
  
 
  
    org.ebaysf.web.cors.CORSFilter 
  
    
 
  
 
   
   
     A comma separated list of allowed origins. Note: An '*' cannot be used for an allowed origin when using credentials. 
    
   
     cors.allowed.origins 
    
   
     http://localhost:8000, http://localhost:8080 
    
  
    
 
  
 
   
   
     A comma separated list of HTTP verbs, using which a CORS request can be made. 
    
   
     cors.allowed.methods 
    
   
     GET,POST,HEAD,OPTIONS,PUT,DELETE 
    
  
    
 
  
 
   
   
     A comma separated list of allowed headers when making a non simple CORS request. 
    
   
     cors.allowed.headers 
    
   
     Content-Type,X-Requested-With,Authorization,Origin,Access-Control-Request-Method,Access-Control-Request-Headers 
    
  
    
 
  
 
   
   
     A comma separated list non-standard response headers that will be exposed to XHR2 object. 
    
   
     cors.exposed.headers 
    
    
  
    
 
  
 
   
   
     A flag that suggests if CORS is supported with cookies 
    
   
     cors.support.credentials 
    
   
     true 
    
  
    
 
  
 
   
   
     A flag to control logging 
    
   
     cors.logging.enabled 
    
   
     true 
    
  
    
 
  
 
   
   
     Indicates how long (in seconds) the results of a preflight request can be cached in a preflight result cache. 
    
   
     cors.preflight.maxage 
    
   
     10 
    
  


 

 


 

 
    
 
  
 
  
    CORS Filter 
  
    
 
  
 
  
    /* 
  


 

 

Run the server project as a Grails application from command line or IDE.

And then run the client project as a NodeJS application.

node scripts\web-server.js

Navigate to http://localhost:8000/app/index.html#/.

Sample codes

The code is hosted on https://github.com/hantsy/angularjs-grails-sample/.

你可能感兴趣的:(spring,AngularJS,REST,grails)