Question

When I specify a @DefaultValue for my service when it is called without the provided parameter, why can it only be a String, and not any other type? The @DefaultValue(0) below throws an error "Cannot convert from int to String" - What if I want the default value to be another type?

@POST
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/createAccount")
public BankActivity createAccount(
        @QueryParam("ownerId") @DefaultValue(0) int ownerID,
        @QueryParam("accountPin") @DefaultValue("1234") String pin,
Was it helpful?

Solution

The @DefaultValue represents a default value for a form parameter, query parameter, cookie, and a few others, as described in the javadoc. These various parameters and values are sent as part of the request. The http server receives them as Strings and your jax-rs container parses them and converts them appropriately. @DefaultValue provides the value as a string and let's the container convert it as it would with a provided value.

So just provide the string representation of the int value and the container will make the correct conversion

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top