I have the following problem:
A team-member changed the sensa/ext js front end and is sending a parmeter with a space instead of an underscore. I don't know the front-end code of the project as well, and this is causing the following error:
Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.web.bind.annotation.RequestParam org.company.project.persistence.enums.DocumentTypeEnum for value 'EXPERT OPINION'; nested exception is java.lang.IllegalArgumentException: No enum constant org.company.project.persistence.enums.DocumentTypeEnum.EXPERT OPINION
I changed a get parameter of the request using fiddler and I saw that the problem is that EXPERT OPINION
is being sent instead of EXPERT_OPINION
.
Initially, I added a filter
and tried changing the get parameter value, but I had to add a wrapper since you cannot modify http requests directly. However, the underlying converter seems to get the parameter
value directly from the original http request so this failed.
Then I decided to try making a custom converter. I have made the following class that is instantiated when I run the project, but it is never called to perform the specific conversion:
@Configuration
public class EnumCustomConversionConfiguration {
@Bean
public ConversionService getConversionService() {
ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
bean.setConverters(getConverters());
bean.afterPropertiesSet();
ConversionService object = bean.getObject();
return object;
}
private Set<Converter> getConverters() {
Set<Converter> converters = new HashSet<Converter>();
converters.add(new StringToEnumConverter(DocumentTypeEnum.class));
return converters;
}
@SuppressWarnings("rawtypes")
private final class StringToEnumConverter<T extends Enum> implements Converter<String, T> {
private final Class<T> enumType;
public StringToEnumConverter(Class<T> enumType) {
this.enumType = enumType;
}
@SuppressWarnings("unchecked")
public T convert(String source) {
checkArg(source);
return (T) Enum.valueOf(enumType, source.trim());
}
private void checkArg(String source) {
// In the spec, null input is not allowed
if (source == null) {
throw new IllegalArgumentException("null source is in allowed");
}
}
}
}