Friday, October 14, 2016

Processing custom object from html form data of JSON from a POST request with JAX-RS

The main concept is, that if your Java bean class implements the Serializable interface than JAX-RS supports conversion from and to JSON out of the box.
So I would expect something like this to work:
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) // this is the "application/x-www-form-urlencoded" media type.
public void processFeedback(@FormParam("feedback") Feedback feedback) {
 this.feedback = feedback;
}
However, with @FormParam this does not seem to work for me.
I get this error:
Caused by: java.lang.RuntimeException: RESTEASY003875: Unable to find a constructor that takes a String param or a valueOf() or fromString() method for javax.ws.rs.FormParam("feedback") on public void my.app.FeedbackEndpoint.processFeedback(my.app.Feedback) for basetype: my.app.Feedback
The solution is to do implement one of the required ways of instantiating the class, so
  • a constructor that takes a String parameter, or
  • a static valueOf() method, or
  • a static fromString() method