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

Dropwizard:如何使JSON中的Integer类型参数为必填项

Troubleshooting Missing Validation for hotel_id in Dropwizard POST Requests

Hey there! Let's dig into why your hotel_id field isn't throwing an error when missing, while extranet_request_id behaves as expected. This is a common gotcha with Hibernate Validator in Dropwizard, so let's break down the most likely fixes:

1. Double-Check Validation Annotations on hotel_id

First, make sure you've applied the correct validation annotation to hotel_id. The most common issue here is forgetting to mark the field as required:

  • For string fields: Use @NotBlank (checks for non-null and non-empty/whitespace) or @NotNull + @NotEmpty
  • For numeric types (like Integer): Use @NotNull (note: if you're using primitive int, switch to Integer—primitive types can't be null, so @NotNull won't trigger for missing fields!)

Example of a properly annotated class:

public class BookingRequest {
    @NotNull(message = "extranet_request_id is required")
    private String extranet_request_id;

    @NotNull(message = "hotel_id is required") // Critical for missing field checks
    private Integer hotel_id; // Use Integer instead of int to support null validation

    // Don't forget getters/setters! Hibernate Validator needs access to the fields
    public String getExtranet_request_id() {
        return extranet_request_id;
    }

    public void setExtranet_request_id(String extranet_request_id) {
        this.extranet_request_id = extranet_request_id;
    }

    public Integer getHotel_id() {
        return hotel_id;
    }

    public void setHotel_id(Integer hotel_id) {
        this.hotel_id = hotel_id;
    }
}

2. Ensure @Valid is Applied to the Request Parameter

In your Dropwizard resource class, you must add the @Valid annotation to the request body parameter to trigger validation. Without this, Hibernate Validator won't run checks at all:

@Path("/bookings")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class BookingResource {

    @POST
    public Response createBooking(@Valid BookingRequest request) {
        // Your business logic here
        return Response.status(Response.Status.CREATED).build();
    }
}

3. Verify Field Accessibility

Hibernate Validator needs to access your fields to validate them. If hotel_id is marked as private without a getter method, the validator might skip it. Either:

  • Make the field public, or
  • Add a public getter method for hotel_id (like in the example above)

4. Check for Nested Object Validation (If Applicable)

If hotel_id is part of a nested object inside your request class, you need to add @Valid to the nested field to cascade validation:

public class BookingRequest {
    @NotNull
    private String extranet_request_id;

    @Valid // Ensures validation runs on the nested HotelDetails object
    @NotNull
    private HotelDetails hotelDetails;

    // ... getters/setters
}

public class HotelDetails {
    @NotNull(message = "hotel_id is required")
    private Integer hotel_id;
    // ...
}

5. Confirm Dropwizard Validation is Enabled

While this is less likely (since extranet_request_id works), double-check your config.yml to ensure validation isn't disabled:

server:
  validation:
    enabled: true # This is the default, but it's worth confirming

Once you've checked these points, test your POST request again with a missing hotel_id—it should now throw a validation exception just like extranet_request_id.

内容的提问来源于stack exchange,提问作者alexlipa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:29:23