【问题标题】:@RestController @GetMapping No adapter for handler issue@RestController @GetMapping 没有处理程序问题的适配器
【发布时间】:2020-08-01 21:12:03
【问题描述】:

学习Spring Rest,有一些疑问:

@RestController
public class TestController {

    @Autowired
    private TestService testService;

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

上面的 sn-p 运行良好,返回响应 10。

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

    @Autowired
    private TestService testService;

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

对于上面的sn-p,给我一个错误如下:

 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】:

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

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

    这应该可行:

    @RequestMapping("/test")
    @RestController
    public class TestController {
    
        @Autowired
        private TestService testService;
    
        @GetMapping 
        public int testTransaction(){
            return 10;
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 1970-01-01
      • 2012-09-26
      • 2021-07-27
      相关资源
      最近更新 更多