A practical guide. Access RESTful Web Services with Play Framework 2.1 for Beginners

I am new to many of the concepts and technologies used in this matter, so I would appreciate a little understanding and help to the newcomer community. I am using the Play Framework version 2.1.3, and I need to transfer the POST data to the RESTful web service so that it can be inserted into the remote database. An XML response will be returned indicating either success or failure.

I’m sure that you know that the documentation for the Play Framework is completely absent and in no way useful for beginners, so I’m not sure how to accomplish this task based on best practices. I am looking for a Java solution for this problem, now I do not have time to learn the Scala language. My experience with web services is quite limited, usually I would use the DAO design pattern (or use one of the many available ORM libraries depending on my needs) in my application and use JDBC to connect directly to the database. This is not an option here.

My first question should be, is there a recommended design scheme for accessing web services? Then, given the structure of Play MVC, how best to implement such a design pattern, pack the data (assuming the application has already captured and confirmed data from the user), send it and process the responses back to the user?

I know this is a rather lengthy question, but my intention is to create a knowledge base for beginners that can easily enter with limited experience, read, understand and replicate what they find here to create a working solution. Having been on the Internet quite widely, I found several unrelated fragments, but nothing concrete with these technologies and modern textbooks. Thank you for your time.

+6
source share
1 answer

Creating queries is straightforward. First you specify the url. There are various ways to add content types, request parameters, timeouts, etc. In request. Then you select the type of request and, possibly, add some content to send. Examples:

WSRequestHolder request = WS.url("http://example.com"); request.setQueryParameter("page", "1"); Promise<Response> promise = request.get(); 
 Promise<Response> promise = WS.url("http://example.com").post(content); 

The tricky part is to send it and use the response of the request. I assume that you have a controller that should return the Result user to the user based on the response of the web service. The result is usually a displayed template or maybe just a status code.

Playback avoids blocking using Futures and Promises . The async controller accepts a Promise<Result> and returns a result (future value) at some point later. An easy-to-use promise is the get and post methods shown above. You do not need to worry about their implementation, you just need to know what they promised to provide Response after the request is completed.

Note the problem here: when creating a query using WS.url("...").get() it will give you a Promise<Response> , although async takes a Promise<Result> . Here you must fulfill another promise yourself, which converts the answer to the result using the map method. If you follow the Play documentation, this will be a bit confusing because Java has no closures (though) and everything needs to be wrapped in a class. However, you do not need to use anonymous classes inside the method call. If you prefer cleaner code, you can also do it like this:

 return async( request .get() // returns a `Promise<Response>` .map(resultFromResponse) // map takes a `Function<Response, Result>` and // returns the `Promise<Result>` we need ); 

The resultFromResponse object may look like this. This is actually just like a cumbersome definition of some kind of callback method that takes a Response as soon as an argument and returns a Result .

 Function<Response, List<T>> resultFromResponse = new Function<Response /* 1st parameter type */, Result /* return type */>() { @Override public Result apply(Response response) { // example: read some json from the response String message = response.asJson().get("message"); Result result = ok(message); return result; } }; 

As @itsjeyd pointed out in the comments when calling web services in Play 2.2.x , you no longer end the call in async . You just return the Promise<Result> :

 public static Promise<Result> index() { return request.get().map(resultFromResponse); } 
+11
source

Source: https://habr.com/ru/post/952920/


All Articles