Handling a path based URL in Java, with Jersey, Spring2.5 and StringTemplate:
@Path("users") @Singleton
public class SingletonUserResourceWithSpring
{
private static final String OPENSOCIAL_MEDIATYPE = "application/atom+xml";
@Inject private UserAccountService userAccountService;
private StringTemplateGroup templates;
public SingletonUserResourceWithSpring() {
templates = new StringTemplateGroup("templates");
}
@GET @Path("/{uid}")
@Produces(OPENSOCIAL_MEDIATYPE)
public String getByUID(@PathParam("uid") String uid) {
UserAccount account = userAccountService.findByUID(uid);
return renderToResponse(account);
}
private String renderToResponse(UserAccount account) {
StringTemplate userxml = loadTemplate("templates/user_opensocial");
userxml.setAttribute("accountName", account.getAccountName());
userxml.setAttribute("uid", account.getUid());
userxml.setAttribute("email", account.getEmail());
userxml.setAttribute("firstName", account.getFirstName());
userxml.setAttribute("lastName", account.getLastName());
userxml.setAttribute("render_summary_flag", "yes");
return userxml.toString();
}
private StringTemplate loadTemplate(String template) {
return templates.getInstanceOf(template);
}
}
That's not bad. It could be even tighter in Groovy. Tip of the hat to javakaffee and Paul Sandoz.
5 Comments
Yes, having configured restlet applications in spring and in groovy, I much prefer groovy. The entire application is just groovy. But don't use the groovy restlet integration linked from the restlet site. That requires... Spring!
JSR 311, indeed does rock!
Paul Sandoz is a very smart man.
you are really nice man.. thanks ..
JSR 311, indeed does rock!
[url="http://www.uggsupply.com"]uggs[/url], with a legendary brand, first to see the snow ugg boots ,Ugg people will not Ben Ben flu cartoon form, and is [url="http://www.uggfree.co.uk"]boots[/url], as a result of many European and American film star has adequate Gordon Street Ugg snow boots and a pretty popular in Europe and America look like the earth, Ugg blowing sustained winds of the popular Madden, in Japan, Taiwan has a lot of fans Ugg.
http://www.airjordanmart.com
http://www.headphonesky.com
http://www.up2heels.com
http://www.toolsinhair.com/
http://www.aceghd.com
http://www.hotsoftshop.com
http://www.goodsbox.com
http://www.shoppingtiffany.com
http://www.topbootsmart.com
http://www.tiffanysale.co.uk/
http://www.jewelrygucci.com
http://www.ustiffanyjewelry.com
http://www.bestguccishoes.com/
http://www.gucci-stores.com/
http://www.uktiffanyjewellery.co.uk
http://www.handbagon.com/
http://www.louisvuittonfr.com/
http://www.tiffanygifts.co.uk
http://www.wmbags.com/
http://www.chanel-jewelry.com