Grails(14)Study the Project

本文介绍了使用 Grails 框架实现 RESTful JSON API 的过程,包括从数据库逆向生成 ER 图、配置文件的理解与使用、基本授权及 OAUTH2 的 Token 实现方式,并通过 StoreController 的例子展示了如何进行单元测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值