简体   繁体   English

在Ratpack Groovy中解析json

[英]Parse json in Ratpack Groovy

I started a small ratpack app in the Groovy console but I couldn't work out from the documentation how to get a hold of json data that has been sent in the request. 我在Groovy控制台中启动了一个小型的ratpack应用程序,但我无法从文档中找到如何获取已在请求中发送的json数据。

@Grab("io.ratpack:ratpack-groovy:0.9.4")
import static ratpack.groovy.Groovy.*
import groovy.json.JsonSlurper

ratpack {
  handlers {
    get {
      def slurper = new JsonSlurper()
      def result = slurper.parseText('{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}')
      render "Hello world! ${result.person}"
    }
    post("foo") {
      def slurper = new JsonSlurper()
      def result = slurper.parseText("WHAT DO i PUT HERE?")
      render "Hello world! ${result.person}"
    }
  }
}

And an example request: 一个示例请求:

curl -XPOST -H "Content-Type: application/json" -d '{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}' localhost:5050/foo

Ratpack provides a concept known as a Parser that allows you to parse incoming request body to a given type. Ratpack提供了一个称为Parser的概念,允许您将传入的请求主体解析为给定类型。

In your case, you could parse incoming request body to a JsonNode, or your own type using the ratpack-jackson module. 在您的情况下,您可以使用ratpack-jackson模块将传入的请求主体解析为JsonNode或您自己的类型。 You can find more information here . 您可以在此处找到更多信息。

Here is your example using the parser provided by the ratpack-jackson module: 以下是使用ratpack-jackson模块提供的解析器的示例:

@Grab("io.ratpack:ratpack-groovy:0.9.12")     
@Grab("io.ratpack:ratpack-jackson:0.9.12")    

import static ratpack.groovy.Groovy.*         
import ratpack.jackson.JacksonModule          
import static ratpack.jackson.Jackson.jsonNode

ratpack {                                     
  bindings {                                  
    add new JacksonModule()                   
  }                                           
  handlers {                                  
    post("foo") {                             
      def postBody = parse jsonNode()
      render "Hello world! ${postBody.person}"
    }                                         
  }                                           
}                 

Your curl 你的卷曲

curl -XPOST -H "Content-Type: application/json" -d '{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}' localhost:5050/foo

Responds as 回复为

Hello world! {"name":"Guillaume","age":33,"pets":["dog","cat"]}

I hope this helps! 我希望这有帮助!

The earlier answers are no longer applicable. 之前的答案不再适用。
Since version 0.9.19, Ratpack returns a Promise. 从版本0.9.19开始,Ratpack返回一个Promise。 Also, it includes Jackson parsing support. 此外,它还包括Jackson解析支持。 So your example would look like this as of the time of this writing: 因此,在撰写本文时,您的示例将如下所示:

context.parse(Jackson.fromJson(Map)).then { data ->
    println data.person
    // do something with person
    context.response.status(201).send()
}

Full docs available here: https://ratpack.io/manual/current/api/ratpack/jackson/Jackson.html#parsing 完整文档可在此处获取: https//ratpack.io/manual/current/api/ratpack/jackson/Jackson.html#parsing

ps One semantic change I made to the example is to return a 201 (created), since it's a POST. ps我对该示例所做的一个语义更改是返回201(已创建),因为它是POST。

request.body.text would give the string format of the JSON body request.body.text将给出JSON主体的字符串格式

post("foo") {
  def slurper = new JsonSlurper()
  def result = slurper.parseText( request.body.text )
  render "Hello world! ${result.person}"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM