zoukankan      html  css  js  c++  java
  • RestTemplate 发送 get 请求使用误区 多值为null

    http://blog.csdn.net/zhousenshan/article/details/71055687

    *****************************************************

    首先看一下官方文档是怎么描述的,传递多个值的情况(注意例子中用到的@pathParam,一般要用@queryParam)

    RestTemplate 实例

    guration
    public class RestConfiguration {
    
        @Bean
        @ConditionalOnMissingBean({RestOperations.class, RestTemplate.class})
        public RestOperations restOperations() {
            SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
            requestFactory.setReadTimeout(5000);
            requestFactory.setConnectTimeout(5000);
    
            RestTemplate restTemplate = new RestTemplate(requestFactory);
    
            // 使用 utf-8 编码集的 conver 替换默认的 conver(默认的 string conver 的编码集为 "ISO-8859-1")
            List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
            Iterator<HttpMessageConverter<?>> iterator = messageConverters.iterator();
            while (iterator.hasNext()) {
                HttpMessageConverter<?> converter = iterator.next();
                if (converter instanceof StringHttpMessageConverter) {
                    iterator.remove();
                }
            }
            messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
    
            return restTemplate;

    请求地址

    get 请求 url 为

    /localhost:8080/test/sendSms?phone=手机号&msg=短信内容

    错误使用

    ired
    private RestOperations restOperations;
    
    public void test() throws Exception{
        String url = "http://localhost:8080/test/sendSms";
    
        Map<String, Object> uriVariables = new HashMap<String, Object>();
        uriVariables.put("phone", "151xxxxxxxx");
        uriVariables.put("msg", "测试短信内容");
    
        String result = restOperations.getForObject(url, String.class, uriVariables);

    服务器接收的时候你会发现,接收的该请求时没有参数的


    正确使用

    ired
    private RestOperations restOperations;
    
    public void test() throws Exception{
        String url = "http://localhost:8080/test/sendSms?phone={phone}&msg={phone}";
    
        Map<String, Object> uriVariables = new HashMap<String, Object>();
        uriVariables.put("phone", "151xxxxxxxx");
        uriVariables.put("msg", "测试短信内容");
    
        String result = restOperations.getForObject(url, String.class, uriVariables);

    等价于

    ired
    private RestOperations restOperations;
    
    public void test() throws Exception{
        String url = "http://localhost:8080/test/sendSms?phone={phone}&msg={phone}";
    
        String result = restOperations.getForObject(url, String.class,  "151xxxxxxxx", "测试短信内容");
  • 相关阅读:
    windows常用快捷键
    清华为什么被称为“水木清华”
    CTP程序化系统开发(C++ && PHP)
    PHP判断SESSION过期的方法
    Linux 下 ---ThinkPHP 图片上传提示:上传根目录不存在!请尝试手动创建
    &#65279导致页面顶部空白一行解决方法 【】
    Code笔记 之:ajax诡异的错误-请求status为200
    PHP之:序列化和反序列化-serialize()和unserialize()
    Linux之:Ubuntu速学笔记(1)
    C语言归并排序(合并排序)算法及代码
  • 原文地址:https://www.cnblogs.com/zhao1949/p/7600237.html
Copyright © 2011-2022 走看看