SpringCloud项目报错:
问题分析:
404错误,未找到服务资源,提示没有找到DeptClientService.queryById(Long)方法;
尝试解决:
- 检查路径是否错误
服务消费者端和服务提供者端都采用@GetMapping,且路径一样,没有问题。 - 检查DeptClientService.queryById(Long)方法,传入参数是否为Null,发现问题
没有加入@PathVariable注解,调用的方法接收不到参数。服务提供端自然找不到DeptClientService.queryById(Long)方法,所以报404错误
解决法案:
在处理类方法参数前加上@PathVairable注解,接收参数
@GetMapping("/dept/get/{id}")
public Dept queryById(@PathVariable Long id);
复习@PathVariable注解:(路径变量)
-
@PathVariable是spring3.0的一个新功能:接收请求路径中占位符的值,该功能在SpringMVC 向 **REST **目标挺进发展过程中具有里程碑的意义。
-
通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx”) 绑定到操作方法的入参中。
@Controller
@RequestMapping("hello")
public class HelloController2 {
/**
*3、占位符映射
* 语法:@RequestMapping(value=”user/{userId}/{userName}”)
* 请求路径:http://localhost:8080/hello/show5/1/james
* @param ids
* @param names
* @return
*/
@RequestMapping("show5/{id}/{name}")
public ModelAndView test5(@PathVariable("id") Long ids ,@PathVariable("name") String names){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","占位符映射:id:"+ids+";name:"+names);
mv.setViewName("hello2");
return mv;
}