RESTEasy's @Form
Join the DZone community and get the full member experience.
Join For Free@Form is one of RESTEasy's best features. It encapsulates any set of inputs - path params, query params, form params, headers, cookies and PUT/POST body. NOTE: @Form does not ONLY encapsulate HTML Form input, although it can as described in my article about integrating RESTEasy with Spring MVC
Here's how it's used in an application:
public class MyForm { @FormParam("stuff") private int stuff; @HeaderParam("myHeader") private String header; @PathParam("foo") public void setFoo(String foo) {...} } @POST @Path("/myservice") public void post(@Form MyForm form) {...}
I've had HTTP input reuse in every big HTTP application that I've built in 3 organizations. 2 of the organizations used RESTEasy, and the results were a lot cleaner than the 3rd org with the use of @Form than with some of the "reuse hacks" I've had to do when using Jersey. Here are some other benefits I've had from @Form:
- Input processing Logic: custom conversion, multi-variable processing (if param X isn't there, then param Y should be used in conjunction with header Z).
- Content based security
- Integration with JSR 303/Bean Validation (you can't use it unless you have a bean...). The processing happened via a RESETEasy interceptor. IMHO, the end result was elegant and simple.
I really hope that this feature gets into JAX-RS 2.0, although I think that the name would have to be more expressive than @Form.
Opinions expressed by DZone contributors are their own.
Comments