📜  实现DELETE方法以删除用户资源

📅  最后修改于: 2021-01-12 00:49:08             🧑  作者: Mango

实现DELETE方法删除用户资源

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

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

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

package com.javatpoint.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 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 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 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 com.javatpoint.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 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 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 request,然后指定要删除的用户ID。现在单击发送按钮。

它删除用户标识:3并返回状态:200 Ok 。再次发送Get请求。它显示除用户3外的所有用户。

在下图中,我们试图删除用户ID:9,该用户ID不存在。因此,它返回状态:404未找到