Grails(14)Study the Project

Grails(14)Study the Project

1. Reverse the ER from database
Use mysql workbench ----> Database ----> Reverse engineering



Follow the wizard and connect to my local database.



Select the Schemata to be Processed.



I got a nice ER diagram, but it seems not useful.



2. Understand the Configuration
In the controller, it calls

def config = ConfigurationHolder.config

String url = config.sillycat.resturl.base + "dashboard/visitsPerLocation"



In the Config.groovy file, it will be configured like this:

grails.config.locations = [ "file:/opt/conf/SillycatConfig.groovy" ]



From my understanding, we can put list here, and we also can place our configuration in Config.groovy.



3. Implement the JSON REST API


Basic authorization
String authorizationString = "Basic " + 'username:password'.bytes.encodeBase64().toString()
connection.setRequestProperty("authorization",authorizationString)

                   

OAUTH2


Token Implementation


Shiro


I will forget about the authority and try to implement the functions first and then take care about the authority.



Here are my mapping files

// Calls to the Web Services
"/location/$id?" {
     format = "json"
     controller = "store"
     action = [POST:"create", GET:"apiGet"]
}



It will go to find a controller named StoreController the method apiGet with the GET method from HTTP.


The controller implementation will be like this:
def apiGet = {
     def storeInstance = params.id ? Store.get(params.id) : null
     if (!storeInstance) {
          Map map = [errorCode:101, errorMessage: "Can not find the location with id =" + params.id ]
          render map as JSON
     } else {
          render storeInstance as JSON
     }
}



And the unit test class will be looking like this:

class StoreControllerTests extends ControllerUnitTestCase {

    
@Test
public void testGetSuccess() {
     List<Store> stores = [
          new Store(storeCode:"TS1",storeName:"Test Store 1",enabled:true),
          new Store(storeCode:"TS2",storeName:"Test Store 2",enabled:true),
          new Store(storeCode:"TS3",storeName:"Test Store 3",enabled:false)]
          mockDomain(Store, stores)


          def controller = new StoreController()
          controller.params.id = 1
          controller.apiGet()


          assertEquals(controller.response.status, 200)
          //System.out.println(controller.response.status)
          //System.out.println(controller.response.contentAsString)
     }
}


And once the server is on, we can test like this:
@Test
public void testGetSuccessswithServer(){
     def client = new RESTClient("http://localhost:8080")
     def response
     response = client.get(path: "person/1")

     System.out.println(response.status)

     System.out.println(response.data)

}



References:
http://www.intelligrape.com/blog/2011/11/16/writing-json-apis-part-i-creating-a-secure-rest-json-api-with-grails-and-spring-security-in-3-easy-steps/

https://github.com/svivekkrishna/Json-API-Sample

http://grails.org/doc/latest/ref/Controllers/allowedMethods.html



http://www.intelligrape.com/blog/2011/12/29/writing-json-apis-part-ii-creating-json-named-configs-to-control-what-you-render/



https://github.com/padcom/grails-json-rest-api-examples



http://www.intelligrape.com/blog/2010/04/28/working-with-rest-call/



http://www.ibm.com/developerworks/cn/opensource/os-cn-shiro/index.html



https://github.com/springside/springside4/wiki/Reference



http://oauth.net/



https://github.com/SpringSource/spring-security-oauth/wiki/oAuth2



https://github.com/adaptivecomputing/grails-spring-security-oauth2-provider



http://grails.org/plugin/spring-security-oauth2-provider



http://grails.org/doc/latest/guide/theWebLayer.html#moreOnJSONBuilder



http://www.ibm.com/developerworks/java/library/j-grails10209/index.html



http://jwicz.wordpress.com/2011/07/11/grails-custom-xml-marshaller/

你可能感兴趣的:(project)