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

问题描述 投票:0回答:1

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

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

spring spring-boot rest spring-mvc spring-rest
1个回答
1
投票

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

应该在@RequestMapping中而不是在@RestController中完成。

这应该起作用:

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

    @Autowired
    private TestService testService;

    @GetMapping 
    public int testTransaction(){
        return 10;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.