Конвертер весны не называется

1

У меня есть конвертер, но он не называется. Я всегда получаю исключение BindException.

public class IntegerToQueConverter implements Converter<Integer, QuestionType> {

@Autowired
private QuestionTypeService questionTypeService;

@Override
public QuestionType convert(Integer questionType) {

    return questionTypeService.findOne(questionType);
}

Я определил следующий компонент в контексте приложения.

<annotation-driven conversion-service="conversionService">

 <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" >
  <property name="converters">
    <list>
        <bean class="org.hr.quiz.converter.IntegerToQueConverter"/>

    </list>
</property>
</bean>

Пожалуйста помоги. Заранее спасибо!

Обновить:

Я хочу, чтобы конвертер был вызван в этом коде:

    @RequestMapping(value="/insertQuestion", method = RequestMethod.POST)
public ModelAndView insertQuestion(@ModelAttribute("questions") Question question)
{
    ModelAndView view=new ModelAndView("addQuestion");

    System.out.println(question.getQuestion());


            .............................

    return view;
}
  • 2
    Вы используете это в веб-приложении? Как правило, вы конвертируете из String в определенный тип объекта.
  • 0
    @KevinBowersox: да, используя это в веб-приложении.
Теги:
spring
spring-mvc

1 ответ

2
Лучший ответ

Измените аргументы типа Converter на <String,QuestionType>:

public class IntegerToQueConverter implements Converter<String, QuestionType> {

    @Autowired
    private QuestionTypeService questionTypeService;

    @Override
    public QuestionType convert(String questionType) {
        return questionTypeService.findOne(questionType);
    }
}

Значения, переданные с запросом, будут иметь тип String который требует, чтобы преобразователь имел тип Converter<String,QuestionType>.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню