RestController
/**
* @RestController: 等价 @Controller + @ResponseBody
* @RequestMapping:请求映射,在Spring4.0注解开发中对RequestMapping进行丰富,分别按照请求类型
* - @PostMapping 新增
* - @GetMapping 查询
* - @PutMapping 修改
* - @DeleteMapping 删除
* @RequestParam:接收表单参数
* @RequestPart:在请求体同时接收 文件和json数据
* @RequestBody:仅仅接收json数据
*
*/
@RestController
@RequestMapping(value = "/restUserManager")
public class RestUserController {
private static final Logger LOGGER= LoggerFactory.getLogger(RestUserController.class);
@Autowired
private IUserService userService;
@PostMapping(value = "/registerUser")
public User registerUser(@RequestPart(value = "user") User user,
@RequestParam(value = "multipartFile",required = false) MultipartFile multipartFile) throws IOException {
if(multipartFile!=null){
String fileName=multipartFile.getOriginalFilename();
String suffix=fileName.substring(fileName.lastIndexOf("."));
File tmpFile = File.createTempFile(fileName.substring(0,fileName.lastIndexOf(".")),suffix);
System.out.println(tmpFile.getName());
tmpFile.delete();
}
userService.saveUser(user);
return user;
}
@PostMapping(value = "/userLogin")
public User userLogin(@RequestBody User user) {
return userService.queryUserByNameAndPassword(user);
}
@PutMapping(value = "/updateUser")
public void updateUser(@RequestPart(value = "user") User user,
@RequestParam(value = "multipartFile",required = false) MultipartFile multipartFile) throws IOException {
if(multipartFile!=null){
String fileName=multipartFile.getOriginalFilename();
String suffix=fileName.substring(fileName.lastIndexOf("."));
File tmpFile = File.createTempFile(fileName.substring(0,fileName.lastIndexOf(".")),suffix);
System.out.println(tmpFile.getName());
tmpFile.delete();
}
//更新用户信息
userService.updateUser(user);
}
@DeleteMapping(value = "/deleteUserByIds")
public void delteUserByIds(@RequestParam(value = "ids") Integer[] ids){
userService.deleteByUserIds(ids);
}
@GetMapping(value = "/queryUserById")
public User queryUserById(@RequestParam(value = "id") Integer id){
//从数据库中查询
return userService.queryUserById(id);
}
@GetMapping(value = "/queryUserByPage")
public List queryUserByPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNow,
@RequestParam(value = "rows",defaultValue = "10") Integer pageSize,
@RequestParam(value = "column",required = false) String column,
@RequestParam(value = "value",required = false) String value){
HashMap results = new HashMap<>();
results.put("total",userService.queryUserCount(column,value));
results.put("rows",userService.queryUserByPage(pageNow,pageSize,column,value));
return userService.queryUserByPage(pageNow,pageSize,column,value);
}
@GetMapping(value = "/queryUserCount")
public Integer queryUserCount(@RequestParam(value = "column",required = false) String column,
@RequestParam(value = "value",required = false) String value){
return userService.queryUserCount(column,value);
}
}
添加RestTemplate的Bean
@SpringBootApplication
@MapperScan(basePackages = "com.baizhi.dao")
public class UserModelApplication {
public static void main(String[] args) {
SpringApplication.run(UserModelApplication.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
提前运行Application
运用RestTemplate测试接口
@SpringBootTest(classes = {UserModelApplication.class})
@RunWith(SpringRunner.class)
public class RestUserControllerTest {
@Autowired
private RestTemplate restTemplate;
private String urlPrefix="http://localhost:8989/user/restUserManager";
/*
@PostMapping(value = "/registerUser")
public User registerUser(@RequestPart(value = "user") User user,
@RequestParam(value = "multipartFile",required = false) MultipartFile multipartFile) throws IOException {
*/
@Test
public void testRegisterUser(){
String url=urlPrefix+"/registerUser";
//模拟表单数据
MultiValueMap formData=new LinkedMultiValueMap();
User user =new User(null,"张晓磊",true,"123456",new Date(),"aa.png","[email protected]");
formData.add("user",user);
//模拟文件上传
FileSystemResource fileSystemResource=new FileSystemResource("C:\\Users\\Administrator\\Desktop/a11111.jpg");
formData.add("multipartFile",fileSystemResource);
User registerUser = restTemplate.postForObject(url, formData, User.class);
assertNotEquals("用户ID",registerUser.getId());
}
/*
@PostMapping(value = "/userLogin")
public User userLogin(@RequestBody User user)
*/
@Test
public void testUserLogin(){
String url=urlPrefix+"/userLogin";
//模拟表单数据
User user=new User();
user.setName("张晓磊");
user.setPassword("123456");
User loginUser = restTemplate.postForObject(url, user, User.class);
assertNotEquals("用户ID",loginUser.getId());
}
/*
@PostMapping(value = "/addUser")
public User addUser(@RequestPart(value = "user") User user,
@RequestParam(value = "multipartFile",required = false) MultipartFile multipartFile) throws IOException
*/
@Test
public void testAddUser(){
String url=urlPrefix+"/addUser";
//模拟表单数据
MultiValueMap formData=new LinkedMultiValueMap();
User user =new User(null,"温晓琪",true,"123456",new Date(),"aa.png","[email protected]");
formData.add("user",user);
//模拟文件上传
FileSystemResource fileSystemResource=new FileSystemResource("/Users/admin/Desktop/head.png");
formData.add("multipartFile",fileSystemResource);
User dbUser = restTemplate.postForObject(url, formData, User.class);
assertNotNull("用户ID",dbUser.getId());
}
/*
@PutMapping(value = "/updateUser")
public void updateUser(@RequestPart(value = "user") User user,
@RequestParam(value = "multipartFile",required = false) MultipartFile multipartFile) throws IOException {
*/
@Test
public void testUpdateUser(){
String url=urlPrefix+"/updateUser";
//模拟表单数据
MultiValueMap formData=new LinkedMultiValueMap();
User user =new User(null,"温晓琪",false,"123456",new Date(),"aa.png","[email protected]");
user.setId(27);
formData.add("user",user);
//模拟文件上传
FileSystemResource fileSystemResource=new FileSystemResource("/Users/admin/Desktop/head.png");
formData.add("multipartFile",fileSystemResource);
restTemplate.put(url, formData);
}
/*
@DeleteMapping(value = "/deleteUserByIds")
public void delteUserByIds(@RequestParam(value = "ids") Integer[] ids)
*/
@Test
public void testDeleteUser(){
String url=urlPrefix+"/deleteUserByIds?ids={id}";
Map parameters=new HashMap();
parameters.put("id","4,5,6");
restTemplate.delete(url,parameters);
}
/*
@GetMapping(value = "/queryUserByPage")
public List queryUserByPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNow,
@RequestParam(value = "rows",defaultValue = "10") Integer pageSize,
@RequestParam(value = "column",required = false) String column,
@RequestParam(value = "value",required = false) String value)
*/
@Test
public void testQueryUserByPage(){
String url=urlPrefix+"/queryUserByPage?page={page}&rows={rows}&column={column}&value={value}";
Map params= new HashMap();
params.put("page",1);
params.put("rows",10);
params.put("column","name");
params.put("value","小");
User[] users=restTemplate.getForObject(url,User[].class,params);
assertNotNull("用户数据",users);
}
/*
@GetMapping(value = "/queryUserCount")
public Integer queryUserCount(@RequestParam(value = "column",required = false) String column,
@RequestParam(value = "value",required = false) String value)
*/
@Test
public void testQueryUserCount(){
String url=urlPrefix+"/queryUserCount?column={column}&value={value}";
//模拟表单数据
Map parameters=new HashMap<>();
parameters.put("column","name");
parameters.put("value","晓");
Integer count = restTemplate.getForObject(url, Integer.class,parameters);
assertNotNull(count);
}
/*
@GetMapping(value = "/queryUserById")
public User queryUserById(@RequestParam(value = "id") Integer id)
*/
@Test
public void testQueryUserById(){
String url=urlPrefix+"/queryUserById?id={id}";
//模拟表单数据
Map parameters=new HashMap<>();
parameters.put("id","2");
User user = restTemplate.getForObject(url, User.class,parameters);
assertNotNull("用户ID",user.getId());
}
}