gpt4 book ai didi

java - 不调用 Spring Converter

转载 作者:行者123 更新时间:2023-11-30 09:07:42 26 4
gpt4 key购买 nike

我有一个转换器,但它没有被调用。我总是遇到 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com