Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@RestController @GetMapping No adapter for handler issue

Learning Spring Rest, had some doubt on below:

@RestController
public class TestController {

    @Autowired
    private TestService testService;

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

Above snippet worked very well, and returned response 10.

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

    @Autowired
    private TestService testService;

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

For above snippet, getting me an error as below:

 threw exception No adapter for handler The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler with root cause

Any idea? what could be the reason..? I am thinking both should work, but above one is not working...

like image 389
john Avatar asked May 07 '26 10:05

john


1 Answers

In your second code piece you didn't specify request mapping for your controller.

This should be done in @RequestMapping not in @RestController.

This should work:

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

    @Autowired
    private TestService testService;

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

Your first code piece works because you specified request mapping on a method level - @GetMapping("/test")

like image 99
Maciej Mościcki Avatar answered May 09 '26 01:05

Maciej Mościcki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!