Aru*_*mar 10 rest spring spring-mvc
我正在使用Spring 3.1为iPhone和Android应用程序创建Restful网站和Web服务.在我的应用程序中,我使用Spring Message Convertors(org.springframework.http.converter.json.MappingJacksonHttpMessageConverter)将JSON转换为Java对象,反之亦然.
我的目标是JSP页面,Iphone/Andois app应该只使用单个控制器方法(相同的URL).
我使用Spring表单标签进行从JSP到控制器的对象绑定,@ModelAttribute如下所示.
@RequestMapping(value = "reset-password", method = RequestMethod.POST)
public ModelAndView resetPassword(@ModelAttributeForgot forgotPassword,
HttpServletRequest request) {
System.out.println("data recived=="+forgotPassword.getNewPassword());
}
Run Code Online (Sandbox Code Playgroud)
但是,如果数据是从iPhone/Android应用程序发布的,那么同样的情况就不适用了,结果是:
recived = = null;
所以为了克服这个问题,我@RequestBody在地方使用了注释@ModelAttribute.
所以我的控制器如下所示:
@RequestMapping(value = "reset-password", method = RequestMethod.POST)
public ModelAndView resetPassword(@RequestBody Forgot forgotPassword,
HttpServletRequest request) {
System.out.println("data recived=="+forgotPassword.getNewPassword());
}
Run Code Online (Sandbox Code Playgroud)
它的工作原理,我得到的结果是:
收到的数据== somedata;
但是@RequestBody然后在JSP页面上不能使用spring表单,并且数据不会转换为对象而且我得到了null值.
@RequestBody注释以JSP形式的弹簧形式标签以JSON的形式发布数据吗?编辑:
在写String到位Bean类的,我能够获得纯文本格式的内容,如下图所示:
@RequestMapping(value = "reset-password", method = RequestMethod.POST)
public ModelAndView resetPassword(@RequestBody String string,
HttpServletRequest request) { }
Run Code Online (Sandbox Code Playgroud)
网页通话结果:
UID = 11&confirmPassword = somepassword&NEWPASSWORD = somepassword
iPhone使用Web服务调用的结果(**JSON)**
{"newPassword":"somepassword","confirmPassword":"somepassword","uid":"11"}
Run Code Online (Sandbox Code Playgroud)
但问题是使用这种方法我必须手动将JSON字符串解析为Java对象.在网页内容中,我必须手动找到我不想要的值.
请帮忙.
问候,
阿伦库马尔
抱歉,但我不相信有办法,因为 @ModelAttribute 是从表单 post 参数绑定的,而 @RequestBody 将正文直接传递给 Json 转换器。您可以用简单的 json post 替换 spring 表单标记,但这可能不如使用两个 @RequestMapping 方法方便。
| 归档时间: |
|
| 查看次数: |
22614 次 |
| 最近记录: |