简体   繁体   中英

Spring 4 REST Service - Getting 404

I am trying a POC of Spring based REST service to upload a file following the tutorial from the tutorial . However when I try to upload the file using i am getting the following error

Error 404: javax.servlet.UnavailableException: SRVE0319E: For the [SpringRestWebservice] servlet, org.springframework.web.servlet.handler.DispatcherServletWebRequest servlet class was found, but a resource injection failure has occurred. java.lang.NoSuchMethodException: org.springframework.web.servlet.handler.DispatcherServletWebRequest.()

Controller code is given below

@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 snippet

<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 content to submit the fileupload

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

Environment

 Eclipse Neon Release (4.6.0)
 Spring 4.2.5
 WAS Liberty v16

Not sure what am missing? Please help

Found out I had to use spring class

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

instead of

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

in web.xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM