当前位置: 首页 > 知识库问答 >
问题:

发布请求错误404 REST API、JAVA、Springboot[关闭]

呼延衡
2023-03-14

编辑问题以包括所需的行为、特定问题或错误,以及再现问题所需的最短代码。这将帮助其他人回答这个问题。

我试着用postman向我的Spring Boot应用程序发送一个简单的post请求。不幸的是,我得到这个404错误。我看过一些教程和文档,但现在仍然知道如何解决这个问题。

控制器

@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);
  }
} 

发布人:在此处输入图像描述

共有2个答案

魏兴邦
2023-03-14

如果没有任何错误代码,很难找到问题。请添加它。

冯阳云
2023-03-14

解决方案1:通过在属性中设置上下文路径来修复404错误。在应用程序中设置属性。属性/yml文件:

server.servlet.context-path=/easymark

解决方案2:将URL映射添加到@RestController中。

@RestController("/easymark")

解决方案2:更改邮递员URL:

http://localhost:8080/easymark/uploadFile

为此:

http://localhost:8080/uploadFile
 类似资料:
  • 我真的不知道我的java代码中的错误在哪里。我必须使用REST API登录Kofax Total Agility。为此,我尝试使用postman测试我的json是否正确构建。以下是我的登录JSON: 我得到了肯定的回答: 到目前为止,一切顺利。为此,我创建了模型: 对于响应: 这些类应该允许我构建 json。现在,我创建了一个方法,用于生成请求对象并期望响应对象。 当我调用这部分代码时,我注意到我

  • 我正在尝试根据下面的代码提出帖子请求: 此请求不需要身份验证,也不需要登录和密码。在邮递员那里,我可以成功地提出请求,但当我尝试执行上述代码时,我得到了: 401未经授权 我在邮递员那里模拟了错误,它给了我下面的信息: 当我在Postman中将身份验证方法更改为“No Auth”时,请求运行良好。 我想我必须在代码中设置这个选项“No Auth”,但我不知道如何设置。 我这样做: 但错误仍在继续。

  • 我正在使用实现一个联系人应用程序。现在,我正试图通过发送以下格式的put请求来更新联系人 我将XML作为字符串发送,作为请求的主体。这是我的xmlString(请求主体) 我写了下面的代码来发送更新联系人的PUT请求。 当我试图在中发送请求时,联系人更新成功。但是当我试图运行上面的程序时,我得到了 400错误请求错误 我不知道我哪里出错了。任何帮助都将不胜感激!

  • 每当页面尝试提交2MB或更大的文件时,都会发生此错误。但是参数在帖子里!我已经检查了Chrome开发工具。有人知道这个错误吗?Springboot 2.0.3。释放

  • 在我的代码中,我试图反复构建一个客户端,并向远程endpoint发送一个输入流: 输入流包含消息的xml正文。我注意到在第一次迭代中。。。请求成功第二次迭代响应为400错误请求。。。第三个400错误请求。。。。等等。。。 如果我将输入流更改为字符串...获取真实的xml并用PUT方法发送它。 有什么区别?为什么它可以处理字符串而不能处理输入流?(似乎inputstream在第一次迭代后会发生变化)

  • ✔加200积分响应期权法 ✔将Access-Control-Allog-Headers,Access-Control-Allog-Methods,Access-Control-Allog-Origin方法响应头添加到OPTIONS ✔将Access-Control-Allow-Headers,Access-Control-Allow-Methods,Access-Control-Allow-Ori