Tuesday, April 19, 2016

Java Spring, JPA Useful materials, problem-solution

1. JPA Tutorial
http://www.java2s.com/Tutorials/Java/JPA/index.htm

2. Spring RestController doesn't set parameter for PUT request
FROM:
http://stackoverflow.com/questions/29017593/spring-4-restcontroller-not-receiving-data-from-put-request
This is a limitation of the Servlet Spec and the inner workings of Spring for populating model attributes.
First, the spec says
3.1.1 When Parameters Are Available
The following are the conditions that must be met before post form data will be populated to the parameter set:
  1. The request is an HTTP or HTTPS request.
  2. The HTTP method is POST.
  3. The content type is application/x-www-form-urlencoded.
  4. The servlet has made an initial call of any of the getParameter family of methods on the request object. If the conditions are not met and the post form data is not included in the parameter set, the post data must still be available to the servlet via the request object's input stream. If the conditions are met, post form data will no longer be available for reading directly from the request object's input stream.
Second, your handler methods' second parameter, the one of type Note, is actually considered a model attribute, as if it was implicitly annotated with @ModelAttribute. As such, it is processed by Spring's ModelAttributeMethodProcessor. This HandlerMethodArgumentResolver uses the getParameter (and its family of methods) for populating the created instance's fields.
Since this is a PUT request, the parameters are not retrievable through getParameter. However, they are still accessible through the request body, but Spring doesn't go there for model attributes.
You can do the conversion yourself. But I suggest you change your request content to JSON or XML for PUT and use @RequestBody annotated parameters.