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();
    }
}
Jigar Naik
  • 1,946
  • 5
  • 29
  • 60
  • 13
    Beware that `@RestController("/api")` is different than `@RestController @RequestMapping("/api")`. – noiaverbale Feb 21 '19 at 08:11
  • 1
    Also `@RequestMapping(path = "/ca", method = RequestMethod.GET)` can be replaced by `@GetMapping("/ca")` – noiaverbale Feb 21 '19 at 08:14
  • @noiaverbale Even after replacing with `@GetMapping` and adding `@RequestMapping` still having same issue. – Jigar Naik Feb 21 '19 at 08:19
  • 1
    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. – Jigar Naik Feb 21 '19 at 08:21

3 Answers3

19

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).

Greg Witczak
  • 1,634
  • 4
  • 27
  • 56
5

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

@RestController
@RequestMapping("api/")
...
lewiscodes
  • 51
  • 1
  • 1
4

Try this

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

instead of

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