Currently I'm working on grails 2.4.3 with GGTS 3.6.0
Requirement - How a grails web service work.
Existing - Currently my closure is working for me as a web service but output is format specific(JSON or XML at a time).
Problem - In closure(web service), how I would be able to return JSON/XML and other format.
Closure code -
def able_Webservice = {
ableService.populateAbleBean(ableBean);
ableService.settingWhereClause(ableBean);
ableService.getDBData(ableBean);
def jsonData = ableService.webservice_Data(ableBean);
render jsonData as JSON
}
Grails has withFormat feature. You can render different responses based on the incoming request Accept header, format parameter or URI extension.
Your code would like:
def able_Webservice = {
ableService.populateAbleBean(ableBean);
ableService.settingWhereClause(ableBean);
ableService.getDBData(ableBean);
def data = ableService.webservice_Data(ableBean);
withFormat {
xml { render data as XML }
json { render data as JSON }
}
}
This uses built-in content negotiation.