Last active
November 18, 2016 07:23
-
-
Save Cepr0/0fd265e31e3baaac8ab01e792d06b8d0 to your computer and use it in GitHub Desktop.
Custom BackendIdConverter example for LocalDate as an Entity primary key (@id) for Spring Data REST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DateIdConverter implements BackendIdConverter { | |
@Override | |
public Serializable fromRequestId(String id, Class<?> aClass) { | |
if(supports(aClass)) { | |
return id == null ? LocalDate.now() : LocalDate.parse(id); | |
} else { | |
return id; | |
} | |
} | |
@Override | |
public String toRequestId(Serializable id, Class<?> aClass) { | |
if(supports(aClass)) { | |
return ((LocalDate) id).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); | |
} else { | |
return id.toString(); | |
} | |
} | |
@Override | |
public boolean supports(Class<?> aClass) { | |
return DateId.class.isAssignableFrom(aClass); | |
} | |
} | |
// An example of using this converter (maybe RepositoryRestMvcConfiguration is a wrong place for this) | |
// @Configuration | |
// public class ApplicationConfig extends RepositoryRestMvcConfiguration { | |
// @Override | |
// public PluginRegistry<BackendIdConverter, Class<?>> backendIdConverterRegistry() { | |
// List<BackendIdConverter> converters = new ArrayList<BackendIdConverter>(); | |
// converters.add(new DateIdConverter()); | |
// converters.add(BackendIdConverter.DefaultIdConverter.INSTANCE); | |
// return OrderAwarePluginRegistry.create(converters); | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment