DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler
Asked Answered
S

3

15

I am trying to design a rest api, and below is my controller code.

when i invoke http://localhost:8080/ the response is fine, but if i hit http://localhost:8080/api/ca it thorws javax.servlet.ServletException: No adapter for handler [...CaDetailController@48224381]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler

@RestController("/api")
public class CaDetailController {

    private static final Logger logger = LoggerFactory.getLogger(GetClassLoader.class.getClass());

    @Autowired
    CaService caService;

    @RequestMapping(path = "/ca", method = RequestMethod.GET)
    public @ResponseBody List<CaDetail> getCorporateActions() {
        logger.info("CaDetailController.findAllCaDetails()");
        return caService.findAllCaDetails();
    }

    @RequestMapping(path = "/ca/{caId}", method = RequestMethod.GET)
    public @ResponseBody List<CaDetail> getCorporateActions(@PathParam("caId") long caId) {
        logger.info("CaDetailController.getCorporateActions() : caId : " + caId);
        return caService.findAllCaDetails();
    }
}

Updated controller.

@RestController
@RequestMapping("/api/ca")
public class CaDetailController {

    private static final Logger logger = LoggerFactory.getLogger(GetClassLoader.class.getClass());

    @Autowired
    CaService caService;

    @GetMapping(path = "/")
    public @ResponseBody List<CaDetail> getCorporateActions() {
        logger.info("CaDetailController.findAllCaDetails()");
        return caService.findAllCaDetails();
    }

    @GetMapping(path = "/{caId}")
    public @ResponseBody List<CaDetail> getCorporateActions(@PathParam("caId") Long caId) {
        logger.info("CaDetailController.getCorporateActions() : caId : " + caId);
        return caService.findAllCaDetails();
    }
}
Swaney answered 21/2, 2019 at 8:2 Comment(4)
Beware that @RestController("/api") is different than @RestController @RequestMapping("/api").Kooky
Also @RequestMapping(path = "/ca", method = RequestMethod.GET) can be replaced by @GetMapping("/ca")Kooky
@Kooky Even after replacing with @GetMapping and adding @RequestMapping still having same issue.Swaney
oh yeah @GetMapping("/") was causing the problem, after removing parameter '/' it worked ! Thanks !! i've got confused witht he parameter of @restController` annotation which is actually a bean name if required to refer.Swaney
S
21

For clarity, fix is:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/ca")
public class CaDetailController {

    @GetMapping
    public String healthcheck1() {
        return "ok!";
    }

    @GetMapping(path = "health")
    public String healthcheck2() {
        return "ok again!";
    }

}

You can call this endpoints using URL: http://localhost:8080/api/ca and http://localhost:8080/api/ca/health

(Assuming default Spring Boot Tomcat configuration).

Schuman answered 22/12, 2019 at 10:18 Comment(0)
F
6

Don't add ("/api") value to @RestController Annotation, add it to @RequestMapping

@RestController
@RequestMapping("api/")
...
Fled answered 12/7, 2021 at 16:5 Comment(0)
W
5

Try this

@RestController
@RequestMapping("/api")
public class CaDetailController {

instead of

@RestController("/api")
public class CaDetailController {
Wnw answered 6/4, 2022 at 13:45 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.