JSR-311 with Spring fragment

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.

Tags:

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