在本节中,我们将实现一个删除方法来删除用户资源。

步骤 1: 打开 UserDaoService.java 文件。

步骤 2: 创建一个方法来删除用户资源。

UserDaoService.java

package cn.javatiku.server.main.user;  
import java.util.ArrayList;  
import java.util.Date;  
import java.util.Iterator;  
import java.util.List;  
import org.springframework.stereotype.Component;  
@Component  
public class UserDaoService   
{  
public static int usersCount=5;  
//creating an instance of ArrayList  
private static List<User> users=new ArrayList<>();  
//static block   
static  
{  
//adding users to the list  
users.add(new User(1, "John", new Date()));  
users.add(new User(2, "Robert", new Date()));  
users.add(new User(3, "Adam", new Date()));  
users.add(new User(4, "Andrew", new Date()));  
users.add(new User(5, "Jack", new Date()));  
}  
//method that retrieve all users from the list  
public List<User> findAll()  
{  
return users;  
}  
//method that adds a user in the list   
public User save(User user)  
{  
if(user.getId()==null)  
{  
user.setId(++usersCount);  
}  
users.add(user);  
return user;  
}  
//method that find a particular user from the list  
public User findOne(int id)  
{  
for(User user:users)  
{  
if(user.getId()==id)  
return user;  
}  
return null;  
}  
//method that delete a user resource  
public User deleteById(int id)  
{  
Iterator<User> iterator = users.iterator();  
while(iterator.hasNext())  
{  
User user=iterator.next();  
if(user.getId()==id)  
{  
iterator.remove();  
return user; //returns the deleted resource back  
}  
}  
return null;  
}  
}  

步骤 3: 打开 UserResource.java 文件并创建一个删除映射以删除用户资源。

UserResource.java

package cn.javatiku.server.main.user;  
import java.net.URI;  
import java.util.List;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.http.ResponseEntity;  
import org.springframework.web.bind.annotation.DeleteMapping;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.PathVariable;  
import org.springframework.web.bind.annotation.PostMapping;  
import org.springframework.web.bind.annotation.RequestBody;  
import org.springframework.web.bind.annotation.RestController;  
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;  
@RestController  
public class UserResource   
{  
@Autowired  
private UserDaoService service;  
@GetMapping("/users")  
public List<User> retriveAllUsers()  
{  
return service.findAll();  
}  
//retrieves a specific user detail  
@GetMapping("/users/{id}")  
public User retriveUser(@PathVariable int id)  
{  
User user= service.findOne(id);  
if(user==null)  
//runtime exception  
throw new UserNotFoundException("id: "+ id);  
return user;  
}  
//method that delete a user resource  
//if the user deleted successfully it returns status 200 OK otherwise 404 Not Found  
@DeleteMapping("/users/{id}")  
public void deleteUser(@PathVariable int id)  
{  
User user= service.deleteById(id);  
if(user==null)  
//runtime exception  
throw new UserNotFoundException("id: "+ id);  
}  
//method that posts a new user detail and returns the status of the user resource  
@PostMapping("/users")  
public ResponseEntity<Object> createUser(@RequestBody User user)    
{  
User sevedUser=service.save(user);    
URI location=ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(sevedUser.getId()).toUri();  
return ResponseEntity.created(location).build();  
}  
}  

步骤 4: 打开 Postman,选择 DELETE 请求,并指定要删除的用户 id。然后点击 Send 按钮。

restful-web-services-delete-resource.png

它会删除用户 id 为 3,并返回 Status: 200 OK。再次发送 Get 请求。它会显示除用户 3 以外的所有用户。

restful-web-services-delete-resource2.png

在下面的图片中,我们试图删除用户 id 为 9,但该用户不存在。因此它返回了 Status: 404 Not Found

restful-web-services-delete-resource3.png

标签: spring, Spring教程, Spring语言学习, Spring框架, Spring框架教程, Spring框架高级教程, spring boot, spring boot入门教程, spring boot学习教程, spring boot下载, spring boot框架入门, spring boot面试题, spring boot笔试题, spring boot学习指南, spring boot技术