首先我们来了解一下RESTful API
请求类型 |
URL |
功能说明 |
GET |
/users |
查询用户列表 |
POST |
/users |
创建一个用户 |
PUT |
/users/id |
根据id修改一个用户 |
DELETE |
/users/id |
根据id删除一个用户 |
主要是用Spring MVC来实现一组对User对象操作的RESTful API,配合注释详细说明在Spring MVC中如何映射HTTP请求、如何传参、如何编写单元测试。
项目结构
User实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public class UserPojo {
private String id;
private String name;
private String happay;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getHappay() { return happay; }
public void setHappay(String happay) { this.happay = happay; }
public String getId() { return id; }
public void setId(String id) { this.id = id; }
|
Conntroller层类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| @RestController @RequestMapping(value = "/user") public class UserController {
static Map< String, UserPojo> map = Collections.synchronizedMap( new HashMap<String,UserPojo>());
@RequestMapping(value = "/",method = RequestMethod.GET ) public List<UserPojo> getUser(){ List<UserPojo> listUser = new ArrayList<UserPojo>(map.values()); return listUser; }
@RequestMapping(value = "/",method = RequestMethod.POST) public String postUser(@ModelAttribute UserPojo userPojo){ map.put(userPojo.getId(),userPojo); return "success"; }
@RequestMapping(value = "/{id}",method = RequestMethod.GET) public UserPojo getByUserId(@PathVariable String id){ return map.get(id); }
@RequestMapping(value = "/{id}",method = RequestMethod.PUT) public String putByIdUser(@PathVariable String id,@ModelAttribute UserPojo userPojo){ UserPojo userPojo1 = new UserPojo(); userPojo1.setName(userPojo.getName()); userPojo1.setHappay(userPojo.getHappay()); map.put(id,userPojo1); return "success"; }
@RequestMapping(value = "/{id}",method = RequestMethod.DELETE) public String deleteByIdUser(@PathVariable String id){ map.remove(id); return "success"; }
}
|
测试结果
下面针对该Controller编写测试用例验证正确性,具体如下。当然也可以通过浏览器插件等进行请求提交验证。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| @RunWith(SpringRunner.class) @SpringBootTest public class UserControllerTests {
private MockMvc mockMvc;
@Before public void setUp() throws Exception { mockMvc = MockMvcBuilders.standaloneSetup(new UserController()).build(); }
@Test public void testUserController() throws Exception {
RequestBuilder requestBuilder = null; requestBuilder = get("/user/"); mockMvc.perform(requestBuilder) .andExpect(status().isOk()) .andExpect(content().string(equalTo("[]")));
requestBuilder = post("/user/") .param("id","1") .param("name","wusha") .param("happay","basketball"); mockMvc.perform(requestBuilder) .andExpect(content().string(equalTo("success")));
requestBuilder = get("/user/"); mockMvc.perform(requestBuilder) .andExpect(status().isOk()) .andExpect(content().string(equalTo("[{\"id\":\"1\",\"name\":\"wusha\",\"happay\":\"basketball\"}]")));
requestBuilder = get("/user/1"); mockMvc.perform(requestBuilder) .andExpect(content().string(equalTo("{\"id\":\"1\",\"name\":\"wusha\",\"happay\":\"basketball\"}")));
requestBuilder = put("/user/1") .param("name","shasha") .param("happay","football"); mockMvc.perform(requestBuilder) .andExpect(content().string(equalTo("success")));
requestBuilder = delete("/user/1"); mockMvc.perform(requestBuilder) .andExpect(content().string(equalTo("success")));
}
}
|
注解
最后再来复习一下springMVC的常用注解也就是我项目中用到的
- @Controller:修饰class,用来创建处理http请求的对象
- @RestController:Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseBody来配合,如果直接用@RestController替代@Controller就不需要再配置@ResponseBody,默认返回json格式。
- @RequestMapping:配置url映射
参数绑定的注解
- @PathVariable是 映射 URL 绑定的占位符,通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。
- @ModelAttribute,运用在参数上,会将客户端传递过来的参数按名称注入到指定对象中,并且会将这个对象自动加入ModelMap中,便于View层使用;
运用在方法上,会在每一个@RequestMapping标注的方法前执行,如果有返回值,则自动将该返回值加入到ModelMap中;
- @RequestParam,主要用于将请求参数区域的数据映射到控制层方法的参数上