Java Generics and Restlet clients

Source: Internet
Author: User

Write a client class that communicates with the Restlet server to test for successful communication and to interact. In order to facilitate the use of other people, so, write a common method of encapsulation, but halfway through the release of some problems.

In the normal way, the sequence goes down like this:

  Public Static voidMain (String args[]) {string URL= "Http://localhost:8888/hi"; Clientresource Clientresource=Newclientresource (URL); User User=NewUser (); User.setname ("ZZ"); User.setage ("12");Representation Representation=clientresource.post ((NewJacksonrepresentation<user>(user))); Jacksonrepresentation<User> jacksonrepresentation=NewJacksonrepresentation<user> (Representation,user.class); Try{User user1=Jacksonrepresentation.getobject ();            System.out.println (User1.getname ());        System.out.println (User1.getage ()); } Catch(IOException e) {e.printstacktrace (); }    }

So there is no problem, you can successfully get the data returned by the server, so on this basis, the restlet things to wrap up. Written in this way:

    Public StaticObject getserverresponse (String url,object user) {Clientresource clientresource=Newclientresource (URL); Representation representation=clientresource.post ((NewJacksonrepresentation<object>(user))); Jacksonrepresentation<Object> jacksonrepresentation=NewJacksonrepresentation<object> (Representation,object.class); Object o=NULL; Try{o=Jacksonrepresentation.getobject (); } Catch(IOException e) {e.printstacktrace (); }        returno; }

So in the main method, you can convert object to user. However, during the conversion process, an error is reported:

Exception in thread "main" Java.lang.ClassCastException:java.util.LinkedHashMap cannot is cast to test. User

Unable to convert Linkedhashmap to user, Linkedhashmap is not used in the code and should be the problem that occurs during the Restlet conversion process. Later tangled up, asked a more experienced colleagues, only to know the above conversion, and did not give a clear definition of the type of return, only converted to object, this is not possible.

After the transformation, the final wording is this:

   Public Static<t, v> V get (String URL, T data, class<v>response) {Clientresource Clientresource=Newclientresource (URL); Representation representation=clientresource.post ((NewJacksonrepresentation<t>(data))); Jacksonrepresentation<V> jacksonrepresentation=NewJacksonrepresentation<v>(representation, response); Try {            returnJacksonrepresentation.getobject (); } Catch(IOException e) {e.printstacktrace (); }        return NULL; }

Thus, using generics, in this encapsulated method, the type of the returned data is specified so that no previous error occurs.

Java Generics and Restlet clients

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.