DispatcherServlet配置需要包括一个支持此处理程序的HandlerAdapter

时间:2019-02-21 08:02:15

标签: spring-boot spring-rest

我正在尝试设计一个REST API,下面是我的控制器代码。

当我调用http://localhost:8080/时,响应很好,但是如果我击中http://localhost:8080/api/ca,它就会使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();
    }
}
  

更新的控制器。

@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();
    }
}

2 个答案:

答案 0 :(得分:1)

不要在@RestController注解中添加("/api")值, 添加到@RequestMapping

@RestController
@RequestMapping("api/")
...

答案 1 :(得分:0)

为清楚起见,解决方法是:

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!";
    }

}

您可以使用URL将此端点称为: http://localhost:8080/api/cahttp://localhost:8080/api/ca/health

(假设使用默认的Spring Boot Tomcat配置)。