0

I wrote a DTO class to pass certain values to an endpoint. To sanitize the passed parameters I am using spring boot starter validation but I would like the error message handled by a Handler to return a message containing the value that was used as input.

Can this be done directly or do I have to implement special methods?

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
public class ForecastWeatherParametersDTO {

    @NotNull
    @Max(value = 90, message = "Latitude must be in range of -90 to 90°. Given: {INSERT_GIVEN_VALUE}.")
    @Min(value = -90, message = "Latitude must be in range of -90 to 90°. Given: {INSERT_GIVEN_VALUE}.")
    private Double latitude;

    @NotNull
    @Max(value = 180, message = "Longitude must be in range of -180 to 180°. Given: {INSERT_GIVEN_VALUE}.")
    @Min(value = -180, message = "Longitude must be in range of -180 to 180°. Given: {INSERT_GIVEN_VALUE}.")
    private Double longitude;

    @Builder.Default
    private List<HourlyForecastVariablesEnum> hourly = new ArrayList<>();
    @Builder.Default
    private List<DailyForecastVariablesEnum> daily = new ArrayList<>();
    @Builder.Default
    private List<ModelsForecastEnum> models = List.of(ModelsForecastEnum.BEST_MATCH);

    // setting
    @JsonProperty("past_days")
    @Builder.Default
    @Max(value = 92, message = "Past days is invalid. Allowed range 0 to 92. Given {INSERT_GIVEN_VALUE}.")
    @Min(value = 0, message = "Past days is invalid. Allowed range 0 to 92. Given {INSERT_GIVEN_VALUE}.")
    private Integer pastDays = 0;

    @JsonProperty("future_days")
    @Builder.Default
    @Max(value = 16, message = "Forecast days is invalid. Allowed range 0 to 16. Given {INSERT_GIVEN_VALUE}.")
    @Min(value = 0, message = "Forecast days is invalid. Allowed range 0 to 16. Given {INSERT_GIVEN_VALUE}.")
    private Integer futureDays = 7;

    @JsonProperty("temperature_unit")
    @Builder.Default
    private TemperatureUnitEnum temperatureUnit = TemperatureUnitEnum.CELSIUS;

    @JsonProperty("windspeed_unit")
    @Builder.Default
    private WindSpeedUnitEnum windSpeedUnit = WindSpeedUnitEnum.KMH;

    @JsonProperty("precipitation_unit")
    @Builder.Default
    private PrecipitationUnitEnum precipitationUnit = PrecipitationUnitEnum.MM;

    @JsonProperty("time_format")
    @Builder.Default
    private TimeFormatEnum timeFormat = TimeFormatEnum.ISO8601;

    @Builder.Default
    @JsonProperty("time_zone")
    private TimeZoneEnum timeZone = TimeZoneEnum.GMT0;

}
1

1 Answer 1

2

This can be done with the Unified Expression Language. Example based on your sample code:

@Max(value = 16, message = "Forecast days is invalid. Allowed maximum of {value}. Given ${validatedValue}.")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.