SpringBoot入门(三)(Spring Boot构建RESTful API)

学习资料:http://blog.didispace.com/springbootrestfulapi/

写今天的博客之前,先将今天会用到的注解简单的解释一下:

名称 描述
@RequestController 使用了这个注解的类会被看作一个controller-使用@RequestMapping的方法有一个默认的@ResponseBody注解。
@RequestMapping(“/user”) 处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
@RequestMapping 中 value 对应映射路径
@RequestMapping 中 method 定义了请求方法
@RequestMapping 中 consumes 定义了只接受请求头部 Content-Type 属性,上例中 content-Type=”application/json”
@RequestMapping 中 produces 定义了只接受请求头部 Accept 属性,上例中 Accept=”application/json”
@RequestBody 将请求内容自动映射成为 Java 对象
@PathVariable 将请求内容自动映射成为基础类型

如果还有对RESTful 不是很清楚的同学请移步:http://www.runoob.com/w3cnote/restful-architecture.html

下面我们尝试使用Spring MVC来实现一组对User对象操作的RESTful API,配合注释详细说明在Spring MVC中如何映射HTTP请求、如何传参、如何编写单元测试。

RESTful API具体设计如下:

我们先定义一个简单的实体类:

public class User {
		
	private long id;
	private String name;
	private Integer age;
    
        //省略构造和get,set
}

然后实现对User对象的操作接口:

package com.zy.SpringBoot.Controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zy.SpringBoot.entity.User;

@RestController
@RequestMapping(value="/users")
public class UserController {
	
	// 创建线程安全的Map 
	static Map<Long, User> map = Collections.synchronizedMap(new HashMap<Long, User>());
	
	
	@RequestMapping(value="/" , method=RequestMethod.GET)
	public List<User> getUsers(){
		// 处理"/users/"的GET请求,用来获取用户列表
        // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
		List<User> list = new ArrayList<>(map.values());
		return list;
	}
	
	
	@RequestMapping(value="/" , method=RequestMethod.POST)
	public String getUser(@ModelAttribute User user){
		// 处理"/users/"的POST请求,用来创建User
        // 除了@ModelAttribute绑定参数之外,还可以通过@RequestParam从页面中传递参数
		map.put(user.getId(), user);
		return "success";
	}
	
	@RequestMapping(value="/{id}" , method=RequestMethod.GET)
	public User getUser(@PathVariable Long id){
		 // 处理"/users/{id}"的GET请求,用来获取url中id值的User信息
        // url中的id可通过@PathVariable绑定到函数的参数中
		return map.get(id);
	}
	
	
	@RequestMapping(value="/{id}" , method=RequestMethod.PUT)
	public String putUser(@PathVariable Long id , @ModelAttribute User user){
		 // 处理"/users/{id}"的PUT请求,用来更新User信息
		User u = map.get(map.get(id));
		u.setName(user.getName());
		u.setAge(user.getAge());
		map.put(id, u);
		return "success";
	}
	
	@RequestMapping(value="/{id}" , method=RequestMethod.DELETE)
	public String deleteUser(@PathVariable Long id){
		 // 处理"/users/{id}"的DELETE请求,用来删除User
		map.remove(id);
		return "success";
	}
}

然后可以用接口测试工具去进行测试,我使用的是postman.


至此,我们通过引入web模块(没有做其他的任何配置),就可以轻松利用Spring MVC的功能,以非常简洁的代码完成了对User对象的RESTful API的创建以及单元测试的编写。其中同时介绍了Spring MVC中最为常用的几个核心注解:@Controller,@RestController,RequestMapping以及一些参数绑定的注解:@PathVariable,@ModelAttribute,@RequestParam等。


github代码地址:https://github.com/zyllycald/SpringBoot

猜你喜欢

转载自blog.csdn.net/weixin_38946418/article/details/80846738