正在学习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
有什么想法吗?可能是什么原因..?我认为两者都应该起作用,但是以上两者都不起作用...
在第二个代码段中,您没有为控制器指定请求映射。
应该在@RequestMapping
中而不是在@RestController
中完成。
这应该起作用:
@RequestMapping("/test")
@RestController
public class TestController {
@Autowired
private TestService testService;
@GetMapping
public int testTransaction(){
return 10;
}
}