Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler

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();
    }
}
like image 420
Jigar Naik Avatar asked Feb 21 '19 08:02

Jigar Naik


People also ask

What is HandlerAdapter?

What Is a Handleradapter? The HandlerAdapter is basically an interface which facilitates the handling of HTTP requests in a very flexible manner in Spring MVC. It's used in conjunction with the HandlerMapping, which maps a method to a specific URL. The DispatcherServlet then uses a HandlerAdapter to invoke this method.

Where we can configure DispatcherServlet?

The Spring DispatcherServlet uses special beans to process requests and render the appropriate views. These beans are part of Spring Framework. You can configure them in the WebApplicationContext , just as you configure any other bean.

Which is the default content type for requests handled by the DispatcherServlet?

The View interface also has getContentType() method, which returns content type the view produces (JstlView has text/HTML). This is usually the default content type for requests handled by the dispatcher servlet in Spring.

What is a DispatcherServlet in Spring boot?

The DispatcherServlet is the front controller in Spring web applications. It's used to create web applications and REST services in Spring MVC. In a traditional Spring web application, this servlet is defined in the web. xml file.


1 Answers

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

@RestController
@RequestMapping("api/")
...
like image 78
lewiscodes Avatar answered Sep 21 '22 22:09

lewiscodes