Spring-mvc:服务器拒绝了此请求,因为请求实体的格式不受所请求方法所请求资源的支持

时间:2016-12-02 18:40:56

标签: java spring-mvc

我已经开始使用spring MVC了。我检查了herehere以及here它似乎无法使用RequestBody属性。使用@ModelAttribute它工作正常。我在申请中出错的地方?

这是我的register.jsp

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<html lang="en">
<head>
 <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="application/json; charset=UTF-8" />
</head>
<body>

<h2>Login</h2>
<form:form  method="post"
                modelAttribute="userForm" action="/assignment/login">
 <spring:bind path="userName">
 <label>Name</label>
 <form:input path="userName" type="text"  class="form-control" id="name" placeholder="Name" />
 </spring:bind>
      <br>         
<spring:bind path="email">
<label class="col-sm-2 control-label">Email</label>
<form:input path="email" class="form-control" id="email" placeholder="Email" />
</spring:bind>
<br>
<spring:bind path="dob">
<label class="col-sm-2 control-label">Date of Birth(dd-mm-yyyy)</label>
<form:input path="dob" class="form-control" id="email" placeholder="Date of birth" />
</spring:bind><br>
<input type="submit" value="save">
 </form:form>
</body>
</html>

这是我的LoginController.java

package assignment.view;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import assignment.model.User;

@Controller
@RequestMapping("/login")
public class LoginController {

@RequestMapping(method=RequestMethod.POST,headers="content-type=application/json")
public String userLogin(@RequestBody User user){
    System.out.println(user);
    return "success";
}

@RequestMapping(method=RequestMethod.GET)
public String register(Model model){
    User user=new User();
    model.addAttribute("userForm", user);
    return "Register";
}
}

0 个答案:

没有答案