• RestTemplate 发送 get 请求使用误区 多个参数传值为null


    RestTemplate 发送 get 请求使用误区 多个参数传值为null(转载)

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

    RestTemplate 实例

    复制代码
    @Configuration
    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 为

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

    错误使用

    复制代码
    @Autowired
    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);
    }
    复制代码

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


    正确使用

    复制代码
    @Autowired
    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);
    }
    复制代码

    等价于

    复制代码
    @Autowired
    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", "测试短信内容");
    }
    复制代码
  • 相关阅读:
    解决远程桌面复制文件大于3G出错问题
    c++builder Form重载WindowProc、WndProc 截获消息
    FireDAC指定mssql驱动
    C++Builder 增加IPEdit控件
    MySQL 字符串按数值进行排序
    Java POI导出Word表格并使用输出流下载文件弹出打开保存框
    Java POI导出Excel并使用输出流下载文件弹出打开保存框
    MySQL 查询除某些字段以外的值
    MySQL 一个字段多个值查询
    Java Calendar 计算两个时间相隔天数
  • 原文地址:https://www.cnblogs.com/hejunnuo/p/10323603.html
Copyright © 2020-2023  润新知