I am specifying the property editors for my controller as :
@InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
binder.registerCustomEditor(Date.class, editor);
}
which work fine until I call the following method in an AJAX call.
@RequestMapping(value = "searchCriteria", method = RequestMethod.GET)
public @ResponseBody Set<SearchCriteria> loadSearchCriterias(){
// call service method to load criterias
Set<SearchCriteria> criterias = new HashSet<SearchCriteria>();
SearchCriteria sampleCriteria = new SearchCriteria();
sampleCriteria.setStartDate(new Date());
criterias.add(sampleCriteria);
return criterias;
}
In this case, the due date in SearchCriteria is not being converted to the proper format by the custom property editor. How can I make property editors to be applied even when I am returning an object through an AJAX call ?
public class SearchCriteria{
Date startDate;
String name;
// getter setters
}
I don’t think it is possible to apply property editor for the returned values. Spring has a converter mechanism for it.
If you returning
jsonand if you have Jackson in your classpath then spring will use it to convert the object to response string.If you returning
xmland if you have JAXB in your classpath then spring will use it to convert the object to response string.If you are using
Jacksonlibrary with spring then you need to tell jackson how you want to serialize the field by using serializer.Ex:
Then annotate your field like
If you want to use this globally you can try the mechanism explained here.