簡體   English   中英

Grails Rest簡單計算器示例

[英]Grails Rest Simple Calculator Example

我開始學習如何在我的網站上實現Rest WS,但我發現它有點困難。 我的想法是從一個非常簡單的例子開始,當我理解基礎知識時,我將能夠理解更復雜的指南。 假設我們有一個包含2個文本字段的表單,我們引入2個數字,然后顯示這兩個數字的乘法。 這是完成該任務的代碼:

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]

    }

}

主要觀點:

<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>

結果視圖:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Sample title</title>
  </head>
  <body>
    <h1>${sms}</h1>
  </body>
</html>

我需要幫助: - 處理http和休息請求(Grails將知道正在請求哪一個) - 創建一個新類來發送請求(兩個數字作為輸入,結果作為輸出)。

PS。 對不起,如果這太基礎了,但如果我能在網上找到這樣的基本信息,我真的不會問這個問題。 PP,提前謝謝

要將REST請求路由到您的控制器,您需要修改UrlMappings.groovy文件,以按照您要發送的語法處理請求。 對多個兩個數字的REST請求可能看起來像example.com/multiply/5/6 ,結果會得到30。 要讓Grails將請求發送到您的CalculatorController的calc方法,您可以在UrlMappings.groovy中添加這樣的行:

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

或者如果你想支持其他操作,比如減去,添加等,你需要創建具有每個操作的名稱的方法,然后在映射中放置$ action代替乘法,如下所示:

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

或者甚至將控制器移動到URL中以實現最終的可擴展性:

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

這會將您的請求路由到您想要的適當操作,並為控制器方法填充適當的參數。 您還可能希望在控制器中使用withFormat閉包,根據請求的內容類型以各種不同的格式(XML,JSON,HTML)發回結果(有關withFormat用法的更多信息,請參閱grails文檔)。

我不確定你發送請求的類是什么意思。 應用程序可以調用此REST服務,就像調用任何其他REST服務一樣。 或者您正在尋找這樣做的例子嗎? 如果是這樣,請查看Grails的REST客戶端工具插件 您還可能會發現此博客條目對於使用Grails創建REST控制器和調用很有用。

UPDATE

要使用Groovy訪問REST服務,請嘗試使用HTTPBuilder的RESTClient擴展(在http://groovy.codehaus.org/modules/http-builder/doc/rest.html獲取)然后您可以調用該服務像這樣:

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"

您還可以在另一個stackoverflow問題上找到有關groovy和REST的更多信息。

這是我到目前為止看到的最好的Grails REST之一的鏈接。 它清楚地解釋了RESTful和RESTlike服務的含義以及如何編寫它們。 注意格式協商位,並記住Grails指南中提到的withFormat {} DSL。

http://www.ibm.com/developerworks/library/j-grails09168/

至於服務的客戶端 - 你真的想要一個JS庫來處理你的REST服務中的JSON / XML數據(查看Sencha的ExtJS或JQuery)。 如果您堅持使用GSP頁面而不是實際使用REST服務,除非您在初始頁面加載后執行所有遠程請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM