[英]Consuming a RestAPI JSON in grails 2.3.x
I am new to grails and I am learning. 我是新手,正在学习。 I want to consume a webservice API(Rest). 我想使用一个Web服务API(Rest)。 I searched for the example but didn't get a good one. 我搜索了示例,但没有得到很好的示例。 My requirement is: The address to consume is eg:localhost:8080/order/createOrder; 我的要求是:要使用的地址为:例如:localhost:8080 / order / createOrder; which will provide the JSON. 它将提供JSON。 My JSON format will be : 我的JSON格式为:
//and from JSON is:
username: 'abc',
password: 'xyz',
order: {
orderDate
orderNumber
subTotal
shipping
discount
netTotalPaid
creditApplied
transactionId
specialInstruction
}
I have to get the username,password and Order from JSON. 我必须从JSON获取用户名,密码和订单。 Order is a Domain class where I will set the data from JSON. Order是一个Domain类,在其中我将通过JSON设置数据。
Please suggest me with a detail explanation. 请给我详细解释。
And if the JSON is not REST service,ieThe application itself provides JSON as a service ? 并且如果JSON不是REST服务,即应用程序本身提供JSON作为服务? , if we don't need to consume a REST service ? ,如果我们不需要使用REST服务?
Getting the JSON 获取JSON
Again, a few options. 同样,一些选择。 I like to use the REST Client Builder Plugin for this sort of thing. 我喜欢将REST Client Builder插件用于此类操作。 It comes with lots of utility methods to make your life easier. 它带有许多实用程序方法,使您的生活更轻松。
For example, to get the JSON from your end point, it would be something like: 例如,要从端点获取JSON,将类似于:
def resp = rest.get("http://grails.org/api/v1.0/plugin/acegi/")
def myJson = resp.json;
Using the JSON 使用JSON
There's a few options. 有一些选择。
If Order
is a domain class that maps to these values exactly, then you can use the GSON
plugin. 如果Order
是一个完全映射到这些值的域类,则可以使用GSON
插件。 This enables the serialization and de serialization of POJOs (or grails Domain objects). 这将启用POJO(或grails域对象)的序列化和反序列化。 An example of the usage is: 用法的示例是:
Gson gson = new GsonBuilder().create()
Order myOrder = gson.fromJson(myJsonData, Order.class)
println myOrder
Or if you want to keep things Grails, you can use the converters.. 或者,如果您想保留Grails的东西,可以使用转换器。
def myOrder = new Order(request.GSON);
Personally, I prefer the former. 就个人而言,我更喜欢前者。
Design Tips 设计技巧
Don't put all this stuff in a controller. 不要将所有这些东西放在控制器中。 Abstract it out to a service, so that your controller simply passes it off to the service layer and doesn't care what happens to it. 将其抽象为服务,以便您的控制器将其简单地传递给服务层,而不管它发生了什么。
Put your URLs to the RESTful service in some sort of object, or string. 将您的URL放入某种对象或字符串中的RESTful服务。 If you change API endpoints, you don't want to have to update it all over your application. 如果更改API端点,则不需要在整个应用程序中都进行更新。
Extra Reading 额外阅读
The GSON plugin home is where you can find the dependency to add to your build config. 在GSON插件主页中,您可以找到要添加到构建配置的依赖项。
The Docs will show you how to use the GSON plugin. 该文档将向您展示如何使用GSON插件。
And these Docs will show you how to use the REST Client Builder Plugin. 这些文档将向您展示如何使用REST Client Builder插件。
I guess this will help.. 我想这会有所帮助..
def restBuilder = new RestBuilder()
def resp = restBuilder.get("http://localhost:8080/order/createOrder")
JSONObject mapData = data.getJSONObject(resp.json)
JsonSlurper slurper = new JsonSlurper()
def object = slurper.parseText(mapData.toString())
def order = Order(object)
Here Order is pojo class 订单是pojo类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.