服务端代码如下
@RequestMapping(value = "/data", method = RequestMethod.GET)
public void getList(HttpEntity<String> entity){
String dataJson = entity.getBody();
String typeStr = entity.getHeaders().getFirst("type");
/*
处理
*/
}
请求的URL地址是http://localhost:8080/restful/data
消息为
String reportJson ="json内容......"
String reportType = "类型";
请问客户端的GET请求该如何来构造?
@RequestMapping(value = "/data", method = RequestMethod.GET)
public void getList(HttpEntity<String> entity){
String dataJson = entity.getBody();
String typeStr = entity.getHeaders().getFirst("type");
/*
处理
*/
}
请求的URL地址是http://localhost:8080/restful/data
消息为
String reportJson ="json内容......"
String reportType = "类型";
请问客户端的GET请求该如何来构造?
解决方案 »
- pdf2swf 转换xls文件时内存溢出
- 大家帮我看看 后台报的这个错误是什么意思呀
- struts1标签错误,提示html:text property method不存在
- java软件开发人员需要学习什么
- 分类
- java byte[]转成String
- 请牛人指点,如何以最优的算法,对两点间的距离进行排序。
- 关于bo和vo的疑惑
- 苍天那儿,大地啊,那位大哥大姐替我出口气啊!——java.lang.reflect.InvocationTargetException
- EJB 3.0新特性。。。
- 我想做搜索引擎
- 文档转换报错 conversion failed: could not load input document
String reportJson ="json内容......"
String reportType = "类型";
HttpURLConnection con =
(HttpURLConnection)new URL("http://localhost:8080/restful/data").openConnection();
con.setRequestMethod("POST"); //设置为post提交
con.setRequestProperty("type", reportType ); //设置header["类型"]
BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
bout.write(reportJson);
bout.flush();
bout.close(); // 取得输入流,并使用Reader读取
BufferedReader reader = new BufferedReader(new InputStreamReader(
con.getInputStream())); String lines;
while ((lines = reader.readLine()) != null) {
System.out.println(lines);
}
reader.close();由于你的action有用 entity.getBody(); 来获取请求体 所以 应该用post method = RequestMethod.POST
如果坚持用GET方式,也没问题
那就把这句删了吧 con.setRequestMethod("POST");
设置true以后就
Server returned HTTP response code: 405 for URL
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html可用的话,楼主结贴或加分吧,呵呵还是那句话,最好用post,因为你需要提交的json内容是通过entity.getBody();获取的
get的entity.getBody();是空的至于get和post的区别,我就不科普了,百度一大堆
String reportJson ="json内容......"
String reportType = "类型";HttpHeaders headers = new HttpHeaders();
headers.set("type", reportType);
HttpEntity<String> entity = new HttpEntity<String>(reportJson, headers);
String respSrc = template.postForObject("http://localhost:8080/restful/data", entity, String.class);
String reportJson ="json内容......"
String reportType = "类型";HttpHeaders headers = new HttpHeaders();
headers.set("type", reportType);
HttpEntity<String> entity = new HttpEntity<String>(reportJson, headers);
ResponseEntity<String> respEntity = template.exchange(new URI("http://localhost:8080/restful/data"), RequestMethod.GET, entity, String.class);
System.out.print(respEntity.getBody());