使用JAX-RS UriInfo时@DefaultValue失效,searchCount返回null求助
Ah, I see the issue here! The problem is that you’re mixing two approaches to handling request parameters in JAX-RS, and the @DefaultValue annotation isn’t working because you’re bypassing JAX-RS’s built-in parameter injection mechanism. Let me break this down and fix it for you.
Why searchCount is always null
When you use uriInfo.getQueryParameters().getFirst(SEARCH_COUNT), you’re directly pulling the raw query parameter from the request. JAX-RS doesn’t automatically apply the @DefaultValue or @Size annotations you added to the searchCount() method—those annotations only work when JAX-RS handles parameter injection for you (via @QueryParam, @PathParam, etc.), not when you manually fetch parameters from UriInfo.
Fix 1: Let JAX-RS handle parameter injection (recommended)
The cleanest solution is to let JAX-RS inject your query parameters directly, either via the constructor or resource method parameters. This way, @DefaultValue and @Size will work as expected without any manual handling.
Here’s how to adjust your wrapper class:
private final String longitude; private final String latitude; private final String range; private final String eventType; private final String placeType; private final String searchCount; public NearByRequestWrapper( @QueryParam("lon") String longitude, @QueryParam("lat") String latitude, @QueryParam("range") @DefaultValue("10000") String range, @QueryParam("eType") @DefaultValue("") String eventType, @QueryParam("pType") @DefaultValue("") String placeType, @QueryParam("count") @DefaultValue("50") @Size(min = 10, max = 100) String searchCount) { this.longitude = longitude; this.latitude = latitude; this.range = range; this.eventType = eventType; this.placeType = placeType; this.searchCount = searchCount; } // Getters if you need them public String getSearchCount() { return searchCount; }
Now JAX-RS will automatically:
- Use the default value for
countif it’s missing from the request - Validate that
searchCountfalls between 10 and 100 (throwing a validation error if it doesn’t)
Fix 2: Manual default value + validation (if you must use UriInfo)
If you absolutely need to use UriInfo for parameter retrieval, you’ll have to manually apply the default value and handle validation yourself. You can still use the @Size annotation, but you’ll need to use Bean Validation’s Validator to enforce it:
import javax.validation.Validator; import javax.validation.ConstraintViolation; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; import java.util.Set; private final UriInfo uriInfo; private final Validator validator; private final String SEARCH_COUNT = "count"; public NearByRequestWrapper(UriInfo uriInfo, Validator validator) { this.uriInfo = uriInfo; this.validator = validator; } public String searchCount() { // Fetch raw parameter or use default value String count = uriInfo.getQueryParameters().getFirst(SEARCH_COUNT); String value = count != null ? count : "50"; // Manually validate the value against @Size Set<ConstraintViolation<String>> violations = validator.validate(value); if (!violations.isEmpty()) { throw new WebApplicationException( Response.status(Response.Status.BAD_REQUEST) .entity("searchCount must be between 10 and 100") .build() ); } return value; }
This approach works, but it’s more verbose and bypasses JAX-RS’s native parameter handling—so the first fix is almost always better.
内容的提问来源于stack exchange,提问作者Eslam Hamdy

