RESTful interface, object, map, and date type of the reference method

Source: Internet
Author: User
Tags getmessage

The

Code is as follows:

Import Org.springframework.stereotype.Controller;
Import Org.springframework.web.bind.annotation.RequestBody;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.ResponseBody;

Import Org.springframework.web.client.RestTemplate;
Import Java.util.Date;
Import Java.util.HashMap;

Import Java.util.Map; /** * @author Administrator */@Controller @RequestMapping (value = "/test") public class TestController {@Reque Stmapping (value = "/object", method = Requestmethod.post) @ResponseBody The Public String object (@RequestBody Location Model model) {return model.getlongitude () + "" + model.getlatitude () + "" + Model.getcreateby () + "" + MODEL.G
    Etcreatetime (); @RequestMapping (value = "/map", method = Requestmethod.post) @ResponseBody The public String map (@RequestBody M Ap<string, string> map) {String CreateBy = Map.get ("CreaTeby ");
    return CreateBy; @RequestMapping (value = "/date", method = Requestmethod.post) @ResponseBody The public String map (@RequestBody
    Date date) {return date.tostring ();
            @RequestMapping (value = "/test_object") @ResponseBody public String Callwsobject () {try {
            Resttemplate template = new Resttemplate ();
            String url = "Http://localhost:8080/location/object";
            Locationmodel model = new Locationmodel ();
            Model.setlongitude ("110.234567");
            Model.setlatitude ("222.238043");
            Model.setcreateby ("wh@aishk.com");
            Model.setcreatetime ("2016-08-21 14:03:26");
            String result = Template.postforobject (URL, model, string.class);
        Return ' Result: ' + result;
            catch (Exception e) {//Todo:handle Exception e.printstacktrace ();
        return E.getmessage (); }} @RequestMapping (value = "/test_map ") @ResponseBody public String Callwsmap () {try {resttemplate template = new Resttemplat
            E ();
            String url = "Http://localhost:8080/location/map";
            map<string, string> map = new hashmap<string, string> ();
            Map.put ("CreateBy", "Xiaohong");
            String result = Template.postforobject (URL, map, string.class);
        Return ' Result: ' + result;
            catch (Exception e) {//Todo:handle Exception e.printstacktrace ();
        return E.getmessage ();
            @RequestMapping (value = "/test_date") @ResponseBody public String callwsdate () {try {
            Resttemplate template = new Resttemplate ();
            String url = "Http://localhost:8080/location/date";
        Return Template.postforobject (URL, new Date (), string.class);
         catch (Exception e) {//Todo:handle Exception e.printstacktrace ();   return E.getmessage (); }
    }
}

Test object, open Browser, Access: http://localhost:8080/project name/test/test_object
Show Results:

result:110.234567 222.238043 xiaoming 2016-08-21 14:03:26

Test map, Open browser, Access: http://localhost:8080/project name/test/test_map
Show Results:

Result:xiaohong

Test date, open browser, Access: http://localhost:8080/project name/test/test_date
Show Results:

Wed Oct 10:19:58 CST 2017

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.