0

I have started with spring MVC. I have checked here and here and here it seems it didn't work with RequestBody attribute. With @ModelAttribute it is working fine. Where I am going wrong in my application?

Here's my 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>

and here's my 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";
}
}
Community
  • 1
  • 1
LowCool
  • 1,187
  • 5
  • 25
  • 51
  • what exactly are you calling? the error message smells like you're trying to hit an endpoint with the wrong content-type. – matias elgart Dec 02 '16 at 18:43
  • I tried to hit http://localhost:8080/assignment/login end point. which will show my Register.jsp clicking on save button. It will post the data and get Success.jsp – LowCool Dec 03 '16 at 06:32
  • and yes I understood the issue by seeing the error. but I didn't get why it is occuring? – LowCool Dec 03 '16 at 06:43
  • try hitting your endpoint, but be very explicit about your content type (application/json) using headers. – matias elgart Dec 03 '16 at 13:11
  • but I have given on the top of it right??as content type! – LowCool Dec 05 '16 at 15:15
  • well, how are you making your calls _to_ your endpoint? using 'curl' perhaps? try adding application/json headers to your client that's making the login request. hack! try: `curl -H "Content-Type: application/json" localhost:8080/assignment/login -d {}` – matias elgart Dec 05 '16 at 15:43
  • No I am not using curl here its simple spring MVC project – LowCool Dec 05 '16 at 15:53

0 Answers0