简体   繁体   English

DELETE方法无法正确删除实体

[英]DELETE method won't properly delete entity

I'm trying to delete an object that I get by using JSON through a POST request. 我正在尝试删除通过POST请求使用JSON获得的对象。 Then by using the objects id, I want to delete it using a DELETE request. 然后,通过使用对象id,我想使用DELETE请求将其DELETE The DELETE request responds back with a 204 No Content . DELETE请求以204 No Content响应。

That's all good but when I use the GET request to get all existing objects, the object that was deleted is still there. 很好,但是当我使用GET请求获取所有现有对象时,被删除的对象仍然存在。

I delete the object by including its id as a @PathParam() . 我通过将其id包含在@PathParam()删除该对象。

I have tested the operation just using plain Java and it works fine. 我已经使用纯Java测试了该操作,并且工作正常。

DatabaseClass.java 数据库类

public class DatabaseClass {

    private static Map<Integer, Message> messages = new HashMap<>();
    private static Map<String, Profile> profiles = new HashMap<>();

    public static Map<Integer, Message> getAllMessages() {
        return messages;
    }

    public static Map<String, Profile> getAllProfiles() {
        return profiles;
    }
}

Message.java Message.java

@XmlRootElement
public class Message {

    private Integer id;
    private String author;
    private String text;
    private Date dateCreated;

    public Message() {}

    public Message(String author, String text) {
        this.author = author;
        this.text = text;
        this.dateCreated = new Date();
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("ID: " + id + "\n");
        sb.append("Author: " + author + "\n");
        sb.append("Text: " + text + "\n");
        sb.append("Date Created: " + dateCreated + "\n\n");

        return sb.toString();
    }
}

MessageService.java MessageService.java

public class MessageService {

    private Map<Integer, Message> messages = DatabaseClass.getAllMessages();

    public MessageService() {}

    public List<Message> getMessages() {
        return new ArrayList<>(messages.values());
    }

    public Message getMessage(Integer id) {
        if(!messages.containsKey(id)) {
            throw new NotFoundException("Message was not found.");
        }

        return messages.get(id);
    }

    public Message addMessage(Message message) {
        message.setId(messages.size() + 1);
        message.setDateCreated(new Date());

        messages.put(message.getId(), message);

        return messages.get(message.getId());
    }

    public Message updateMessage(Message message) {
        if(message.getId() <= 0) { 
            return null;
        }
        messages.put(message.getId(), message);

        return messages.get(message.getId());
    }

    public Message deleteMessage(Integer id) {
        return messages.remove(id);
    }
}

MessageResource.java MessageResource.java

@Path("messages")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class MessageResource {

    private MessageService mService = new MessageService();

    @POST
    public Message addMessage(Message message) {
        return mService.addMessage(message);
    }

    @GET
    public List<Message> getMessages() {
        return mService.getMessages();
    }

    @GET
    @Path("{messageId}")
    public Message getMessage(@PathParam("messageId") Integer messageId) {
        return mService.getMessage(messageId);
    }

    @PUT
    @Path("{messageId}")
    public Message updateMessage(@PathParam("messageId") Integer messageId, 
            Message message) {
        return mService.updateMessage(message);
    }

    @DELETE
    @Path("{messagesId}")
    public Message deleteMessage(@PathParam("messageId") Integer messageId) {
        return mService.deleteMessage(messageId);
    }
}

I also have another question on here and it contains another object called Profile . 我在这里还有另一个问题 ,它包含另一个名为Profile对象。 The ProfileResource.java class has the POST, PUT, GET, and DELETE methods. ProfileResource.java类具有POST,PUT,GET和DELETE方法。 If I deleted a profile the response I get back is 200 OK and not 204 No Content which I find very weird because I should be getting a 204 No Content . 如果我删除了个人资料,我得到的答复是200 OK而不是204 No Content ,我觉得很奇怪,因为我应该得到204 No Content Either way the profile gets deleted successfully. 无论哪种方式,配置文件都会成功删除。

Because MessageService is being created every request which calls the constructor in resources. 因为正在创建MessageService所以每个请求都会在资源中调用构造函数。

private MessageService mService = new MessageService();

If you create a new message using POST and DELETE, the same will work. 如果使用POST和DELETE创建新消息,则同样可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM