Spring 4 REST服务 - 获取404

时间:2016-09-10 22:40:04

标签: spring file rest model-view-controller upload

我正在尝试使用基于Spring的REST服务的POC来按照tutorial的教程上传文件。但是,当我尝试上传文件时,我收到以下错误

  

错误404:javax.servlet.UnavailableException:SRVE0319E:对于   [SpringRestWebservice] servlet,   org.springframework.web.servlet.handler.DispatcherServletWebRequest   发现了servlet类,但资源注入失败了   发生了。 java.lang.NoSuchMethodException:   org.springframework.web.servlet.handler.DispatcherServletWebRequest。()

控制器代码如下所示

@RestController
@RequestMapping(value = "/restService")
// Max uploaded file size (here it is 20 MB)
@MultipartConfig(fileSizeThreshold = 20971520)
public class RestServiceController {

@RequestMapping(value = "/fileUpload")
public String uploadFile(@RequestParam("uploadedFile") MultipartFile  uploadedFileRef){
    System.out.println("Entering RestServiceController.uploadFile");

    // Get name of uploaded file.
    String fileName = uploadedFileRef.getOriginalFilename();
    System.out.println("File to upload : " + fileName);

    // Path where the uploaded file will be stored.
    String path = "C:/SpringRestService/" + fileName;

    // This buffer will store the data read from 'uploadedFileRef'
    byte[] buffer = new byte[1000];
    FileInputStream reader = null;
    //FileOutputStream writer = null;
    int totalBytes = 0;

    try {
        // Now create the output file on the server.
        //File outputFile = new File(path);

        //outputFile.createNewFile();

        // Create the input stream to uploaded file to read data from it.
        reader = (FileInputStream) uploadedFileRef.getInputStream();

        // Create writer for 'outputFile' to write data read from
        // 'uploadedFileRef'
        //writer = new FileOutputStream(outputFile);

        // Iteratively read data from 'uploadedFileRef' and write to
        // 'outputFile';            
        int bytesRead = 0;
        while ((bytesRead = reader.read(buffer)) != -1) {
            //writer.write(buffer);
            totalBytes += bytesRead;
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            reader.close();
            //writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    System.out.println("Leaving RestServiceController.uploadFile");
    return "File uploaded successfully! Total Bytes Read="+totalBytes;
}
}

web.xml片段

<servlet>
<servlet-name>SpringRestWebservice</servlet-name>
<servlet-class>org.springframework.web.servlet.handler.DispatcherServletWebRequest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SpringRestWebservice</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

提交文件上载的HTML内容

<body>
<form method="POST" enctype="multipart/form-data"
  action="http://localhost:9080/SpringRestWebservice/restService/fileUpload">
File to upload: <input type="file" name="uploadedFile"><br />
<input type="submit" value="Upload">
</form>
</body>

环境

 Eclipse Neon Release (4.6.0)
 Spring 4.2.5
 WAS Liberty v16

不确定遗失了什么?请帮忙

1 个答案:

答案 0 :(得分:0)

发现我必须使用春季课

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

而不是

<servlet-class>org.springframework.web.servlet.handler.DispatcherServletWebRequest</servlet-class>
web.xml中的