I started to learn how to implement Rest WS on my website, but I find it a little bit difficult. My idea is to start with a very simple example, and when I understand the basics, I’ll be able to understand more complex guides. Supposing we have a form with 2 textfields, we introduce 2 numbers and then the multiplication of those two numbers is shown. This is the code to accomplish that:
class CalculatorController {
def index = { }
def calc = {
def nr_1 = params.first_nr
def nr_2 = params.second_nr
def result
def erro = 'no'
if(nr_1.isInteger() && nr_2.isInteger())
result = nr_1.toInteger() * nr_2.toInteger()
else
erro = 'yes'
chain(action:"print_result", model:[erro: erro, result: result, nr1: nr_1, nr2: nr_2])
}
def print_result = {
if(chainModel.erro.equals('yes'))
[sms : 'Please introduce only 2 numbers!']
else
[sms: 'The result of the multiplication of ' + chainModel.nr1 + ' with ' + chainModel.nr2 + ' is ' + chainModel.result]
}
}
Main View:
<html>
<head>
<title></title>
<meta name="layout" content="main" />
<style type="text/css" media="screen">
</style>
</head>
<body>
This program is a calculator:<br><br>
<g:form name="myForm" action="calc">
<h1>Introduce first number: </h1><g:textField name="first_nr" value="${myValue}" /> <br>
<h1>Introduce second number: </h1><g:textField name="second_nr" value="${myValue}" /> <br>
<g:submitButton name="update" value="Update" />
</g:form>
</body>
</html>
Result view:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample title</title>
</head>
<body>
<h1>${sms}</h1>
</body>
</html>
I need help for:
– handle both http and rest request (Grails will know which one is being requested)
– create a new class to send the request (two numbers as input, the result as output).
PS. Sorry if this is too basic, but I wouldn’t really ask such thing if I could find such basic information on the web.
Thanks in advance,
PP
To route the REST request to your controller, you’d want to modify your UrlMappings.groovy file to handle requests following a syntax like what you’d want to send. A REST request to multiple two numbers might look like
example.com/multiply/5/6and you’d get 30 back as the result. To have Grails send the request for that to the calc method of your CalculatorController, you’d add a line like this to your UrlMappings.groovy:Or if you wanted to support other operations, like subtract, add, etc, you’d want to create methods with the name for each of those and then put $action in place of multiply in the mapping, like this:
Or even move the controller into the URL for the ultimate in extensibility:
This will route your request as you’d want to the appropriate action with the appropriate parameters being populated for your controller method. You also might want to use the withFormat closure in your controller to send back the result in a variety of different formats (XML, JSON, HTML) based upon the requested content-type (see the grails docs for more on withFormat usage).
I’m not sure what you mean about a class to send the request. An app can call this REST service just like any other REST service would be called. Or are you looking for an example of doing that? If so, look at the REST client facilities plug-in for Grails. You also might find this blog entry useful about making REST controllers and calls with Grails.
UPDATE
To access the REST service using Groovy, try using the RESTClient extension of HTTPBuilder (get it at
http://groovy.codehaus.org/modules/http-builder/doc/rest.html) Then you can make a call to the service like this:You can also find more information about groovy and REST on another stackoverflow question.