Spring 3 REST hello world example
2015-05-15 15:13
537 查看
Spring 3 REST hello world example
By mkyong | August 2, 2011 | Last Updated : November 14, 2012
In Spring 3, old RequestMapping class is enhanced to support RESTful features, which makes Spring developers easier to develop REST services in Spring MVC.
In this tutorial, we show you how to use Spring 3 MVC annotations to develop a RESTful style web application.
1. Project Directory
Review the project folder structure.
2. Project Dependency
To develop REST in Spring MVC, just include the core Spring and Spring MVC dependencies.
pom.xml
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
3. REST Controller
For Spring RESTful, you need PathVariable, RequestMapping and RequestMethod. Following code should be self-explanatory.
MovieController.java
package com.mkyong.common.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@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";
}
}
4. JSP Views
A JSP page to display the value.
list.jsp
<html>
<body>
<h1>Spring 3 MVC REST web service</h1>
<h2>Movie Name : ${movie}</h2>
</body>
</html>
5. Demo
See REST URLs demonstration.
URL : http://localhost:8080/SpringMVC/movie/ironMan
Spring MVC REST demo
URL : http://localhost:8080/SpringMVC/movie/SpiderMan4
spring mvc rest demo
By mkyong | August 2, 2011 | Last Updated : November 14, 2012
In Spring 3, old RequestMapping class is enhanced to support RESTful features, which makes Spring developers easier to develop REST services in Spring MVC.
In this tutorial, we show you how to use Spring 3 MVC annotations to develop a RESTful style web application.
1. Project Directory
Review the project folder structure.
2. Project Dependency
To develop REST in Spring MVC, just include the core Spring and Spring MVC dependencies.
pom.xml
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
3. REST Controller
For Spring RESTful, you need PathVariable, RequestMapping and RequestMethod. Following code should be self-explanatory.
MovieController.java
package com.mkyong.common.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@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";
}
}
4. JSP Views
A JSP page to display the value.
list.jsp
<html>
<body>
<h1>Spring 3 MVC REST web service</h1>
<h2>Movie Name : ${movie}</h2>
</body>
</html>
5. Demo
See REST URLs demonstration.
URL : http://localhost:8080/SpringMVC/movie/ironMan
Spring MVC REST demo
URL : http://localhost:8080/SpringMVC/movie/SpiderMan4
spring mvc rest demo
相关文章推荐
- Spring REST Hello World JSON Example
- Spring 3 REST hello world example
- Spring REST Hello World XML Example
- Spring Data MongoDB hello world example
- Gradle – Spring 4 MVC Hello World Example – Annotation
- Spring Security hello world example
- Spring MVC hello world example
- RESTEasy hello world example
- Gradle – Spring 4 MVC Hello World Example
- Spring MVC Hello World Example(转)
- Gradle – Spring 4 MVC Hello World Example – Annotation
- Spring MVC Hello World Example
- Maven的安装使用以及 Maven+Spring hello world example
- Spring 3 MVC hello world example – Annotation
- Spring 3 MVC Framework Based Hello World Web Application Example Using Maven, Eclipse IDE And Tomcat
- Spring MVC Hello World Example
- Spring 3 MVC hello world example
- Spring Security Hello World Example--reference
- Spring Security hello world example
- Spring 3 MVC hello world example