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

joh*_*ohn 2 rest spring spring-mvc spring-boot spring-rest

学习Spring Rest,有以下疑问:

@RestController
public class TestController {

    @Autowired
    private TestService testService;

    @GetMapping("/test")    
    public int testTransaction(){
        return 10;
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码段效果很好,并返回了响应 10。

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

    @Autowired
    private TestService testService;

    @GetMapping 
    public int testTransaction(){
        return 10;
    }
}
Run Code Online (Sandbox Code Playgroud)

对于上面的片段,给我一个错误如下:

 threw exception No adapter for handler The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler with root cause
Run Code Online (Sandbox Code Playgroud)

任何的想法?可能是什么原因..?我认为两者都应该有效,但上面的一个不起作用......

Mac*_*cki 10

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

这应该在@RequestMappingnot in 中完成@RestController

这应该有效:

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

    @Autowired
    private TestService testService;

    @GetMapping 
    public int testTransaction(){
        return 10;
    }
}
Run Code Online (Sandbox Code Playgroud)

您的第一个代码段有效,因为您在方法级别指定了请求映射 - @GetMapping("/test")