jax-rsjoobyjavax.inject

Jooby Query Param Value


Here is the resource class as the earlier dependency is Jooby. How to add a new query param value which can by default false if not passed and 1st parameter is mandatory

import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue;
import org.jooby.Result;
import org.jooby.mvc.GET;
import org.jooby.mvc.Header;
import org.jooby.mvc.Local;
import org.jooby.mvc.POST;
import org.jooby.mvc.Path; 
import javax.inject.Named;

@Path("/extract")
public class ExtractResource{

@GET
public Result getResponse(@Named("Key1") final String Key1, 
@DefaultValue("false") @QueryParam("key2") Boolean  
key2){
  return response;
}
}

Here I want to introduce a new query param value key2 default value is false. How to add the dependency between javax.ws.rs and jooby. I added the javax Query param But I want key1 to be mandatory and key2 when only passed gives the response based on that

Solution

  • You can do something like this:

    public class ExtractResource{
        
        @GET
        @Path("/extract/:key1)
        public Result getResponse(String Key1, Optional<Boolean> key2) {
          return response;
        }
    }
    

    Where your mandatory parameter key1 is a path parameter and your query parameter is key2.

    Remember that the parameters passed in the URL must have the same name with the method parameters otherwise use the @Named attribute to specify the correct name.