java - Reusability in REST services with JAX-RS -


i have simple rest-like service, implemented in jax-rs maintain foo's. creates foos post foos, lists foos get foos , details given foo get foo/42 (where 42 given foo's id).

public class fooservice {    @post   @path("/foos")   public foo create(foo foo){     entitymanager.gettransaction().begin();     entitymanager.persist(foo);     entitymanager.gettransaction().commit();     return foo;   }    @get   @path("/foos")   public list<foo> get(){         list<foo> foos = //more-or-less generic jpa querying-code         return foos;   }    @get   @path("/foos/{id}")   public foo get(@pathparam("id") int id){         foo foo = //more-or-less generic jpa querying-code         return foo;   } } 

now, if have similar service maintaining bar's, how suppose elegantly avoid code repetition?

public class barservice {    @post   @path("/bars")   public bar create(bar bar){     entitymanager.gettransaction().begin();     entitymanager.persist(bar);     entitymanager.gettransaction().commit();     return bar;   }    @get   @path("/bars")   public list<bar> get(){         list<bar> bars = //more-or-less generic jpa querying-code         return bars;   }    @get   @path("/bars/{id}")   public bar get(@pathparam("id") int id){         bar bar = //more-or-less generic jpa querying-code         return bar;   } } 

the difference path values specified in @path annotations. since value should static (visible @ compile time) it's not possible create abstracservice class like:

public abstract class abstracservice<x> {    //abstrac static not possible   public abstract static final string path;    @post   @path(path)   public x create(x entity){ ... }    @get   @path(path)   public list<x> get(){ ... }    @get   @path(path + "/{id}")   public x get(@pathparam("id") int id){ ... } }   public class fooservice extends abstractservice<foo>{       //just override path "foos" }      public class barservice extends abstractservice<bar>{       //just override path "bars" }     

do need override each service method adjust @path , them call supers implementation?

the above classes fooservice , barservice way similar keep reusability-bell silent.

pretty common throughout resource classes pattern

@path("foo") public class foosresource {     @get  // `foo` resources     ...     @get      @path("{id}")  // `foo` id     ...     @post // create new `foo` in `foo` collection     ...     @put      @path("{id}")  // update `foo` id } 

you idea. point name of collection on resource class, instead of resource method level have it

@path("foo/{id}") 

you going need @path anyway make class resource class, why not use appropriate name it.

another thing might add do. add class field in abstract class, constructor arg can pass class to. in concrete class implementation, super(foo.class). way, jpa has access class, make easier work type querying.


Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

c# - SharpSsh Command Execution -

python - Specify path of savefig with pylab or matplotlib -