i'm trying create jersey resources programatically (without annotations). have method raisealarm takes name , id input parameter. want take name json input , want id come path parameter. code looks thing this...
public class jerseyexample { public static void main(string[] args) { jerseyexample deployer = new jerseyexample(); deployer.init(); } public static class baseresource extends resourceconfig { public baseresource() { init(); } public void init() { try { resource.builder resourcebuilder2 = resource.builder(); resourcebuilder2.path("/raisealarm/{id}"); resourcemethod.builder method2 = resourcebuilder2.addmethod("post") .consumes(mediatype.application_json_type) .produces(mediatype.application_json_type) .handledby(this, this.getclass().getmethod("raisealarm", name.class, string.class)); resource childresource1 = resourcebuilder2.build(); resource.builder resourcebuilder = resource.builder(); resourcebuilder.path("/employee/status"); resourcebuilder.addchildresource(childresource1); resource rootresource = resourcebuilder.build(); registerresources(rootresource); } catch (exception e) { e.printstacktrace(); } } public string raisealarm(name notification,@pathparam("id") string id) { system.out.println("inside raise alarm "); system.out.println(notification.tostring() + " id: "+id); return "result"; } public void destroy() { } public static class name { string firstname; string lastname; string middlename; public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getlastname() { return lastname; } public void setlastname(string lastname) { this.lastname = lastname; } public string getmiddlename() { return middlename; } public void setmiddlename(string middlename) { this.middlename = middlename; } @override public string tostring() { return firstname + " " + middlename + " " + lastname; } } } public void init() { server server = new server(); servletcontexthandler context0 = new servletcontexthandler(servletcontexthandler.sessions); servletholder serveltholder1 = new servletholder(new servletcontainer(new baseresource())); context0.addservlet(serveltholder1, "/*"); context0.setvirtualhosts(new string[]{"@external"}); serverconnector connector = new serverconnector(server); connector.sethost("localhost"); connector.setport(9069); connector.setname("external"); handlercollection collection = new handlercollection(); collection.addhandler(context0); server.sethandler(collection); server.addconnector(connector); try { server.start(); server.join(); } catch (exception e) { e.printstacktrace(); } } }
the above code works. want know way in can declare path paramenters or query parameters programatically, can define method signature raisealarm(name notification,string id) , avoid @pathparam("id") annotation.
Comments
Post a Comment