Answer the question
In order to leave comments, you need to log in
Why is Converter not picked up?
Good day to all.
Please help me with the following problem.
I have an application on Spring MVC 4.3.2. It has a controller that accepts a string parameter. For greater typing, I want to turn this string parameter into an enum. Those. having:
package my.app.enums;
public enum Option {
OPTION_1,
OPTION_2,
OPTION_3;
}
@Controller
@RequestMapping("foo")
public class MyController {
@RequestMapping("bar/{option}")
public ModelAndView getBar(@PathVariable("option") Option option) {
// всяко-разно
}
}
import org.springframework.core.convert.converter.Converter;
import my.app.enums.Option;
public class OptionConverter implements Converter<String, Option> {
@Override
public Option convert(String source) {
return Option.valueOf(source); // на самом деле, посложнее, но это не суть
}
}
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set merge="true">
<bean class="my.app.OptionConverter"/>
</set>
</property>
</bean>
Answer the question
In order to leave comments, you need to log in
The conversionService bean itself must be specified in <mvc:annotation-driven/>
Conversion and Formatting
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set merge="true">
<bean class="my.app.OptionConverter"/>
</set>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"/>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question