Parsing restful services with spring resttemplate

Source: Internet
Author: User

In the previous blog post "Building restful services with spring MVC 4", we talked about how to use Spring MVC 4 to build restful services, based on the above article, and continue to explain how to parse spring restful services. In the previous article, we used the Jacson package to serialize the Java object, and this article still uses this package to deserialize the Java object.


Building RESTful Services


1. We continue to add the Buildrestuser () method to the Favrestfulcontroller class, which serializes the Favuser object into a JSON object output, with the following code:


@RequestMapping (value= "Buildrestuser") public Favuser Buildrestuser () {Favuser favuser = new Favuser ();  Favuser.setuserid ("mm");  Favuser.setusername ("Meimei");  Favuser.setuserage (18);  Favuser.setcreatedate (New Date ()); return favuser; }


2. Start the Favspringmvcrestful project, use the Firefox restclient plugin to test, enter http://localhost in the URL: 8080/favspringmvcrestful/ Buildrestuser, the output as shown in the results, the RESTful service is built to complete.


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/53/94/wKiom1RrP5bQXK_kAAIBhwZkpb8047.jpg "title=" Rest-result.png "style=" width:600px;height:395px; "alt=" wkiom1rrp5bqxk_kaaibhwzkpb8047.jpg "width=" "height=" 395 "border=" 0 "hspace=" 0 "vspace=" 0 "/>



Parsing RESTful Services


1. Create a new "consumerestful" Java project to import the jar required for the project in the build path.


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/53/92/wKioL1RrQDjwbP5dAAQOgt7G__s789.jpg "title=" Buildpath.png "width=" "height=" 448 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:600px;height:448px; "alt=" Wkiol1rrqdjwbp5daaqogt7g__s789.jpg "/>


2. Import the Favuser.java object, note that the date type in the object should be the same as the date type of the Favuser object in the build restful service, that is, the CreateDate getter method is formatted, or an error will be made during deserialization.


  3. Create a new Consumespringrestful.java, and in the main () method, test the RESTful service.


package com.favccxx.favsoft.main;import org.springframework.web.client.resttemplate;import  com.favccxx.favsoft.pojo.favuser;public class consumespringrestful {public static  Void main (String[] args)  {resttemplate resttemplate = new resttemplate (); Favuser favuser = resttemplate.getforobject ("http://localhost:8080/favspringmvcrestful/ Buildrestuser ",  favuser.class);         system.out.println (" UserId :     " + favuser.getuserid ());         System.out.println ("username:    "  + favuser.getusername ());         system.out.println ("userage:    "  +  Favuser.getuserage ());         system.out.println ("createDate:      " + favuser.getcreatEDate ());}} 


4. Run the project and enter the following, which explains the success of the RESTful service.


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/53/94/wKiom1RrQDKCw7P4AAMYPskEUqo810.jpg "title=" Favrestful.png "width=" "height=" 282 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:600px;height:282px; "alt=" Wkiom1rrqdkcw7p4aamypskeuqo810.jpg "/>



  in this case, the Jacson framework parses the JSON object, and of course it can be parsed using a framework such as Gson,fastjson,jersey, but the usual practice is to deserialize the JSON as the same as the serialized tool class. prevent data mapping errors caused by types mismatch.


This article is from the "Dust Wind with the Sky" blog, please be sure to keep this source http://favccxx.blog.51cto.com/2890523/1579305

Parsing restful services with spring resttemplate

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.