If I have RequestMapping in a Spring controller, for example ...
@RequestMapping(method = RequestMethod.GET, value = "{product}") public ModelAndView getPage(@PathVariable Product product)
And the product is an listing. eg. Product.Home
When I request a page, mysite.com/home
I get
Unable to convert value "home" from type 'java.lang.String' to type 'domain.model.product.Product'; nested exception is java.lang.IllegalArgumentException: No enum const class domain.model.product.Product.home
Is there a way for an enum type converter to understand that a lowercase house is actually Home?
I would like url not to be case sensitive and my Java enumerations with standard capital letters.
thank
Decision
public class ProductEnumConverter extends PropertyEditorSupport { @Override public void setAsText(final String text) throws IllegalArgumentException { setValue(Product.valueOf(WordUtils.capitalizeFully(text.trim()))); } }
register it
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="domain.model.product.Product" value="domain.infrastructure.ProductEnumConverter"/> </map> </property> </bean>
Add to controllers that require special conversion
@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Product.class, new ProductEnumConverter()); }
java spring spring-mvc
dom farr Jan 06 2018-11-11T00: 00Z
source share