有段时间没搞java web编程了,很多错误都比较低级,不应该出现,小记,提醒自己应该细心。
写了测试页面,然后进行文件上传测试,忽然发现之前能够用curl调试成功的接口,居然报错了。错误信息如下:
2010-09-27 11:12:44,926 DEBUG [org.springframework.beans.BeanUtils] - No property editor [org.springframework.web.multipart.MultipartFileEditor] found for type org.springframework.web.multipart.MultipartFile according to 'Editor' suffix convention 2010-09-27 11:12:44,927 DEBUG [org.springframework.web.servlet.handler.SimpleMappingExceptionResolver] - Resolving exception from handler [com.roosher.web.mvc.SpaceController@15f6479]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found 2010-09-27 11:12:44,928 DEBUG [org.springframework.web.servlet.handler.SimpleMappingExceptionResolver] - Resolving to view 'uncaughtException' for exception of type [org.springframework.beans.ConversionNotSupportedException], based on exception mapping [.lang.Exception] 2010-09-27 11:12:44,928 DEBUG [org.springframework.web.servlet.handler.SimpleMappingExceptionResolver] - Exposing Exception as model attribute 'exception' 2010-09-27 11:12:44,932 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name 'uncaughtException'; model is {exception=org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found} org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found
主要错误就是
org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile';
怎么看都不知道错在哪里,看异常提示以为是编写PropertiesEditor的错误,或者跟Convert相关,但找寻代码没有看着痕迹。最后在调试页面看到低级的错误:
<form action="/roosher-backend/space/image" method="POST" enctype="application/x-www-form-urlencoded">
......
</form>
原来是enctype错了,应该是 enctype="multipart/form-data"。
@Controller public class Payer { @RequestMapping(...) public String pay(@ModelAttribute User user,BindingResult result) { } }
@Controller public class Payer { @RequestMapping(...) public String pay(@ModelAttribute User user,@RequestParam Hello hello, BindingResult result) { } }