What framework should we use to build a Java REST API?

3,429 views
Skip to first unread message

Peter Dietz

unread,
May 17, 2013, 12:26:36 PM5/17/13
to dspac...@googlegroups.com
So, build a REST API for DSpace, easier said than done. My first problem in getting started is what technology framework should we use to do this? I've been looking into what exists in Java-land, and there are plenty of options, but I haven't yet struck gold, in terms of finding something that will help us accomplish what we're trying to do. My problems are that many technologies look very complex, look hard to get started, or seem too enterprisey and overloaded with @annotations that its going to give us more clumsy code.

So, I could use some advice on which frameworks that anyone has heard of, is familiar with, or thinks would be a good fit to use. Bonus points for pointing to code samples, or getting started guides.

Peter Dietz

unread,
May 17, 2013, 12:33:25 PM5/17/13
to dspac...@googlegroups.com
Restlet, is one technology that I have been looking into, and following its getting started guide. It seems promising, and once I'm over the learning curve, it might fit.

Its got a getting started guide: http://restlet.org/discover/firststeps

Thus far, (development-grade) getting started goes like:
    public static void main(String[] args) throws Exception {
        Server mailServer = new Server(Protocol.HTTP, 8111);
        mailServer.setNext(new MailServerApplication());
        mailServer.start();
    }


Then define your routes:
        Router router = new Router(getContext());
        String base = "http://localhost:8111";
        router.attach(base + "/",                      RootServerResource.class);
        router.attach(base + "/accounts/",             AccountsServerResource.class);
        router.attach(base + "/accounts/{accountId}",  AccountServerResource.class);

        return router;


Make interfaces for your resources. 
public interface AccountResource {

    @Get("txt")
    public String represent();

    @Put("txt")
    public void store(String account);

    @Delete
    public void remove();
}


Then, your concrete implementation, to actually do stuff:
public class AccountServerResource extends ServerResource implements AccountResource {
    private int accountId;

    @Override
    protected void doInit() {
        this.accountId = Integer.parseInt(getAttribute("accountId"));
    }

    @Override
    public String represent() {
        return AccountsServerResource.getAccounts().get(this.accountId);
    }

    @Override
    public void store(String account) {
        AccountsServerResource.getAccounts().set(this.accountId, account);
    }

    @Override
    public void remove() {
        AccountsServerResource.getAccounts().remove(this.accountId);
    }

Peter Dietz

unread,
May 17, 2013, 2:33:30 PM5/17/13
to dspac...@googlegroups.com
Jersey. The "reference implementation" of JAX-RS 1. 

Richard has set it up for MDS REST endpoint. 
https://github.com/richardrodgers/mds/tree/master/rest

      <dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.11</version>
      </dependency>


I haven't gotten around to using it, so I can't speak to how friendly it is. 

Basically, it looks like your free to spit out some annotations next to methods, and then they automatically get wired up to respond to that endpoint (path / method).
@Path("/rest")
@Produces({APPLICATION_XML, APPLICATION_JSON})
public class CurationResource {
...
 
@GET @Path("tasks")
 
public List<Task> getTasks() {
     
return taskDao.getTasks();
 
}


 
@GET @Path("taskgroups")
 
public List<TaskGroup> getTaskGroups() {
     
return taskDao.getTaskGroups();
 
}


 
@GET @Path("selectors")
 
public List<Selector> getSelectors() {
     
return selectorDao.getSelectors();
 
}


 
@GET @Path("selectorgroups")
 
public List<SelectorGroup> getSelectorGroups() {
     
return selectorDao.getSelectorGroups();
 
}


 
@POST @Path("dso/{dsoId}")
 
public Curation curateDso(@Context SecurityContext sec, @PathParam("dsoId") String dsoId, CurationOrder order) {
     
Curation curation = initCuration(dsoId, "dso", sec, order);
     
return curate(curation);
 
}




On Friday, May 17, 2013 12:26:36 PM UTC-4, Peter Dietz wrote:

Richard Rodgers

unread,
May 17, 2013, 3:43:26 PM5/17/13
to dspac...@googlegroups.com
Yes, for me the important question (in this regard) is whether to look for a JAX-RS implementation or something else. There are a bunch of pretty good implementations besides Jersey: Apache CXF, JBoss RESTEasy, Restlet, just to name the major ones.

If we decide on JAX-RS, then we can almost defer the question of the best library: at the protocol level they are interchangeable.

FWIW, I think JAX-RS is pretty good to work with, and the 2.0 spec has just been approved, which will tighten up the differences among the implementers, and gives some nice additional features.

Richard
Reply all
Reply to author
Forward
0 new messages