我对这个请求有一个问题,我不明白它,因为我有另一个在 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/