This post originated from an RSS feed registered with Java Buzz
by Bill de hÓra.
Original Post: JSR-311 with Spring fragment
Feed Title: Bill de hÓra
Feed URL: http://www.dehora.net/journal/atom.xml
Feed Description: FD85 1117 1888 1681 7689 B5DF E696 885C 20D8 21F8
Latest Java Buzz Posts
Latest Java Buzz Posts by Bill de hÓra
Latest Posts From Bill de hÓra
Advertisement
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 .
Read: JSR-311 with Spring fragment