通过http向spring的controller传值总结,网上有很多,总结一下网上没有的:
1. get和post可用HttpServletRequest.getParameter接收参数;
2. post的json可用@RequestBody HashMap<String,String> json接收;
3. put在spring中有bug,不可用。
实例:
1.get
@RequestMapping(value = "/controller", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public class FormUrlencodedController {
private final static Log logger = LogFactory.getLog(FormUrlencodedController.class);
@RequestMapping(value = "/action", method = RequestMethod.GET)
public ResponseEntityWrapper get(HttpServletRequest req, HttpServletResponse res) {
ResponseEntityWrapper rew = new ResponseEntityWrapper();
String account = req.getParameter("account");//req.getParameter("account");
String password = req.getParameter("password");//req.getParameter("password");
return rew;
}
}
2.post
@RequestMapping(value = "/controller", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public class FormUrlencodedController {
@RequestMapping(value = "/action", method = RequestMethod.POST)
public ResponseEntityWrapper post(HttpServletRequest req, HttpServletResponse res) {
ResponseEntityWrapper rew = new ResponseEntityWrapper();
String account = req.getParameter("account");//req.getParameter("account");
String password = req.getParameter("password");//req.getParameter("password");
return rew;
}
}
3.put
Spring的RequestMethod.PUT方法接收不到参数
http://my.oschina.net/shellj/blog/392470
4.post Json
@Controller("MailConfigurationController")
@RequestMapping(value = "/email",produces = MediaType.APPLICATION_JSON_VALUE) //
public class MailConfigurationController {
private final static Log logger = LogFactory.getLog(MailConfigurationController.class);
@RequestMapping(value = "/account", method = RequestMethod.POST)
@ResponseBody
public ResponseEntityWrapper createEmailAccount(@RequestBody HashMap<String,String> json, HttpServletRequest req, HttpServletResponse res) {
ResponseEntityWrapper rew = new ResponseEntityWrapper();
String account =json.get("account");//req.getParameter("account");
String password = json.get("password");//req.getParameter("password");
return rew;
}
}
更多参考:http://www.zuidaima.com/share/1751862044773376.htm