I want to make my query param class immutable (with public final fields set via constructor). Is there a way to enforce SearchQueryParam instance creation via constructor and do not expose dreadful getters/setters?
Here is sample code which works:
@RequestMapping(value = "/search", method = GET, produces = APPLICATION_JSON_VALUE)
public List<Result> search(SearchQueryParam searchQueryParam) {
//do stuff;
}
public class SearchQueryParam {
@DateTimeFormat(iso = DATE_TIME)
private DateTime from;
@DateTimeFormat(iso = DATE_TIME)
private DateTime to;
public DateTime getFrom() {
return from;
}
public void setFrom(DateTime from) {
this.from = from;
}
public DateTime getTo() {
return to;
}
public void setTo(DateTime to) {
this.to = to;
}
}
but I would like my SearchQueryParam class look more like this:
public final class SearchQueryParam {
@DateTimeFormat(iso = DATE_TIME)
public final DateTime from;
@DateTimeFormat(iso = DATE_TIME)
public final DateTime to;
public SearchQueryParam(DateTime from, DateTime to) {
this.from = from;
this.to = to;
}
}