I'm Solomon Duskis, NYC consultant and a Java/J2EE guy. I work at Sungard Consulting Services in NYC. The postings on this site are my own and do not necessarily represent the positions, strategies or opinions of my employer. Solomon is a DZone MVB and is not an employee of DZone and has posted 22 posts at DZone. You can read more from them at their website. View Full User Profile

RESTEasy's @Form

01.11.2011
| 6175 views |
  • submit to reddit

@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.

 

From http://www.jroller.com/Solomon/entry/resteasy_s_form

Published at DZone with permission of Solomon Duskis, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Chris Boersma replied on Tue, 2011/01/11 - 9:06pm

Just wondering why you'd want to integrate this with Spring when Spring 3 already has automatic form binding? Looks interesting nonetheless

Solomon Duskis replied on Wed, 2011/01/12 - 1:43pm in response to: Chris Boersma

MVC in general has some great use cases for injection of Request scoped beans into Singleton controllers.  Spring MVC doesn't have that feature, so I thought I'd build it into a different MVC framework to see the usefulness of the idea.

George Valotas replied on Sun, 2011/01/16 - 3:55pm

Jersey (at least the latest one I've checked: 1.5) has the InjectParam annotation which does what the form annotation does for Resteasy.

Solomon Duskis replied on Sat, 2011/01/22 - 10:21pm in response to: George Valotas

George, can you please point me at some additional information about @InjectParam?  Everything I found points to it being similar in nature to @Autowired in Spring rather than RESTEasy's @Form. 

Solomon Duskis replied on Sat, 2011/01/22 - 10:23pm in response to: Chris Boersma

It looks like my first answer wasn't really addressing the question... I was confusing this post with another post of mine.  Spring's Form binding relates to POST form data (or query parameters), not other types of binding, as far as I know.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.