@RestController @GetMapping没有处理程序问题的适配器

时间:2020-04-19 12:26:24

标签: spring spring-boot rest spring-mvc spring-rest

正在学习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

有什么主意吗?可能是什么原因..?我认为两者都应该起作用,但是以上两者都不起作用...

1 个答案:

答案 0 :(得分:5)

在第二段代码中,您没有为控制器指定请求映射。

这应该在@RequestMapping中完成,而不要在@RestController中完成。

这应该有效:

@RequestMapping("/test")
@RestController
public class TestController {

    @Autowired
    private TestService testService;

    @GetMapping 
    public int testTransaction(){
        return 10;
    }
}

您的第一个代码段有效,因为您在方法级别-@GetMapping("/test")

上指定了请求映射