RestTemplate的get传参和接收问题

clorxd 2015-12-01 11:10:16
我最近才知道Spring有个RestTemplate于是想在实际开发中试试,可却遇到个奇怪的问题,我要调通的目标接口是C#编写的一个接口,接口可用Http get方法访问,我用RestTemplate先进行了如下调用
 String result=template.getForObject("http://172.16.88.128/shortmessageservice.asmx/Send?sysName=DFCFMonitor&phoneNumbers=123123123123&content=2131&priority=1", String.class);

返回结果一切正常(因为我的IP还没开相应权限)
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{"success":false, "message":"您的IP地址没有授权-172.16.20.162", "messageid":-1}</string>

可是当我如下使用时就报错了
RestTemplate template=new RestTemplate();
Map<String,String> info=new HashMap<String,String>();
info.put("sysName","DFCFMonitor");
info.put("phoneNumbers","123123123123");
info.put("content","2131");
info.put("priority","1");
String result=template.getForObject("http://172.16.88.128/shortmessageservice.asmx/Send", String.class,info);

错误如下,不知道为何会如此,而且我少传参数测试表明:对方的接口是有参数校验的,问题应该在我这,错误500也很让我费解,求教到底是怎么回事?还有就是我搞如何解析这样XML里夹杂jeson的返回格式(目前的唯一想法就是subString,有没有更好点的)
Exception in thread "main" org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:565)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:272)
...全文
18944 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
MImanchi 2019-06-19
  • 打赏
  • 举报
回复
引用 6 楼 m0_38138278 的回复:
如果你是get带参,如果需要通过map的形式传参。你url要这样写,通过http://172.16.88.128/shortmessageservice.asmx/Send?sysName={sysName}&phoneNumbers={phoneNumbers}&content={content}&priority={priority} 参数与你最后一个map的key对应
正解
YKRY35 2018-11-02
  • 打赏
  • 举报
回复
HashMap 改用 MultiValueMap 试试?
夜雨恋星辰 2018-09-04
  • 打赏
  • 举报
回复
RestTemplate不是很清楚,后面截取字符串的可以使用正则
public static void main(String[] args) {
String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?> " +
" <string xmlns=\"http://tempuri.org/\">" +
" {\"success\":false, \"message\":\"您的IP地址没有授权-172.16.20.162\", \"messageid\":-1}" +
" </string>";
System.out.println("字符串为:" + str);
String pat = "<string.*>(.*)</string>";
Pattern pattern = Pattern.compile(pat,Pattern.DOTALL);
Matcher m = pattern.matcher(str);
if(m.find()){
System.out.println("string标签中的字符串为: " + m.group(1) );
}
}
黑白猿 2018-09-03
  • 打赏
  • 举报
回复 1
应该是服务器端参数解析出错导致,第二种方式传递代码有点问题,服务器端解析时没有找到相应参数报错。参考你给出的第一种方式的参数,如果想用Map传递参数,请考虑使用如下代码,下面的代码和你第一种方式是等价的。:
        RestTemplate template=new RestTemplate();
Map<String,String> mapInfo=new HashMap<String,String>();
mapInfo.put("sysName","DFCFMonitor");
mapInfo.put("phoneNumbers","123123123123");
mapInfo.put("content","2131");
mapInfo.put("priority","1");
String result=template.getForObject("http://172.16.88.128/shortmessageservice.asmx/Send?sysName={sysName}&phoneNumbers={phoneNumbers}&content={content}&priority={priority}", String.class, mapInfo);
m0_38138278 2018-09-03
  • 打赏
  • 举报
回复
如果你是get带参,如果需要通过map的形式传参。你url要这样写,通过http://172.16.88.128/shortmessageservice.asmx/Send?sysName={sysName}&phoneNumbers={phoneNumbers}&content={content}&priority={priority} 参数与你最后一个map的key对应
Abellor 2016-12-13
  • 打赏
  • 举报
回复
你用get方式需要将值拼接在url后面,你第一次正确的那是一种方式,第二次你用了map传值,但是同时你还得在url后面用占位符的形式将参数拼接在后面才可以这是第二种方式,例:url?param={param} 这样
dracularking 2015-12-14
  • 打赏
  • 举报
回复
引用 3 楼 clorxd 的回复:
引用 2 楼 dracularking 的回复:
RestTemplate的那个是post方式吧?如果是的话,服务端支持post吗?
我用的是get,服务端是没问题的
问题是你第二种方式使用时,getForObject的重载实现方式未必就是get,或者哪里实现不一样,要找资料看一下的
clorxd 2015-12-14
  • 打赏
  • 举报
回复
引用 2 楼 dracularking 的回复:
RestTemplate的那个是post方式吧?如果是的话,服务端支持post吗?
我用的是get,服务端是没问题的
clorxd 2015-12-04
  • 打赏
  • 举报
回复
没人懂么?⊙﹏⊙b汗
dracularking 2015-12-04
  • 打赏
  • 举报
回复
RestTemplate的那个是post方式吧?如果是的话,服务端支持post吗?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧