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

摘要:
˃转换器=迭代器。next();如果{iterator.remove();}}消息转换器。添加returnrestTemplate;}}请求地址get请求url为http://localhost:8080/test/sendSms?phone=手机号码&msg=错误使用@AutowiredprivateRestOperationsrestOperations;publicvoidtest()throwsException{Stringurl=“http://localhost:8080/test/sendSms“;MapuriVariables=newHashMap();uriVariables.put;uriVarianges.put;Stringresult=restOperations.getForObject;}当服务器收到请求时,您会发现@AutowiredprivateRestOperationsrestOperations;publicvoid test()throwsException{Stringurl=”http://localhost:8080/test/sendSms?

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

首先看一下官方文档是怎么描述的,传递多个值的情况(注意例子中用到的@pathParam,一般要用@queryParam)RestTemplate 发送 get 请求使用误区 多个参数传值为null第1张

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", "测试短信内容");
}
复制代码

免责声明:文章转载自《RestTemplate 发送 get 请求使用误区 多个参数传值为null》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Nodejs介绍及npm工具使用easyui中datagrid自带loading效果下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Switch重构处理

public class Fun{         public void IFNumberid(int flag)          {               switch (flag)          {         case 1:...

测试开发进阶——spring boot——MVC——get访问——使用@RequestParam获取参数(前端可传可不传参,以及使用默认值)

控制器: package com.awaimai.web; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframewo...

转:获取客户端网卡的MAC地址

教程由JAVA中文网整理校对发布(javaweb.cc) 1.获取客户端ip地址( 这个必须从客户端传到后台):jsp页面下,很简单,request.getRemoteAddr() ;因为系统的VIew层是用JSF来实现的,因此页面上没法直接获得类似request,在bean里做了个强制转换 1. public String getMyIP() { 2....

MyBatisplus 使用IPage和Page分页

@RequestMapping(value = "/cityList",method =RequestMethod.POST) public ResponseVo<IPage<GovernanceRegulationClosePo>> querySearchCityDataList(@RequestBody Map<S...

二、获取微信用户openId

/// <summary> /// 登录首页 /// </summary> /// <returns></returns> public ActionResult Index() { if (Session["isTrue"] == null) { string weixinA...

jmeter:BeanShell 预处理程序(引用java脚本,实现参数化)

这里以为本地的注册接口为例子: 如图:这个注册接口需要传2个参数,用户名和密码,用户名是唯一的,密码可以一样,需要对用户名进行参数化。 首先在jmeter里面添加BeanShell 预处理程序,如图: 这里需要编写java代码,对用户名实现参数化: 调试好以后,运行效果如图: 代码如下: package com.mg.java.study; imp...