【问题标题】:DispatcherServlet configuration needs to include a HandlerAdapter that supports this handlerDispatcherServlet 配置需要包含一个支持这个处理程序的 HandlerAdapter
【发布时间】:2019-02-21 08:02:15
【问题描述】:

我正在尝试设计一个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();
    }
}

【问题讨论】:

  • 注意@RestController("/api") 与@RestController @RequestMapping("/api") 不同。
  • 也可以将@RequestMapping(path = "/ca", method = RequestMethod.GET)替换为@GetMapping("/ca")
  • @noiaverbale 即使在替换为@GetMapping 并添加@RequestMapping 后仍然存在同样的问题。
  • 哦,是的,@GetMapping("/") 导致了问题,在删除参数 '/' it worked ! Thanks !! i've got confused witht he parameter of @restController` 注释后,如果需要引用,它实际上是一个 bean 名称。

标签: spring-boot spring-rest


【解决方案1】:

为了清楚起见,修复是:

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/ca 和 http://localhost:8080/api/ca/health

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      试试这个

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

      而不是

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

      【讨论】:

        猜你喜欢
        • 2014-10-23
        • 2021-07-27
        • 2017-03-15
        • 1970-01-01
        • 2015-09-22
        • 2016-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多