java - RESTEasy - Reuse method parameters when @GET/@POST -
i have method in resteasy service i'd use both @get/@post, , data may come both query string , request body.
@get @post public string mymethod(@queryparam("param1") @formparam("param1") string param1, @queryparam("param2") @formparam("param1") string param2) { // ...do things } however, haven't found yet way without doing following:
@get public string mymethod(@queryparam("param1") string param1, @queryparam("param2") string param2) { // ...do things } @post public string mymethod2(@formparam("param1") string param1, @formparam("param2") string param2) { return this.mymethod(param1, param2); } does knows how make first example work, or approach least code possible?
quoting book restful java jax-rs:
jax-rs defines 5 annotations map specific http operations:
@javax.ws.rs.get@javax.ws.rs.put@javax.ws.rs.post@javax.ws.rs.delete@javax.ws.rs.head(...)
@getannotation instructs jax-rs runtime java method process http requests uri. use 1 of other 5 annotations described earlier bind different http operations. one thing note, though, may apply 1 http method annotation per java method. deployment error occurs if apply more one.
(the text above written creator of resteasy.)
and, in short, resteasy complies jax-rs, can't annotate method more 1 http verb.
if not convinced, looking @ @get annotation, can see meta-annotation @httpmethod.
/** * indicates annotated method responds http requests * @see httpmethod */ @target({elementtype.method}) @retention(retentionpolicy.runtime) @httpmethod(httpmethod.get) public @interface { } and if open @httpmethod, check javadoc (it error method annotated more 1 annotation annotated httpmethod.):
/** * associates name of http method annotation. java method annotated * runtime annotation annotated annotation * used handle http requests of indicated http method. error * method annotated more 1 annotation annotated * {@code httpmethod}. * * @see * @see post * @see put * @see delete * @see head */ @target({elementtype.annotation_type}) @retention(retentionpolicy.runtime) @documented public @interface httpmethod { so, that's it, can't have them both in same method.
that said, could, if really must, achieve through preprocessinterceptor called before jax-rs method.
still, way more complicated (as you'd have parse parameters yourself) , less maintainable (services being delivered @ interceptors!?).
bottom line, knowledge, solution optimal.
check i'm saying in test below:
public class queryandformparamtest { @path("/") public static class interceptedresource { @get //@path("/stuff") // uncomment , not work public string otherservice(@queryparam("yadda") string name){ return "im never called in example" + name; } } public static class myinterceptor implements preprocessinterceptor, acceptedbymethod { @override public boolean accept(class declaring, method method) { system.out.println("accepted method "+method.getname()); // can check if interceptor should act on method here return true; // it'll act everytime } @override public serverresponse preprocess(httprequest request, resourcemethod method) throws failure, webapplicationexception { // parsing form parameters if (request.gethttpheaders().getmediatype() != null && request.gethttpheaders().getmediatype().iscompatible(mediatype.valueof("application/x-www-form-urlencoded"))) { multivaluedmap<string, string> formparameters = request.getformparameters(); if (formparameters != null) { (string key : formparameters.keyset()) { system.out.println("[form] "+key + ": "+formparameters.get(key)); } } } // parsing query parameters multivaluedmap<string, string> queryparameters = request.geturi().getqueryparameters(); if (queryparameters != null) (string key : queryparameters.keyset()) { system.out.println("[query] "+key + ": "+queryparameters.get(key)); } string responsetext = "do something: " + request.geturi().getqueryparameters().getfirst("test"); return new serverresponse(responsetext, 200, new headers<object>()); } } @test public void test() throws exception { dispatcher dispatcher = mockdispatcherfactory.createdispatcher(); dispatcher.getproviderfactory().getserverpreprocessinterceptorregistry().register(new myinterceptor()); dispatcher.getregistry().addsingletonresource(new interceptedresource()); mockhttprequest request = mockhttprequest.get("/?test=somestuff"); mockhttpresponse response = new mockhttpresponse(); dispatcher.invoke(request, response); system.out.println(response.getcontentasstring()); assert.assertequals("do something: somestuff", response.getcontentasstring()); } }
Comments
Post a Comment