Error submitting form using SpringMVC and REST
Created an online form in JSP using the SpringMVC tag libraries. The controller for my form is a RESTful web service.
A RESTful web service has two calls:
(1) http: // localhost: 8080 / myapp / applications / new
This calls the online form in the browser (it works).
(2) http: // localhost: 8080 / myapp / applications / create
This saves the form data to the database (handles submit). Here it breaks down.
The following are conventions from the sample experimental demo application that ships with the Spring Framework.
Online form:
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<body>
<form:form modelAttribute="application" method="POST" action="create">
<table>
<tr>
<td>Name:</td>
<td><form:input path="name" size="30" maxlength="80"/></td>
</tr>
<tr>
<td>Description:</td>
<td><form:input path="description" size="30" maxlength="80"/></td>
</tr>
<tr>
<td>Image URL:</td>
<td><form:input path="imgUrl" size="30" maxlength="80"/></td>
</tr>
</table>
<input type="submit" value="Save" />
</form:form>
</body>
</html>
RESTful web service that serves as a form controller:
@Controller
@Path(ApplicationsResource.APPLICATION_URL)
public class ApplicationsResource
{
private final Logger log =
LoggerFactory.getLogger(ApplicationsResource.class);
public static final String APPLICATION_URL = "/applications";
@Autowired
private ApplicationManager applicationManager;
@Autowired
private ProfileManager profileManager;
@POST
@Path("create")
@Produces(MediaType.TEXT_HTML)
public Model getNewApplication(@Context HttpServletRequest request,
@RequestAttribute Model model)
{
Application app = new Application();
model.addAttribute("application", app);
try
{
if ("POST".equalsIgnoreCase(request.getMethod()))
{
if (app != null)
{
applicationManager.save(app);
log.info("Added application: " + app.getName());
}
else
{
log.info("Application not added");
}
}
}
catch (Exception e)
{
log.info("Exception: ", e);
throw new
WebApplicationException(Response.status(
RestError.SERVER_ERROR_HTTP_RESP).
type("application/json;charset=utf-8").
entity(new ErrorOutput(RestError.SERVER_ERROR_CODE, RestError.SERVER_ERROR_MSG, e.toString())).build());
}
return model;
}
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder)
{
dataBinder.setDisallowedFields(new String[] {"id"});
}
@GET
@Path("new")
@Produces( { MediaType.TEXT_HTML })
public ModelAndView getNewApplicationForm()
{
log.info("ApplicationsResource - Inside getNewApplicationForm");
ModelAndView mv = new ModelAndView("/applications/applications_new");
mv.addObject("application", new Application());
return mv;
}
}
An exception is thrown when I click on submit:
Failed to POST / application / create org.jboss.resteasy.spi.BadRequestException:
Could not find post body reader for type:
interface org.springframework.ui.Model content type: application / x-www-form-urlencoded at
org.jboss.resteasy.core.MessageBodyParameterInjector $ 1 createReaderNotFound (MessageBodyParameterInjector.java:73)
Does anyone know why I am getting this exception?
It would be greatly appreciated if someone could help me with this problem ...
Happy programming and thank you for taking the time to read this.
source to share