2013-04-17 62 views
1

在我的练习中,我必须开发一个弹簧应用程序,应该可以通过WebGUI和REST服务访问。 现在我浏览一下Spring MVC的示例,其中有关于Spring MVC的世界教程this弹簧Mvc Web应用程序与休息界面

控制器类似如下:

@Controller 
@RequestMapping("/welcome") 
public class HelloController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String printWelcome(ModelMap model) { 
     model.addAttribute("message", "Spring 3 MVC Hello World"); 
    return "hello"; 
    } 
} 

然后我通过Spring REST example看起来像这样看:

@Controller 
@RequestMapping("/movie") 
public class MovieController { 

@RequestMapping(value = "/{name}", method = RequestMethod.GET) 
public String getMovie(@PathVariable String name, ModelMap model) { 

    model.addAttribute("movie", name); 
    return "list"; 

} 

@RequestMapping(value = "/", method = RequestMethod.GET) 
public String getDefaultMovie(ModelMap model) { 

    model.addAttribute("movie", "this is default movie"); 
    return "list"; 

} 

} 

现在我想知道,怎么办这两个例子(Spring的MVC和春天休息)不同? 他们都使用相同的注释和工作类似。这不仅仅是REST的例子吗?

如何为Spring-MVC应用程序提供Rest-Interface?

问候

+0

RESTful服务与访问资源的方式有关。请参阅:https://en.wikipedia.org/wiki/Representational_state_transfer#RESTful_web_APIs – Aurand

回答

1

两个样品是关于Spring Web MVC框架。

你应该更注重的定义,像什么是REST

https://en.wikipedia.org/wiki/Representational_state_transfer

表述性状态转移意在唤起人们的 精心设计的Web应用程序的行为的图像:提出了一个网络(虚拟状态机),用户通过选择链接(状态转换)通过 应用程序前进,导致 下一页(代表应用程序的下一个状态)为 转移给用户并呈现供其使用。

Spring Web MVC极大地方便了开发REST Web API,就是这样。

2

为了提供REST接口与Spring MVC应用程序,你可以申请@RequestMapping批注与路径名到每个控制器中的方法,这对于每个其余服务创建一个唯一的URL路径你想提供。

意思是,其余的服务只是Spring MVC控制器中使用@RequestMapping注解的方法。

如果您想了解Spring MVC中如何支持基于REST的服务,下面的链接可能会有所帮助:

http://blog.springsource.org/2009/03/08/rest-in-spring-3-mvc/#features

-1

记住@ResponseBody方法上的返回类型是REST。可以使用JSON或XML协商返回的对象。