一:编写用户详情服务
1.任务
@PathVariable隐射url片段到java方法的参数
在url声明中使用正则表达式
@JsonView控制json输出内容
二:@PathVariable
1.@PathVariable小测试
测试类
1 @Test 2 public void whenGetInfoSuccess() throws Exception { 3 //发送请求 4 mockMvc.perform(MockMvcRequestBuilders.get("/user/1") 5 .contentType(MediaType.APPLICATION_JSON_UTF8)) 6 .andExpect(MockMvcResultMatchers.status().isOk()) 7 .andExpect(MockMvcResultMatchers.jsonPath("$.username").value("tom")); 8 }
控制类
1 @RequestMapping(value="/user/{id}",method=RequestMethod.GET) 2 public User getInfo(@PathVariable(value="idid") String idid){ 3 System.out.println("id="+idid); 4 User user=new User(); 5 user.setUsername("tom"); 6 return user; 7 }
2.效果
三:url声明中的正则表达式
1.测试
测试类
1 /** 2 * 测试url声明的正则表达式 3 * @throws Exception 4 */ 5 @Test 6 public void whenGetInfoFail() throws Exception { 7 //发送请求 8 mockMvc.perform(MockMvcRequestBuilders.get("/user/aa") 9 .contentType(MediaType.APPLICATION_JSON_UTF8)) 10 .andExpect(MockMvcResultMatchers.status().is4xxClientError()); 11 }
控制类
只许传递的id为数字。
1 @RequestMapping(value="/user/{id:\d+}",method=RequestMethod.GET) 2 public User getInfo(@PathVariable(value="idid") String idid){ 3 System.out.println("id="+idid); 4 User user=new User(); 5 user.setUsername("tom"); 6 return user; 7 }
四:@JsonView
1.说明
以过滤序列化对象的字段属性,可以使你有选择的序列化对象
2.使用步骤
使用接口来声明多个视图
在值对象的get方法上指定视图
在controller方法上指定视图
3.按照步骤实现一个场景
4.程序
完成前两步
1 package com.cao.dto; 2 3 import com.fasterxml.jackson.annotation.JsonView; 4 5 public class User { 6 //接口 7 public interface UserSimpleView {}; 8 public interface UserDetailView extends UserSimpleView {}; //继承之后,可以展示父的所有 9 10 private String username; 11 private String password; 12 13 @JsonView(UserSimpleView.class) 14 public String getUsername() { 15 return username; 16 } 17 public void setUsername(String username) { 18 this.username = username; 19 } 20 21 @JsonView(UserDetailView.class) 22 public String getPassword() { 23 return password; 24 } 25 public void setPassword(String password) { 26 this.password = password; 27 } 28 29 }
完成第三步
1 package com.cao.web.controller; 2 3 import static org.mockito.Matchers.contains; 4 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import org.apache.commons.lang.builder.ReflectionToStringBuilder; 9 import org.apache.commons.lang.builder.ToStringStyle; 10 import org.springframework.data.domain.Pageable; 11 import org.springframework.data.web.PageableDefault; 12 import org.springframework.web.bind.annotation.PathVariable; 13 import org.springframework.web.bind.annotation.RequestMapping; 14 import org.springframework.web.bind.annotation.RequestMethod; 15 import org.springframework.web.bind.annotation.RequestParam; 16 import org.springframework.web.bind.annotation.RestController; 17 18 import com.cao.dto.User; 19 import com.cao.dto.UserQueryCondition; 20 import com.fasterxml.jackson.annotation.JsonView; 21 22 //此controller可以提供restful服务 23 @RestController 24 public class UserController { 25 @RequestMapping(value="/user",method=RequestMethod.GET) 26 @JsonView(User.UserSimpleView.class) 27 public List<User> query(UserQueryCondition condition,@PageableDefault(size=13) Pageable pageable){ 28 //反射的方法来打印 29 System.out.println(ReflectionToStringBuilder.toString(condition, ToStringStyle.MULTI_LINE_STYLE)); 30 // 31 System.out.println(pageable.getPageSize()); 32 System.out.println(pageable.getPageNumber()); 33 System.out.println(pageable.getSort()); 34 // 35 List<User> userList=new ArrayList<>(); 36 userList.add(new User()); 37 userList.add(new User()); 38 userList.add(new User()); 39 return userList; 40 } 41 42 @RequestMapping(value="/user/{id:\d+}",method=RequestMethod.GET) 43 @JsonView(User.UserDetailView.class) 44 public User getInfo(@PathVariable(value="id") String idid){ 45 System.out.println("id="+idid); 46 User user=new User(); 47 user.setUsername("tom"); 48 return user; 49 } 50 51 }
测试类一:
1 /* 2 * 测试几个常见的声明 3 */ 4 @Test 5 public void whenQuerySuccess() throws Exception { 6 //发送请求 7 String result=mockMvc.perform(MockMvcRequestBuilders.get("/user") 8 .param("username", "Job") 9 .param("age", "18") 10 .param("xxx", "XXX") 11 //分页,查第三页,每页15条,按照age降序 12 // .param("page", "3") 13 // .param("size", "15") 14 // .param("sort", "age,desc") 15 // 16 .contentType(MediaType.APPLICATION_JSON_UTF8)) 17 .andExpect(MockMvcResultMatchers.status().isOk()) 18 .andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(3)) 19 .andReturn().getResponse().getContentAsString(); 20 System.out.println("result="+result); 21 }
效果
测试类二
1 /** 2 * 测试pathVariable 3 * @throws Exception 4 */ 5 @Test 6 public void whenGetInfoSuccess() throws Exception { 7 //发送请求 8 String result=mockMvc.perform(MockMvcRequestBuilders.get("/user/1") 9 .contentType(MediaType.APPLICATION_JSON_UTF8)) 10 .andExpect(MockMvcResultMatchers.status().isOk()) 11 .andExpect(MockMvcResultMatchers.jsonPath("$.username").value("tom")) 12 .andReturn().getResponse().getContentAsString(); 13 System.out.println("result="+result); 14 }
效果
五:重构代码
1.重构服务
@RequestMapping("/user")
@GetMapping
1 package com.cao.web.controller; 2 3 import static org.mockito.Matchers.contains; 4 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import org.apache.commons.lang.builder.ReflectionToStringBuilder; 9 import org.apache.commons.lang.builder.ToStringStyle; 10 import org.springframework.data.domain.Pageable; 11 import org.springframework.data.web.PageableDefault; 12 import org.springframework.web.bind.annotation.GetMapping; 13 import org.springframework.web.bind.annotation.PathVariable; 14 import org.springframework.web.bind.annotation.RequestMapping; 15 import org.springframework.web.bind.annotation.RequestMethod; 16 import org.springframework.web.bind.annotation.RequestParam; 17 import org.springframework.web.bind.annotation.RestController; 18 19 import com.cao.dto.User; 20 import com.cao.dto.UserQueryCondition; 21 import com.fasterxml.jackson.annotation.JsonView; 22 23 //此controller可以提供restful服务 24 @RestController 25 @RequestMapping("/user") 26 public class UserController { 27 // @RequestMapping(value="/user",method=RequestMethod.GET) 28 @JsonView(User.UserSimpleView.class) 29 @GetMapping 30 public List<User> query(UserQueryCondition condition,@PageableDefault(size=13) Pageable pageable){ 31 //反射的方法来打印 32 System.out.println(ReflectionToStringBuilder.toString(condition, ToStringStyle.MULTI_LINE_STYLE)); 33 // 34 System.out.println(pageable.getPageSize()); 35 System.out.println(pageable.getPageNumber()); 36 System.out.println(pageable.getSort()); 37 // 38 List<User> userList=new ArrayList<>(); 39 userList.add(new User()); 40 userList.add(new User()); 41 userList.add(new User()); 42 return userList; 43 } 44 45 // @RequestMapping(value="/user/{id:\d+}",method=RequestMethod.GET) 46 @JsonView(User.UserDetailView.class) 47 @GetMapping(value="/{id:\d+}") 48 public User getInfo(@PathVariable(value="id") String idid){ 49 System.out.println("id="+idid); 50 User user=new User(); 51 user.setUsername("tom"); 52 return user; 53 } 54 55 }
2.重新跑测试类
这里主要是验证重构代码后的正确性。