java - 不调用 Spring Converter

标签 java spring spring-mvc

我有一个转换器,但它没有被调用。我总是遇到 BindException。

public class IntegerToQueConverter implements Converter<Integer, QuestionType> {

@Autowired
private QuestionTypeService questionTypeService;

@Override
public QuestionType convert(Integer questionType) {

    return questionTypeService.findOne(questionType);
}

我在应用程序上下文中定义了以下 bean。

<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;
}

最佳答案

更改 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> .

关于java - 不调用 Spring Converter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23890136/

相关文章:

java - Maven cucumber 报告失败 - Java.io.FilenotFoundexception cucumber.json

javascript - 前端应用程序和另一个后端服务使用的 REST API 的身份验证方法

spring - spring框架中的命令对象是什么

java - JPA findAll 可分页请求总数按列分组

java - RooJavaBean 方法 getId() 不会覆盖抽象方法 Persistable.getId()

spring-mvc - WAR 文件不会在 Tomcat 7 上启动

java - 如何在webflux ServerHttpRequest中获取原始url路径

Java 迭代问题

java - 使用 Realm 包含多个库时 DEX 失败

java - 在几秒钟内没有响应后,如何停止主线程中通过 TaskExecutor 运行的线程?