Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6798421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:43:40+00:00 2026-05-26T18:43:40+00:00

I started to learn how to implement Rest WS on my website, but I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:43:41+00:00Added an answer on May 26, 2026 at 6:43 pm

    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/6 and 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:

    "/multiply/$first_nr/$second_nr"(controller:"calculator", action:"calc")
    

    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:

    "/$action/$first_nr/$second_nr"(controller:"calculator")
    

    Or even move the controller into the URL for the ultimate in extensibility:

    "/$controller/$action/$first_nr/$second_nr"{}
    

    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:

    import groovyx.net.http.RESTClient
    
    def calculator = new RESTClient( 'http://example.com/myapp/' )
    def resp = calculator.get( path : 'multiply/5/5' )
    assert resp.status == 200
    assert resp.data == "The result of the multiplication of 5 with 5 is 25"
    

    You can also find more information about groovy and REST on another stackoverflow question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Started to learn programming 2 months ago, in one of my little projects i
I started to learn using Instrument, but I cannot figure it out. After I
I started learn GWT by example on google and my first conclusion is: too
I started to learn Java, JSF2 and JPA2 and used Pet Catalog example project
I started to learn everything connected with Azure platform a little time ago. I'm
I just started to learn jquery and JavaScript so i can implement it with
I'm trying to learn from the http://jquery.malsup.com/form/#getting-started example. When I click submit, the alert
Started to learn Haskell, I decided to get acquainted with Parsec, but there were
I tried to learn on my own, but soon started to realize that by
I just started to learn Ruby and as a .Net developer, I'm wondering if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.