最近在Java web 项目中需要采用非常简单的REST框架,Struts2、webwork、JSF 经过一番比较,最后选择了Spring3,理由只有一个 “简单,好用,并满足需要”。很久以前就Rod Johnson大叔说 Spring3 全面支持REST风格的Web服务,"We're really seeing extensive interest and growth in REST, and it will have comprehensive support for RESTful Web services," said Johnson,今天亲自尝试了一下,真有点相识恨晚的感觉,如果在这次项目运用没有太大的问题,将来在其他项目会大量运用。
工作原理如图所示:
*根据HTTP请求的URL,调用相应的DispatcherServlet控制器。
*提供一个视图是作为HTTP响应发送。
页面上最终运行效果,如图所示:
主要代码:
清单1:TopicController
package com.javabloger.springrest.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/topic") //url映射的名称
public class TopicController {
@RequestMapping(value = "/{id}",method=RequestMethod.GET)
public String helloWorld(
@PathVariable Long id,
HttpServletRequest request,
[...]
文章来源:
http://www.javabloger.com/article/spring3-rest-mvc-example.html?source=rss