@RequestBody获取Json请求数据
2016-07-22 09:02
597 查看
使用springmvc在后端获取前端发送的json数据,使用的是@requestBody注解,前后端代码如下
但是收到的code是base64编码之后的,于是将String类型的code改成对象类型。
改完之后无法访问到这个接口了,参数不匹配,于是把前端ajax改成如下
时间有限,需要加深了解springmvc的json自动转换机制。 http://blog.csdn.net/kobejayandy/article/details/12690555
$("#submitCode").click(function() { var code = editor.getValue(); $.ajax({ type : "POST", url: "compileJava", contentType: "application/json; charset=utf-8", dataType: "json", headers: { Accept: "application/json" }, data: {code: code}, success : function(data) { alert(data.msg); $("#response").html(data.msg); } }); });
@RequestMapping(value = "/compileJava", method = RequestMethod.POST) @ResponseBody public Object CompileJava(HttpServletRequest request, @RequestBody String code) { String path = request.getSession().getServletContext().getRealPath("/file/java"); System.out.println(code); try { path = compileService.savaJava(code, path); if (path == null) { return false; } String ans = compileService.compileJava(path); System.out.println("ans"+ans); if (ans.length() > 1000) { ans = ans.substring(0, 1000)+"....."; } if (ans.trim().length() > 0) { return new Result(true, ans); } else { return new Result(true, "编译成功"); } } catch (Exception e) { e.printStackTrace(); return new Result(false, "编译失败"); } }
但是收到的code是base64编码之后的,于是将String类型的code改成对象类型。
@RequestMapping(value = "/compileJava", method = RequestMethod.POST) @ResponseBody public Object CompileJava(HttpServletRequest request, @RequestBody Src code) { String path = request.getSession().getServletContext().getRealPath("/file/java"); System.out.println(code.getCode()); try { path = compileService.savaJava(code.getCode(), path); if (path == null) { return false; } String ans = compileService.compileJava(path); System.out.println("ans"+ans); if (ans.length() > 1000) { ans = ans.substring(0, 1000)+"....."; } if (ans.trim().length() > 0) { return new Result(true, ans); } else { return new Result(true, "编译成功"); } } catch (Exception e) { e.printStackTrace(); return new Result(false, "编译失败"); } }
改完之后无法访问到这个接口了,参数不匹配,于是把前端ajax改成如下
$("#submitCode").click(function() { var code = editor.getValue(); $.ajax({ type : "POST", url: "compileJava", contentType: "application/json; charset=utf-8", dataType: "json", headers: { Accept: "application/json" }, data: JSON.stringify({code: code}), success : function(data) { alert(data.msg); $("#response").html(data.msg); } }); });
时间有限,需要加深了解springmvc的json自动转换机制。 http://blog.csdn.net/kobejayandy/article/details/12690555
相关文章推荐
- easyUI datagrid 取选中行id
- 文章标题POJ 2785:4 Values whose Sum is 0?(二分)
- Android官方开发文档Training系列课程中文版:管理系统UI之变暗系统条
- 秒懂设计模式(一): Builder模式
- 通讯录之手动型Segue和自动型Segue
- [leetcode] 375. Guess Number Higher or Lower II 解题报告
- Expression Tree Build
- LeetCode 376. Wiggle Subsequence(摇摆子序列)
- [leetcode] 376. Wiggle Subsequence 解题报告
- 解决SurfaceView调用setZOrderOnTop(true)遮挡其他控件的问题
- LeetCode 232. Implement Queue using Stacks
- LeetCode 62. Unique Paths
- LeetCode 225. Implement Stack using Queues
- Android适配——采用Values-dpi-wSize X hSize 模式,并分析原理
- LeetCode 303. Range Sum Query – Immutable
- LeetCode 347. Top K Frequent Elements
- LeetCode 63. Unique Paths II
- iOS 分类之UIColor+Hex
- LeetCode 96. Unique Binary Search Trees
- LeetCode 300. Longest Increasing Subsequence