Spring MVC中发布Restful Web服务
- - 企业架构 - ITeye博客对于企业应用来说,数据是许多业务的命脉,软件通常是可替换的,但是多年积累的数据是永远不能替换的. 近些年来,以信息为中心的表述性状态转移(Representational State Transfer, REST)已成为替换传统SOAP Web服务的流行方案,为了帮助Spring开发人员,Spring3.0以上的版本中封装了对REST的良好支持.
<!-- 启动注解驱动的spring mvc功能,注册请求url和注解pojo类方法的映射 --> <mvc:annotation-driven/> <!-- 启动包扫描功能,以便注册带有@controller,@service,@repository,@component等注解的类为spring的bean --> <context:component-scan base-package=“com.xxx.framework" />
@Controller @RequestMapping(value = "/tasks") public class TasksController { @RequestMapping(value = "/{id}", method = RequestMethod.PUT) @ResponseStatus(HttpStatus.NO_CONTENT) public void putTaskRequest(@PathVariable("id") long id, @Valid String content) { System.out.println("put"); } @RequestMapping(method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) public TaskDefinition createTaskRequest() { System.out.println("post"); return null; } @RequestMapping(value = "/{id}", method = RequestMethod.GET) @ResponseStatus(HttpStatus.NO_CONTENT) public void showTaskContent(@PathVariable("id") long id) { System.out.println(id); } @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.NO_CONTENT) public void killTaskRequest(@PathVariable("id") long id) { System.out.println("kill"); }
23:18:14,723 INFO DefaultAnnotationHandlerMapping:315 - Mapped URL path [/tasks/{id}] onto handler 'tasksController' 23:18:14,724 INFO DefaultAnnotationHandlerMapping:315 - Mapped URL path [/tasks/{id}.*] onto handler 'tasksController' 23:18:14,724 INFO DefaultAnnotationHandlerMapping:315 - Mapped URL path [/tasks/{id}/] onto handler 'tasksController' 23:18:14,724 INFO DefaultAnnotationHandlerMapping:315 - Mapped URL path [/tasks] onto handler 'tasksController' 23:18:14,725 INFO DefaultAnnotationHandlerMapping:315 - Mapped URL path [/tasks.*] onto handler 'tasksController' 23:18:14,725 INFO DefaultAnnotationHandlerMapping:315 - Mapped URL path [/tasks/] onto handler 'tasksController'