java - long 类型的方法参数缺少 URI 模板变量 'id'

标签 java spring spring-mvc

我对这个请求有一个问题,我不明白它,因为我有另一个在 id 中使用 long 的请求,它是有效的,但是每当我尝试时都会返回这个错误,实际上我对 Spring 不太了解,所以这个问题很难解决。

@RequestMapping(value = "/novopost", method = RequestMethod.POST)
    public String savePost(@Valid Post post, @PathVariable("id") long id){
        post.setData(LocalDate.now());
        Usuario usuario = usuarioRepository.findById(id);
        System.out.println(usuario);
        post.setUsuarios(usuario);
        System.out.println(post);
        postService.save(post);
        return "redirect:/time-line";
    }

Error

There was an unexpected error (type=Internal Server Error, status=500). Missing URI template variable 'id' for method parameter of type long org.springframework.web.bind.MissingPathVariableException: Missing URI template variable 'id' for method parameter of type long

最佳答案

您需要在网址中指定/{id}

 @RequestMapping(value = "/novopost/{id}", method = RequestMethod.POST)
 public String savePost(@Valid Post post, @PathVariable("id") long id){
      post.setData(LocalDate.now());
      Usuario usuario = usuarioRepository.findById(id);
      System.out.println(usuario);
      post.setUsuarios(usuario);
      System.out.println(post);
      postService.save(post);
      return "redirect:/time-line";
 }

关于java - long 类型的方法参数缺少 URI 模板变量 'id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66286381/

相关文章:

java - 使用java测试ReactiveMongoTemplate的配置

java - 我怎样才能修改mysql更新方法?

java - 在 C 上工作的 send() 套接字,但从 bufferedReader 读取的 Java 没有得到响应

java - com.jcraft.jsch.JSchException : remote port forwarding failed for listen port 19999

spring-mvc - ServletContext 和 Spring MVC

java - 使用 spring MVC 将文件夹上传到 mysql 时出错

java - 自定义@RequestParam返回消息和条件参数

java - 使用 Proguard 将 Guava 嵌入到另一个 jar 中

java - 为什么枚举在 java.utils 中被转换为 ArrayList 而不是 List?

java - Spring Integration 是否适合 "reliable queue"的网络场处理?