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;
}
${validatedValue}as mentioned in this answer: stackoverflow.com/questions/74735952/…