You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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.

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 count if it’s missing from the request
  • Validate that searchCount falls 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:00:10