Spring Controller接收http传值

2 篇文章 0 订阅
1 篇文章 0 订阅

通过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

SpringRequestMethod.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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值