我正在尝试设计一个rest api,下面是我的 Controller 代码。
当我调用 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();
}
}
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();
}
}
最佳答案
为清楚起见,修复是:
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 配置)。
关于spring-boot - DispatcherServlet 配置需要包含一个支持这个处理程序的 HandlerAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54802028/