Spring restservice と 2 日間格闘しましたが、ファイルのアップロードができません。
私のコントローラーとそのメソッドは、ガイドhttp://spring.io/guides/gs/uploading-files/に基づいています
しかし、それには多くの問題がありました。私のリクエストは常にエラーを返していたので、最初に library: jackson-mapper-asl を含める必要がありました。なぜ Spring Webservice テンプレート プロジェクトにこのライブラリが追加されなかったのだろうか。
jackson-mapper-asl ライブラリを追加したので、単純な GET リクエストは適切に処理されました。しかし、このガイドで説明されているようにファイルのアップロードを処理するメソッドを実装すると、まったく機能しません。
これは私の Controller クラスとそのメソッドの実装です:
@Controller
@RequestMapping("/controller")
public class GreetingsController {
@RequestMapping(value="/upload2", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload2(@RequestParam("name") String name, @RequestParam("myfile") MultipartFile file){
if (file != null && !file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
これは、リクエストを行うAndroidコードの私の部分です:
final String serviceAddress3= "http://someaddress:8080/testappx2/controller/upload2";
String mb05 = sdcardMountPoint + "/DCIM/halfMB.jar";
final RestTemplate restTemplate = new RestTemplate(true);
FileSystemResource fsr = new FileSystemResource(mb05);
final LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
map.add("name", "superplik");
map.add("myfile", fsr);
Thread t = new Thread(new Runnable(){
public void run() {
Object result = restTemplate.postForObject(serviceAddress2, map, String.class);
}
});
t.start();
私は次のライブラリを使用しています:
* spring-core 3.0.5.RELEASE
* spring-web 3.0.5.RELEASE
* spring-webmvc 3.0.5.RELEASE
* jackson-mapper-asl 1.9.13
SoapUI を使用して、Android モック APP から直接 POST リクエストを作成しようとしました。私が得た唯一の結果は次のとおりです。org.springframework.web.client.HttpClientErrorException: 400 Bad Request
アプリケーションサーバーのログを掘り下げることにしましたが、リクエストが「完了」していないことがわかりました
以下の要求処理ログ:
16:09:46,029 TRACE [org.jboss.as.web.security] (http--0.0.0.0-8080-2) Begin invoke, caller=null
16:09:46,030 TRACE [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-2) Bound request context to thread: org.apache.catalina.connector.RequestFacade@6195db1d
16:09:46,032 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-2) DispatcherServlet with name 'mvc-dispatcher' processing POST request for [/testappx2/controller/upload]
16:09:46,034 TRACE [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-2) Testing handler map [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping@16321271] in DispatcherServlet with name 'mvc-dispatcher'
16:09:46,035 DEBUG [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (http--0.0.0.0-8080-2) Mapping [/controller/upload] to HandlerExecutionChain with handler [hello.GreetingsController@6ee8560d] and 2 interceptors
16:09:46,037 TRACE [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-2) Testing handler adapter [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter@5769a85d]
16:09:46,039 DEBUG [org.apache.tomcat.util.http.Parameters] (http--0.0.0.0-8080-2) Set encoding to ISO-8859-1
16:09:46,040 DEBUG [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver] (http--0.0.0.0-8080-2) Resolving exception from handler [hello.GreetingsController@6ee8560d]: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'name' is not present
16:09:46,043 DEBUG [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver] (http--0.0.0.0-8080-2) Resolving exception from handler [hello.GreetingsController@6ee8560d]: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'name' is not present
16:09:46,045 DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (http--0.0.0.0-8080-2) Resolving exception from handler [hello.GreetingsController@6ee8560d]: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'name' is not present
16:09:46,047 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-2) Null ModelAndView returned to DispatcherServlet with name 'mvc-dispatcher': assuming HandlerAdapter completed request handling
16:09:46,048 TRACE [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-2) Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@6195db1d
16:09:46,049 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-2) Successfully completed request
16:09:46,050 TRACE [org.springframework.web.context.support.XmlWebApplicationContext] (http--0.0.0.0-8080-2) Publishing event in WebApplicationContext for namespace 'mvc-dispatcher-servlet': ServletRequestHandledEvent: url=[/testappx2/controller/upload]; client=[172.16.241.58]; method=[POST]; servlet=[mvc-dispatcher]; session=[null]; user=[null]; time=[20ms]; status=[OK]
16:09:46,051 TRACE [org.springframework.web.context.support.XmlWebApplicationContext] (http--0.0.0.0-8080-2) Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/testappx2/controller/upload]; client=[172.16.241.58]; method=[POST]; servlet=[mvc-dispatcher]; session=[null]; user=[null]; time=[20ms]; status=[OK]
16:09:46,052 TRACE [org.jboss.as.web.security] (http--0.0.0.0-8080-2) End invoke, caller=null
16:09:46,052 TRACE [org.jboss.security.SecurityRolesAssociation] (http--0.0.0.0-8080-2) Setting threadlocal:null
16:09:46,052 TRACE [org.jboss.jca.core.api.connectionmanager.ccm.CachedConnectionManager] (http--0.0.0.0-8080-2) popped object: org.jboss.as.connector.deployers.processors.CachedConnectionManagerSetupProcessor$CachedConnectionManagerSetupAction@4bae320
Appserver は、「name」パラメータが渡されなかったと言っています:[hello.GreetingsController@6ee8560d]: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'name' is not present
面白いのは(私が偶然発見したことです)、「myfile」パラメーターをLinkedMultiValueMapに追加するのを省略すると、それが見つかり、解析され、メソッドの本体になります。
アプリサーバー ログ:
16:52:29,391 DEBUG [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver] (http--0.0.0.0-8080-1) Resolving exception from handler [hello.GreetingsController@788a7860]: org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'myfile' is not present
16:52:29,391 DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (http--0.0.0.0-8080-1) Resolving exception from handler [hello.GreetingsController@788a7860]: org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'myfile' is not present
16:52:29,392 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-1) Null ModelAndView returned to DispatcherServlet with name 'mvc-dispatcher': assuming HandlerAdapter completed request handling
16:52:29,393 TRACE [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-1) Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@8f22ae
16:52:29,393 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http--0.0.0.0-8080-1) Successfully completed request
16:52:29,394 TRACE [org.springframework.web.context.support.XmlWebApplicationContext] (http--0.0.0.0-8080-1) Publishing event in WebApplicationContext for namespace 'mvc-dispatcher-servlet': ServletRequestHandledEvent: url=[/testappx3/controller/upload2]; client=[172.16.241.58]; method=[POST]; servlet=[mvc-dispatcher]; session=[null]; user=[null]; time=[35ms]; status=[OK]
16:52:29,394 TRACE [org.springframework.web.context.support.XmlWebApplicationContext] (http--0.0.0.0-8080-1) Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/testappx3/controller/upload2]; client=[172.16.241.58]; method=[POST]; servlet=[mvc-dispatcher]; session=[null]; user=[null]; time=[35ms]; status=[OK]
16:52:29,395 TRACE [org.jboss.as.web.security] (http--0.0.0.0-8080-1) End invoke, caller=null
現在、appserver は「myfile」パラメータがないと言っています。
RequestParameter
このメソッドで複数を宣言したのは何か問題があると思いました。しかし、複数で試してみたところ、String
RequestParameters
うまくいくようです。
私は何を間違っていますか?私はハンセン病ですか?
前もって感謝します!