简短 & 快速介绍REST
REST表示 Representational State Transfer(表示性状态转换).
它是可以用来设计web services的框架,可以被不同的客户端调用。
核心思想是:使用简单的HTTP协议来实现调用,而不是CORBA, RPC 或者 SOAP等负责的机制。
在Rest 基础设计中,资源使用以下动词进行操作。
- 创建资源 : 使用 HTTP POST
- 获取资源 : 使用 HTTP GET
- 更新资源 : 使用 HTTP PUT
- 删除资源 : 使用 HTTP DELETE
也意味着,你作为Rest 服务开发者或者客户,应该遵循以上的标准。
尽管没有限制必须返回的类型,但是一般基于Web services的Rest返回JSON或者XML作为响应。
客户端可以指定(使用HTTP Accept header)他们想要的资源类型吗,服务器返回需要的资源。
指明资源的Content-Type。如果想详细的理解 restful可以参考这里:StackOverflow link
基于Rest的Controller(控制器)
我们的 REST API :
- GET 方式请求 /api/user/ 返回用户列表
- GET 方式请求 /api/user/1返回id为1的用户
- POST 方式请求 /api/user/ 通过user对象的JSON 参数创建新的user对象
- PUT 方式请求 /api/user/3 更新id为3的发送json格式的用户对象
- DELETE 方式请求/api/user/4删除 ID为 4的user对象
- DELETE 方式请求/api/user/删除所有user
- package com.websystique.springmvc.controller;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.MediaType;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.util.UriComponentsBuilder;
- import com.websystique.springmvc.model.User;
- import com.websystique.springmvc.service.UserService;
- @RestController
- public class HelloWorldRestController {
- @Autowired
- UserService userService; //Service which will do all data retrieval/manipulation work
- //-------------------Retrieve All Users--------------------------------------------------------
- @RequestMapping(value = "/user/", method = RequestMethod.GET)
- public ResponseEntity<List<User>> listAllUsers() {
- List<User> users = userService.findAllUsers();
- if(users.isEmpty()){
- return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND
- }
- return new ResponseEntity<List<User>>(users, HttpStatus.OK);
- }
- //-------------------Retrieve Single User--------------------------------------------------------
- @RequestMapping(value = "/user/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<User> getUser(@PathVariable("id") long id) {
- System.out.println("Fetching User with id " + id);
- User user = userService.findById(id);
- if (user == null) {
- System.out.println("User with id " + id + " not found");
- return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
- }
- return new ResponseEntity<User>(user, HttpStatus.OK);
- }
- //-------------------Create a User--------------------------------------------------------
- @RequestMapping(value = "/user/", method = RequestMethod.POST)
- public ResponseEntity<Void> createUser(@RequestBody User user, UriComponentsBuilder ucBuilder) {
- System.out.println("Creating User " + user.getName());
- if (userService.isUserExist(user)) {
- System.out.println("A User with name " + user.getName() + " already exist");
- return new ResponseEntity<Void>(HttpStatus.CONFLICT);
- }
- userService.saveUser(user);
- HttpHeaders headers = new HttpHeaders();
- headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(user.getId()).toUri());
- return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
- }
- //------------------- Update a User --------------------------------------------------------
- @RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
- public ResponseEntity<User> updateUser(@PathVariable("id") long id, @RequestBody User user) {
- System.out.println("Updating User " + id);
- User currentUser = userService.findById(id);
- if (currentUser==null) {
- System.out.println("User with id " + id + " not found");
- return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
- }
- currentUser.setName(user.getName());
- currentUser.setAge(user.getAge());
- currentUser.setSalary(user.getSalary());
- userService.updateUser(currentUser);
- return new ResponseEntity<User>(currentUser, HttpStatus.OK);
- }
- //------------------- Delete a User --------------------------------------------------------
- @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
- public ResponseEntity<User> deleteUser(@PathVariable("id") long id) {
- System.out.println("Fetching & Deleting User with id " + id);
- User user = userService.findById(id);
- if (user == null) {
- System.out.println("Unable to delete. User with id " + id + " not found");
- return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
- }
- userService.deleteUserById(id);
- return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
- }
- //------------------- Delete All Users --------------------------------------------------------
- @RequestMapping(value = "/user/", method = RequestMethod.DELETE)
- public ResponseEntity<User> deleteAllUsers() {
- System.out.println("Deleting All Users");
- userService.deleteAllUsers();
- return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
- }
- }
详解:
@RestController :首先我们使用的是Spring 4的新注解 @RestController注解.
此注解避免了每个方法都要加上@ResponseBody注解。也就是说@RestController 自己戴上了 @ResponseBody注解,看以看作是
@Controller 和 @ResponseBody的结合体。
@RequestBody : 如果方法参数被 @RequestBody注解,Spring将绑定HTTP请求体到那个参数上。如果那样做,Spring将根据请求中的ACCEPT或者 Content-Type header(私下)使用 HTTP Message converters 来将http请求体转化为domain对象。
@ResponseBody : 如果方法加上了@ResponseBody注解,Spring返回值到响应体。如果这样做的话,Spring将根据请求中的 Content-Type header(私下)使用 HTTP Message converters 来将domain对象转换为响应体。
ResponseEntity 是一个真实数据.它代表了整个 HTTP 响应(response). 它的好处是你可以控制任何对象放到它内部。
你可以指定状态码、头信息和响应体。它包含你想要构建HTTP Response 的信息。
@PathVariable 此注解意味着一个方法参数应该绑定到一个url模板变量[在'{}'里的一个]中
一般来说你,要实现REST API in Spring 4 需要了解@RestController , @RequestBody, ResponseEntity 和 @PathVariable 这些注解 .另外, spring 也提供了一些支持类帮助你实现一些可定制化的东西。
MediaType : 带着 @RequestMapping 注解,通过特殊的控制器方法你可以额外指定,MediaType来生产或者消耗。
发布和测试此API
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.5.3</version>
- </dependency
因为Spring在类路径发现了这个库,它调用了内置的MappingJackson2HttpMessageConverter 转换器将响应(对象集合)转换为JSON格式。
根据RestTemplate 写REST Client
RestTemplate随之出现。RestTemplate 提供了高级方法,来响应者6种主要的HTTP方法。
HTTP 方法和对应的 RestTemplate方法:
- HTTP GET : getForObject, getForEntity
- HTTP PUT : put(String url, Object request, String…urlVariables)
- HTTP DELETE : delete
- HTTP POST : postForLocation(String url, Object request, String… urlVariables), postForObject(String url, Object request, ClassresponseType, String… uriVariables)
- HTTP HEAD : headForHeaders(String url, String… urlVariables)
- HTTP OPTIONS : optionsForAllow(String url, String… urlVariables)
- HTTP PATCH and others : exchange execute
- package com.websystique.springmvc;
- import java.net.URI;
- import java.util.LinkedHashMap;
- import java.util.List;
- import org.springframework.web.client.RestTemplate;
- import com.websystique.springmvc.model.User;
- public class SpringRestTestClient {
- public static final String REST_SERVICE_URI = "http://localhost:8080/Spring4MVCCRUDRestService";
- /* GET */
- @SuppressWarnings("unchecked")
- private static void listAllUsers(){
- System.out.println("Testing listAllUsers API-----------");
- RestTemplate restTemplate = new RestTemplate();
- List<LinkedHashMap<String, Object>> usersMap = restTemplate.getForObject(REST_SERVICE_URI+"/user/", List.class);
- if(usersMap!=null){
- for(LinkedHashMap<String, Object> map : usersMap){
- System.out.println("User : id="+map.get("id")+", Name="+map.get("name")+", Age="+map.get("age")+", Salary="+map.get("salary"));;
- }
- }else{
- System.out.println("No user exist----------");
- }
- }
- /* GET */
- private static void getUser(){
- System.out.println("Testing getUser API----------");
- RestTemplate restTemplate = new RestTemplate();
- User user = restTemplate.getForObject(REST_SERVICE_URI+"/user/1", User.class);
- System.out.println(user);
- }
- /* POST */
- private static void createUser() {
- System.out.println("Testing create User API----------");
- RestTemplate restTemplate = new RestTemplate();
- User user = new User(0,"Sarah",51,134);
- URI uri = restTemplate.postForLocation(REST_SERVICE_URI+"/user/", user, User.class);
- System.out.println("Location : "+uri.toASCIIString());
- }
- /* PUT */
- private static void updateUser() {
- System.out.println("Testing update User API----------");
- RestTemplate restTemplate = new RestTemplate();
- User user = new User(1,"Tomy",33, 70000);
- restTemplate.put(REST_SERVICE_URI+"/user/1", user);
- System.out.println(user);
- }
- /* DELETE */
- private static void deleteUser() {
- System.out.println("Testing delete User API----------");
- RestTemplate restTemplate = new RestTemplate();
- restTemplate.delete(REST_SERVICE_URI+"/user/3");
- }
- /* DELETE */
- private static void deleteAllUsers() {
- System.out.println("Testing all delete Users API----------");
- RestTemplate restTemplate = new RestTemplate();
- restTemplate.delete(REST_SERVICE_URI+"/user/");
- }
- public static void main(String args[]){
- listAllUsers();
- getUser();
- createUser();
- listAllUsers();
- updateUser();
- listAllUsers();
- deleteUser();
- listAllUsers();
- deleteAllUsers();
- listAllUsers();
- }
- }
重启服务器,运行上面的程序。
- Testing listAllUsers API-----------
- User : id=1, Name=Sam, Age=30, Salary=70000.0
- User : id=2, Name=Tom, Age=40, Salary=50000.0
- User : id=3, Name=Jerome, Age=45, Salary=30000.0
- User : id=4, Name=Silvia, Age=50, Salary=40000.0
- Testing getUser API----------
- User [id=1, name=Sam, age=30, salary=70000.0]
- Testing create User API----------
- Location : http://localhost:8080/Spring4MVCCRUDRestService/user/5
- Testing listAllUsers API-----------
- User : id=1, Name=Sam, Age=30, Salary=70000.0
- User : id=2, Name=Tom, Age=40, Salary=50000.0
- User : id=3, Name=Jerome, Age=45, Salary=30000.0
- User : id=4, Name=Silvia, Age=50, Salary=40000.0
- User : id=5, Name=Sarah, Age=51, Salary=134.0
- Testing update User API----------
- User [id=1, name=Tomy, age=33, salary=70000.0]
- Testing listAllUsers API-----------
- User : id=1, Name=Tomy, Age=33, Salary=70000.0
- User : id=2, Name=Tom, Age=40, Salary=50000.0
- User : id=3, Name=Jerome, Age=45, Salary=30000.0
- User : id=4, Name=Silvia, Age=50, Salary=40000.0
- User : id=5, Name=Sarah, Age=51, Salary=134.0
- Testing delete User API----------
- Testing listAllUsers API-----------
- User : id=1, Name=Tomy, Age=33, Salary=70000.0
- User : id=2, Name=Tom, Age=40, Salary=50000.0
- User : id=4, Name=Silvia, Age=50, Salary=40000.0
- User : id=5, Name=Sarah, Age=51, Salary=134.0
- Testing all delete Users API----------
- Testing listAllUsers API-----------
- No user exist----------
完整的例子
更新pom.xml添加项目依赖
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.websystique.springmvc</groupId>
- <artifactId>Spring4MVCCRUDRestService</artifactId>
- <packaging>war</packaging>
- <version>1.0.0</version>
- <name>Spring4MVCCRUDRestService Maven Webapp</name>
- <properties>
- <springframework.version>4.2.0.RELEASE</springframework.version>
- <jackson.version>2.5.3</jackson.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>${springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>${jackson.version}</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.1.0</version>
- </dependency>
- </dependencies>
- <build>
- <pluginManagement>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.2</version>
- <configuration>
- <source>1.7</source>
- <target>1.7</target>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.4</version>
- <configuration>
- <warSourceDirectory>src/main/webapp</warSourceDirectory>
- <warName>Spring4MVCCRUDRestService</warName>
- <failOnMissingWebXml>false</failOnMissingWebXml>
- </configuration>
- </plugin>
- </plugins>
- </pluginManagement>
- <finalName>Spring4MVCCRUDRestService</finalName>
- </build>
- </project>
User Service
- package com.websystique.springmvc.service;
- import java.util.List;
- import com.websystique.springmvc.model.User;
- public interface UserService {
- User findById(long id);
- User findByName(String name);
- void saveUser(User user);
- void updateUser(User user);
- void deleteUserById(long id);
- List<User> findAllUsers();
- void deleteAllUsers();
- public boolean isUserExist(User user);
- }
- package com.websystique.springmvc.service;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.concurrent.atomic.AtomicLong;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import com.websystique.springmvc.model.User;
- @Service("userService")
- @Transactional
- public class UserServiceImpl implements UserService{
- private static final AtomicLong counter = new AtomicLong();
- private static List<User> users;
- static{
- users= populateDummyUsers();
- }
- public List<User> findAllUsers() {
- return users;
- }
- public User findById(long id) {
- for(User user : users){
- if(user.getId() == id){
- return user;
- }
- }
- return null;
- }
- public User findByName(String name) {
- for(User user : users){
- if(user.getName().equalsIgnoreCase(name)){
- return user;
- }
- }
- return null;
- }
- public void saveUser(User user) {
- user.setId(counter.incrementAndGet());
- users.add(user);
- }
- public void updateUser(User user) {
- int index = users.indexOf(user);
- users.set(index, user);
- }
- public void deleteUserById(long id) {
- for (Iterator<User> iterator = users.iterator(); iterator.hasNext(); ) {
- User user = iterator.next();
- if (user.getId() == id) {
- iterator.remove();
- }
- }
- }
- public boolean isUserExist(User user) {
- return findByName(user.getName())!=null;
- }
- private static List<User> populateDummyUsers(){
- List<User> users = new ArrayList<User>();
- users.add(new User(counter.incrementAndGet(),"Sam",30, 70000));
- users.add(new User(counter.incrementAndGet(),"Tom",40, 50000));
- users.add(new User(counter.incrementAndGet(),"Jerome",45, 30000));
- users.add(new User(counter.incrementAndGet(),"Silvia",50, 40000));
- return users;
- }
- public void deleteAllUsers() {
- users.clear();
- }
- }
Model (模型)类
- package com.websystique.springmvc.model;
- public class User {
- private long id;
- private String name;
- private int age;
- private double salary;
- public User(){
- id=0;
- }
- public User(long id, String name, int age, double salary){
- this.id = id;
- this.name = name;
- this.age = age;
- this.salary = salary;
- }
- public long getId() {
- return id;
- }
- public void setId(long id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public double getSalary() {
- return salary;
- }
- public void setSalary(double salary) {
- this.salary = salary;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + (int) (id ^ (id >>> 32));
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- User other = (User) obj;
- if (id != other.id)
- return false;
- return true;
- }
- @Override
- public String toString() {
- return "User [id=" + id + ", name=" + name + ", age=" + age
- + ", salary=" + salary + "]";
- }
- }
- package com.websystique.springmvc.configuration;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
- @Configuration
- @EnableWebMvc
- @ComponentScan(basePackages = "com.websystique.springmvc")
- public class HelloWorldConfiguration {
- }
- package com.websystique.springmvc.configuration;
- import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
- public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
- @Override
- protected Class<?>[] getRootConfigClasses() {
- return new Class[] { HelloWorldConfiguration.class };
- }
- @Override
- protected Class<?>[] getServletConfigClasses() {
- return null;
- }
- @Override
- protected String[] getServletMappings() {
- return new String[] { "/" };
- }
- }
为你的REST API添加CORS支持
” XMLHttpRequest cannot load http://abc.com/bla. Origin http://localhost:12345 is not allowed by Access-Control-Allow-Origin.”
- package com.websystique.springmvc.configuration;
- import java.io.IOException;
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServletResponse;
- public class CORSFilter implements Filter {
- public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
- System.out.println("Filtering on...........................................................");
- HttpServletResponse response = (HttpServletResponse) res;
- response.setHeader("Access-Control-Allow-Origin", "*");
- response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
- response.setHeader("Access-Control-Max-Age", "3600");
- response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
- chain.doFilter(req, res);
- }
- public void init(FilterConfig filterConfig) {}
- public void destroy() {}
- }
- package com.websystique.springmvc.configuration;
- import javax.servlet.Filter;
- import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
- public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
- @Override
- protected Class<?>[] getRootConfigClasses() {
- return new Class[] { HelloWorldConfiguration.class };
- }
- @Override
- protected Class<?>[] getServletConfigClasses() {
- return null;
- }
- @Override
- protected String[] getServletMappings() {
- return new String[] { "/" };
- }
- @Override
- protected Filter[] getServletFilters() {
- Filter [] singleton = { new CORSFilter()};
- return singleton;
- }
- }
相关推荐
在Spring框架中,可以通过Spring MVC的@RestController注解和@RequestMapping等注解轻松实现RESTful API的开发,提供JSON、XML等数据格式的接口。 【Demo项目结构】 "SPRING-MVC-MQ-CXF-REST_Demo"这个项目很可能是...
4. **创建RESTful API**:通过`@RestController`注解,我们可以创建处理HTTP请求的REST端点。比如,使用`@RequestMapping`定义GET请求,`@RequestBody`和`@ResponseBody`用于处理JSON或XML数据的输入和输出。 5. **...
**Spring 3.0 实现 REST 实例** 在软件开发中,REST(Representational State Transfer,表述性状态转移)是一种广泛采用的Web服务设计风格,它以简洁、灵活的方式提供了客户端与服务器之间的通信。Spring框架,...
这篇博客“基于Spring和Spring MVC实现可跨域访问的REST服务”深入探讨了如何使用这两个流行的Java框架来创建这样的服务。Spring作为核心框架提供依赖注入和整体架构支持,而Spring MVC作为其Web模块,专门用于构建...
在Spring 4.0中,`@RestController`是一个重要的新特性,它标志着Spring MVC框架对RESTful服务支持的增强。`@RestController`是`@Controller`和`@ResponseBody`两个注解的组合,使得处理HTTP请求变得更加简洁和高效...
"Spring MVC REST Demo"是一个示例项目,旨在展示如何在Spring MVC框架中实现RESTful Web服务。下面将详细讨论Spring MVC与RESTful API的结合,以及如何创建和测试此类服务。 首先,Spring MVC是Spring框架的一部分...
在本篇中,我们将深入探讨如何使用Spring MVC构建RESTful接口,主要关注的是在实际项目中创建第一个REST风格接口的源代码实现。REST(Representational State Transfer)是一种网络应用程序的设计风格和开发方式,...
2. **Spring MVC与REST**:Spring MVC提供了一套优雅的方式来实现RESTful服务。通过使用`@RestController`注解,我们可以创建处理HTTP请求的控制器类,而`@RequestMapping`、`@GetMapping`、`@PostMapping`等注解则...
- 如果涉及到数据库操作,可以结合Spring Data JPA简化数据库访问,通过`@Repository`、`@Entity`、`@Service`和`@Transactional`等注解实现ORM。 通过"spring mvc rest基础学习demo",初学者可以快速掌握如何使用...
【标题】"mavin spring4 mvc mybatis 整合 带事务,REST风格" 涉及到的是一个基于Java技术栈的Web应用程序开发,整合了Maven、Spring4 MVC、MyBatis框架,并且实现了RESTful API设计以及事务管理。下面将对这些关键...
在上述代码中,`@RestController`是Spring4 MVC提供的特殊注解,它结合了`@Controller`和`@ResponseBody`,表示该类中的方法会自动将结果对象转换为HTTP响应体。`@GetMapping`和`@PostMapping`分别对应HTTP的GET和...
标题中的“Spring MVC – Easy REST-Based JSON Services with @ResponseBody”是指使用Spring MVC框架构建基于REST的JSON服务,并通过使用`@ResponseBody`注解来简化这一过程。REST(Representational State ...
Spring 3.0 MVC 实现 REST 代码是一个关于构建基于 RESTful 风格的 Web 服务的示例。REST(Representational State Transfer)是一种架构风格,常用于设计网络应用程序,强调通过统一资源标识符(URI)来访问资源,...
以上就是"spring4MVC+Hibernate4实例(增删改查)"中的主要技术点,这个实例为初学者提供了学习Spring MVC和Hibernate整合的实践平台,并且通过REST风格实现了Web服务。通过理解和实践这些概念,开发者可以更好地...
`spring-rest.rar`是一个包含相关代码的压缩包,用于演示如何将Spring MVC应用于RESTful服务的实现。 首先,让我们了解REST(Representational State Transfer)的核心理念。REST是一种网络应用程序的设计风格和...
本教程将深入探讨如何利用Spring 3 MVC实现RESTful服务。 首先,理解REST的基本原则至关重要。RESTful架构有以下关键概念: 1. 资源(Resource):在Web服务中,资源是核心概念,通过URI(统一资源标识符)来唯一...
Spring3MVC-REST-HelloWorld 是一个基础的示例,用于展示如何在Spring框架的MVC模块中实现RESTful Web服务。这个实例是初学者理解Spring MVC与REST结合使用的理想起点。REST(Representational State Transfer)是一...
在这个小例子中,我们将探讨如何使用Spring MVC来实现REST接口。 首先,让我们理解Spring MVC的基本架构。Spring MVC通过DispatcherServlet作为前端控制器,接收HTTP请求,然后根据请求映射找到相应的Controller...
【标题】"mavin spring4 mvc mybatis 整合 带REST风格"涉及到的知识点主要涵盖了Java Web开发中的三大核心框架:Maven、Spring 4 MVC以及MyBatis,以及RESTful服务的设计和实现。以下是这些技术的详细介绍: 1. ...
Spring MVC通过@RequestMapping的produces和consumes属性,以及@Consumes和@Produces注解,支持构建符合REST原则的服务,能够处理JSON、XML等多种内容类型。 11. **模板引擎集成** Spring MVC可以轻松地与各种...