有没有理由不将控制器映射为接口?
在所有示例和问题中,我看到周围的控制器,都是具体的类.是否有一个原因?我想将请求映射与实现分开.当我试图@PathVariable在我的具体课程中获得一个参数时,我碰到了一堵墙.
我的Controller界面如下所示:
@Controller
@RequestMapping("/services/goal/")
public interface GoalService {
@RequestMapping("options/")
@ResponseBody
Map<String, Long> getGoals();
@RequestMapping(value = "{id}/", method = RequestMethod.DELETE)
@ResponseBody
void removeGoal(@PathVariable String id);
}
Run Code Online (Sandbox Code Playgroud)
而实施班:
@Component
public class GoalServiceImpl implements GoalService {
/* init code */
public Map<String, Long> getGoals() {
/* method code */
return map;
}
public void removeGoal(String id) {
Goal goal = goalDao.findByPrimaryKey(Long.parseLong(id));
goalDao.remove(goal);
}
}
Run Code Online (Sandbox Code Playgroud)
该getGoals()方法效果很好; 在removeGoal(String id)抛出一个异常
ExceptionHandlerExceptionResolver - Resolving exception from handler [public void
todo.webapp.controllers.services.GoalServiceImpl.removeGoal(java.lang.String)]: …Run Code Online (Sandbox Code Playgroud)