Hi Pavel
Thanks for your response.. But I want to declare my method signature as
raiseAlarm(Name notification,String id) and avoid annotations all together.
I was wondering if there is some api where we can map query or path
parameters like we map 'consumes', 'produces','path' etc parameters.
Regards
Rajat.
On 28-Jan-2016 6:01 PM, "Pavel Bucek" <pavel.bucek_at_oracle.com> wrote:
> Hi Rajat,
>
> you can replace @PathParam with @Context UriInfo info and then call
>
> info.getPathParameters()
>
> (see
> https://jersey.java.net/apidocs/latest/jersey/javax/ws/rs/core/UriInfo.html#getPathParameters())
>
> which will return Map<String, String>; key is path param name, value is
> its value.
>
> Hope it helps,
> Pavel
>
> On 28/01/16 07:42, rajat shukla wrote:
>
> I'm trying to create jersey resources programatically (without
> Annotations). I have a method raiseAlarm that takes a Name and id as input
> parameter. I want to take Name from the JSON input and I want id to come
> from path parameter. The code looks some thing like this...
> {code}
>
> 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,_at_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();
> }
> }
> }
>
>
> {code}
>
> The above code works. I want to know a way in which I can declare the Path
> paramenters or Query parameters programatically, so that I can define my
> method signature as raiseAlarm(Name notification,String id) and avoid
> @PathParam("id") annotation.
>
> Regards,
> Rajat.
>
>
>