Spring MVC出现POST 400 Bad Request &405 Request method 'GET' not supported

首先描述一下出现错误的情景:
我刚学springmvc,想做一个登录界面的东西。然后试着写了一个controller如下:
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login( String name,
String password, Model model) {
User u = userService.login(name, password);
if (u == null)
throw new UserException("no user exist!");
if (!u.getPassword().equals(password)) {
throw new UserException("password is not right!");
}
model.addAttribute("loginUser", u);
return "redirect:/user/users";
}

然后就出现了 400 badrequest这个错误了。网上找资料,说是属性不匹配吧,于是找jsp页面的错误,没有不匹配的地方,于是添加
log4j.logger.org.springframework.web=debug到log4j日志里面,果然出现错误的地方了。究其原因是

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String show(@PathVariable int id, Model model) {
        model.addAttribute("user", userService.load(id));
        return "user/show";
    }

因为这个mapping地址localhost:8080/xxx/user/id(int类型)的,
login的mapping地址localhost:8080/xxx/user/login(String类型)的,
所以在dispatch的时候,spring误认为是要去访问show这个方法,就造成了数据类型不匹配,也就是String型无法转变为int类型的错误,也就造成了400 bad request错误了,如此,解决方法就是在show方法的
@Request

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值