Java – how to: access restful web services for beginners using play framework 2.1

Many concepts and techniques I use in this problem are quite new, so I hope to know and help beginners in the community I'm using play framework 2.1 In version 3, I need to post data to a restful web service so that I can insert it into a remote database An XML response will be returned indicating success or failure

I'm sure you know that the documentation of play framework is very scarce and is not helpful for beginners, so I'm not sure how to complete this task considering best practices I'm looking for a Java solution to this problem. I don't have time to learn Scala at present My experience with web services is quite limited. Usually, I will implement DAO design pattern in my application (or use one of many available ORM libraries as needed) and use JDBC to connect directly to the database This is not an option

My first question is, is there a recommended design pattern for accessing web services? Then, considering the play MVC framework, how can we best implement such a design pattern, package the data (assuming that the application has captured and verified the user's data), send it out and process the response back to the user?

I know this is a rather lengthy problem, but my purpose is to create a knowledge base for beginners. They can easily obtain limited experience, read, understand and copy what they find here, so as to produce an effective solution After extensive web search, I found some disjointed fragments, but I didn't specifically cover these technologies or the latest tutorials Thank you for your time

Solution

Creating a request is simple First, you provide a URL There are many ways to add content types, query parameters, timeout, etc. to the request You can then select a request type and optionally add some content to send example:

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 complex part is sending it and using the requested response I assume you have a controller that should return the results to the user based on the response of the web service The result is usually a rendering template or maybe just state code

Play by using futures and promises to avoid blocking The asynchronous method of the controller adopts promise < result > and returns the result (future value) at a later time The get and post methods shown above provide an easy - to - use promise You don't need to care about their implementation, just know that they promise to provide a response when the request is completed

Note the problem here: when using WS When URL ("...") creates a request, get () will give you a promise < response > even if promise < result > is accepted asynchronously Here, you have to fulfill another promise yourself, which will use the map method to convert the response to the result If you follow the play documentation, this may seem a little confusing because Java has no closures (but) and everything must be included in the class You do not have to use anonymous classes in method calls If you prefer cleaner code, you can also do 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 object resultfromresponse might look like this It's actually like the tedious definition of some callback method. It takes response as the only parameter and returns 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, calling web services in play 2.2 X, you no longer wrap the call asynchronously You just need to return promise < result >:

public static Promise<Result> index() {
    return request.get().map(resultFromResponse);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>