Home > Blockchain >  Post request ERROR 404 REST API , JAVA, Springboot
Post request ERROR 404 REST API , JAVA, Springboot

Time:07-28

I try to send a simple post request to my Spring Boot app with postman. Unfortunately, I get this 404 ERROR. I saw some tutorial and documentation, but still now know how I can solve this problem.

Controller

@RestController
public class FileUploadController {
 @PostMapping("/uploadFile")
  public ResponseEntity<FileUploadResponse> uploadFile(@RequestParam("file") MultipartFile multipartFile) throws IOException {
    FileUploadResponse response = new FileUploadResponse();
    //FileUploadUtil is the class we programme for saving file to data server.
    FileUploadUtil fileUploadUtil = new FileUploadUtil();
    //StringUtils.cleanPath:remove invalid characters from the path
    String fileName = StringUtils.cleanPath(Objects.requireNonNull(multipartFile.getOriginalFilename()));
    //
    response.setFileName(fileName);
    response.setDownloadUri("/downloadFile/"   fileUploadUtil.saveFile(fileName, multipartFile));

    return new ResponseEntity<>(response, HttpStatus.OK);
  }
}

Util

  String saveFile(String fileName, MultipartFile multipartFile) throws IOException {
    //Files-Upload is name of Folder that we save the file
    String uploadFileLocation = "Files-Upload";
    Path uploadPath = Paths.get(uploadFileLocation);

    if (!Files.exists(uploadPath.resolve(fileName))) {
      Files.createDirectories(uploadPath);
    }
    /*
    -try catch for caching possible Error.
    -multipartFile:A representation of an uploaded file received in a multipart request.
    -getInputStream:Return an InputStream to read the contents of the file from.
    -Input Stream:you can read data from a Java InputStream as an ordered sequence of bytes. This is useful when reading data
     from a file, or received over the network
     */
    try (InputStream inputStream = multipartFile.getInputStream()) {
      /*.resolve:It returns a string with absolute path, An absolute path always contains the root element and the complete
      directory list required to locate the file*/
      //path is for addressing a folder locally, so this line tell us to get the address of the file in java that we want to save
      // our uploaded file.
      //we search for "file name" in our upload directory
      //(InputStream in, Path target, CopyOption... options)
      Files.copy(inputStream, uploadPath.resolve(fileName), StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
      throw new RuntimeException("Error Uploading file", e);
    }
    return fileName   RandomStringUtils.randomAlphanumeric(8);
  }
} 

and post man: enter image description here

CodePudding user response:

A 404 means the URL you requested was not found. To fix it, try changing your postman configuration from this:

http://localhost:8080/easymark/uploadFile

to this:

http://localhost:8080/uploadFile

This change would align the HTTP call with how you configured your controller, which is listening for /uploadFile based on this annotation:

@PostMapping("/uploadFile")

CodePudding user response:

Without any Error Code it´s hard to find the Problem. Please add it.

  • Related