正在学习Spring Rest,下面对此有疑问:
@RestController
public class TestController {
@Autowired
private TestService testService;
@GetMapping("/test")
public int testTransaction(){
return 10;
}
}
以上代码片段效果很好,并返回了响应10。
@RestController("/test")
public class TestController {
@Autowired
private TestService testService;
@GetMapping
public int testTransaction(){
return 10;
}
}
对于以上代码段,出现如下错误:
threw exception No adapter for handler The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler with root cause
有什么主意吗?可能是什么原因..?我认为两者都应该起作用,但是以上两者都不起作用...
答案 0 :(得分:5)
在第二段代码中,您没有为控制器指定请求映射。
这应该在@RequestMapping
中完成,而不要在@RestController
中完成。
这应该有效:
@RequestMapping("/test")
@RestController
public class TestController {
@Autowired
private TestService testService;
@GetMapping
public int testTransaction(){
return 10;
}
}
您的第一个代码段有效,因为您在方法级别-@GetMapping("/test")