我正在尝试设计一个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();
}
}
为清楚起见,解决方法是: