动态注册spring mvc控制器

时间:2015-11-09 07:00:04

标签: java spring spring-mvc

我目前正在开发一个Web应用程序,其中客户端(AngularJS)调用RESTful API(使用spring MVC实现)。我的控制器目前看起来像这样:

@RestController
@RequestMapping("myapp/employees")
public class EmployeesController{
    @Autowired
    private Bus bus;
    @RequestMapping("/", POST)
    public void hireEmployee(@RequestBody HireEmployeeCommand hireEmployeeCommand){
        bus.send(hireEmployeeCommand);
    }
    @RequestMapping("/", DELETE)
    public void fireEmployee(@RequestBody FireEmployeeCommand fireEmployeeCommand){
        bus.send(fireEmployeeCommand);
    }
//etc...
}

其中Command是模拟对特定事务的请求的DTO(它们与CQRS中的命令或Bob叔叔的清洁体系结构中的请求模型具有相同的用途)。

spring负责将请求分派给控制器并将它们映射到参数这一事实非常方便。但是,我想知道是否有一种方法可以让这个精益呃可能是通过编写一个能让控制器注册看起来像这样的DSL:

routesFor("myapp/employees")
.post("/", HireEmployeeCommand.class)
.delete("/", FireEmployeeCommand.class);

和其他地方我将有一个统一的请求处理程序,看起来像:

public void handleRequest(CommandBase cmd){
    //CommandBase is the base class of all commands
    //cmd should be a concrete implementation of CommandBase
    //properties of cmd should have been mapped from the HttpRequest using whatever object mapping utility spring uses
    bus.send(cmd);
}

无论如何,我可以配置spring将请求“映射”到相应的命令并将它们分派给通用请求处理程序吗?

0 个答案:

没有答案