• 《Java Spring框架》Spring Http发送和接收案例


    以下案例基于:Spring 3.2x 版本。

    用于测试的Action(controller)。

        @RequestMapping(value = "getData.json")
        public @ResponseBody
        ServerResponse getData(HttpSession session, String flag){
            String str;
            if(!StringUtils.isBlank(flag)){
                str = "您传的是“"+flag+"”";
            }else{
                str = "您传的是空";
            }
            return new ServerResponse(str);
        }

    案例(get):

    import org.springframework.http.converter.StringHttpMessageConverter;
    import org.springframework.web.client.RestTemplate;
    import org.springframework.web.util.UriComponents;
    import org.springframework.web.util.UriComponentsBuilder;
    
    import java.net.URI;
    import java.nio.charset.Charset;
    import java.util.HashMap;
    import java.util.Map;
    
    public class HttpGetDemo {
    
        static String URL = "http://XXX.XXX.XXX.XXX:XXXX/getData.json";
    
        public static void main(String[] args) {
            //get请求案例。
            getRequest();
        }
    
        public static void getRequest(){
            RestTemplate restTemplate = HttpGetDemo.getInstance("utf-8");
            StringBuilder result = new StringBuilder();
            result.append(restTemplate.getForEntity(URL, String.class).getBody());
            System.out.println(result);
            System.out.println("--------------------------------------------------------------");
            //方式二
            result = new StringBuilder();
            result.append(restTemplate.getForEntity(URL+"?flag={1}", String.class, "方法2").getBody());
            System.out.println(result);
            System.out.println("--------------------------------------------------------------");
            //方式三
            result = new StringBuilder();
            Map<String, String> params = new HashMap<String, String>();
            params.put("flag", "方法3");
            result.append(restTemplate.getForEntity(URL+"?flag={flag}", String.class, params).getBody());
            System.out.println(result);
            System.out.println("--------------------------------------------------------------");
            //方式四
            result = new StringBuilder();
            UriComponents uriComponents = UriComponentsBuilder.fromUriString(
                    URL+"?flag={flag}")
                    .build()
                    .expand("方法4")
                    .encode();
            URI uri = uriComponents.toUri();
            result.append(restTemplate.getForEntity(uri, String.class).getBody());
            System.out.println(result);
        }
    
        public static RestTemplate getInstance(String charset) {
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(1,new StringHttpMessageConverter(Charset.forName(charset)));
            return restTemplate;
        }
    }

    运行结果:

    用于测试的Action(controller)。

    @RequestMapping(value = "getData.json")
    public @ResponseBody
    ServerResponse getData(HttpSession session,@RequestBody People people){
        return new ServerResponse(people);
    }

     案例(post):

    public class ServerResponse {
        String msg;
        String code;
        Boolean success;
        People data;
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        public String getCode() {
            return code;
        }
    
        public void setCode(String code) {
            this.code = code;
        }
    
        public Boolean getSuccess() {
            return success;
        }
    
        public void setSuccess(Boolean success) {
            this.success = success;
        }
    
        public People getData() {
            return data;
        }
    
        public void setData(People data) {
            this.data = data;
        }
    
        public String toString(){
            return "姓名:"+data.getName()+",年龄:"+data.getAge()+"。";
        }
    }
    public class People {
        private String name;
        private String age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAge() {
            return age;
        }
    
        public void setAge(String age) {
            this.age = age;
        }
    }
    import org.springframework.http.ResponseEntity;
    import org.springframework.http.converter.StringHttpMessageConverter;
    import org.springframework.web.client.RestTemplate;
    
    import java.nio.charset.Charset;
    
    public class HttpPostDemo {
        static String URL = "http://XXX.XXX.XXX.XXX:XXXX/getData.json";
    
        public static void main(String[] args) {
            //post请求案例。
            postRequest();
        }
    
        public static void postRequest(){
            RestTemplate restTemplate = HttpGetDemo.getInstance("utf-8");
            People people = new People();
            people.setAge("18");
            people.setName("李磊");
            ResponseEntity<ServerResponse> serverResponseEntity = restTemplate.postForEntity(URL, people,ServerResponse.class);
            System.out.println(serverResponseEntity.getBody());
            System.out.println("--------------------------------------------------------------");
    
            //方式二
            people = new People();
            people.setAge("20");
            people.setName("韩梅梅");
            ServerResponse serverResponse = restTemplate.postForObject(URL, people, ServerResponse.class);
            System.out.println(serverResponse);
        }
    
        public static RestTemplate getInstance(String charset) {
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(1,new StringHttpMessageConverter(Charset.forName(charset)));
            return restTemplate;
        }
    }

    运行结果:

    This moment will nap, you will have a dream; But this moment study,you will interpret a dream.
  • 相关阅读:
    #import &lt;/usr/include/objc/objc-class.h&gt; not such file or directory问题的解决方法
    关于二进制补码
    DirectSound的应用
    Qt on Android: http下载与Json解析
    双绞线的制作,T568A线序,T568B线序
    Java设计模式之从[暗黑破坏神存档点]分析备忘录(Memento)模式
    linux 系统升级中的一些配置
    malloc函数具体解释
    老鸟的Python新手教程
    ubuntu12.04 安装配置jdk1.7
  • 原文地址:https://www.cnblogs.com/jssj/p/11594549.html
Copyright © 2020-2023  润新知