0

При выполнении примера с Rest API c сайта Oracle, при запросе @DELETE выпадает ошибка сервера 404 Not Found.

Вот сам код:

Note.java

public class Note {
    private final UUID id;
    private final String title;

    private Note(NoteBuilder builder){
        this.id = builder.id;
        this.title = builder.title;
    }

    public Note(){
        Note not = new Note.NoteBuilder().id().build();
        this.id = not.getId();
        this.title = not.getTitle();
    }

    public Note(UUID id, String title) {
        Note not = new Note.NoteBuilder().id()
                .title(title)
                .build();

        this.id = not.getId();
        this.title = not.getTitle();
    }

    public UUID getId(){
        return this.id;
    }

    public String getTitle() {
        return this.title;
    }

    @Override
    public String toString(){
        return "ID: " + id
                + " Title: " + title;
    }

    public static class NoteBuilder{
        private UUID id;
        private String title = "";

        public NoteBuilder id(){
            this.id =  UUID.randomUUID();
            return this;
        }

        public NoteBuilder title(String title){
            this.title = title;
            return this;
        }

        public Note build(){
            return new Note(this);
        }
    }
}

NoteList.java

public class NoteList {
    private static final CopyOnWriteArrayList<Note> nList = new CopyOnWriteArrayList<>();

    static {
        nList.add(
                new Note.NoteBuilder().id()
                        .title("1 заметка")
                        .build()
        );

        nList.add(
                new Note.NoteBuilder().id()
                        .title("2 заметка")
                        .build()
        );
    }
    private NoteList() {
    }

    public static CopyOnWriteArrayList<Note> getInstance(){
            return nList;
        }
}

NoteService.java

@Path("/notes")
public class NoteService {

    private final CopyOnWriteArrayList<Note> nList = NoteList.getInstance();

    @GET
    @Path("/all")
    @Produces(MediaType.APPLICATION_JSON)
    public String getAll(){
        Gson gson = new Gson();
       // return nList.toString();
        return gson.toJson(nList);
    }

    @POST 
    @Path("/add")
    @Produces(MediaType.APPLICATION_JSON)
    public Response addNote(Note note){
        nList.add(note);
        return Response.status(201).build();
    }

    @DELETE
    @Path("/remove/{id}")
    public Response deleteCustomer(@PathParam("id") UUID id){
        Predicate<Note> note = n -> n.getId() == id;
        if (nList.removeIf(note)) {
            return Response.status(Response.Status.OK).build();
        }else {
            return Response.status(Response.Status.NOT_FOUND).build();
        }

    }
}

При запросе @GET выводится следующее:

[{"id": "313695e0-72a5-4aab-9047-fbec516dab4f", "title":"1 заметка", "id": "9e4b9d09-347b-439c-aecf-b259b086f987","title":"2 заметка"}]

Правда почему то в не в виде дерева. Но тем не менее, это всё равно объект JSON, как я понимаю. Запрос @POST также выполняется без проблем. Но @DELETE - ошибка 404.

Запрос @DELETE подаю через сURL в таком виде:

curl -X DELETE -i http://localhost:8080/test_war_exploded/notes/remove/9e4b9d09-347b-439c-aecf-b259b086f987

Данная ошибка показывает на то, что объекта с этим id нет, но @GET же показывает что есть. Что не так-то, не могу понять этот момент?

1 ответ 1

1
Predicate<Note> note = n -> n.getId().equals(id);
0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.