1

I am new to restful webservices and following java restful api tutorials. all the HTTP requests are working fine except the DELETE request. i am facing the same issue as described in this link. REST - HTTP Status 405 - Method Not Allowed

also , in the predefined header values postman is showing allow →GET,OPTIONS,PUT(image link is below)

i am following the correct syntax and url format as per the specification(pasted in image) but delete is not working.

allowed : GET,OPTIONS,PUT.img

please let me know where i am missing.

Edit : Source code :

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.koushik.javabrains.messenger.model.Message;
import org.koushik.javabrains.messenger.service.MessageService;



@Path("/messages")
public class MessageResource {
    MessageService messageService = new MessageService();
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Message> getMessages(){
        System.out.println("Hello There");
        List<Message> returnedList = messageService.getAllMessages();
        System.out.println(returnedList);
        return returnedList;
    }
    @GET
    @Path("/{messageId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Message getMessage(@PathParam("messageId") long messageId){

        System.out.println("Message returned");
        return messageService.getMessage(messageId);

    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Message addMessage(Message message){
        return messageService.addMessage(message);
    }

    @PUT
    @Path("/{messageId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Message updateMessage(@PathParam("messageId") long id , Message message){
        message.setId(id);
        return messageService.updateMessage(message);
    }

    @DELETE
    @Path("/messageId")
    @Produces(MediaType.APPLICATION_JSON)
    public void deleteMessage(@PathParam("messageId") long id){
        System.out.println("Hello There");
        messageService.removeMessage(id);
    }
}






package org.koushik.javabrains.messenger.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.koushik.javabrains.messenger.database.DatabaseClass;
import org.koushik.javabrains.messenger.model.Message;

public class MessageService {

    private Map<Long , Message> messages = DatabaseClass.getMessages();

    public MessageService(){

        messages.put(1L, new Message(1, "Hello World" , "koushik"));
        messages.put(2L, new Message(2, "Hello Jersey" , "Koushik"));

    }

    public List<Message> getAllMessages(){

        return new ArrayList<Message>(messages.values());
    }

    public Message getMessage(long id){
        Message m = messages.get(id);
        System.out.println("Value of message "+m);
        return m;
    }

    public Message addMessage(Message message){

        message.setId(messages.size()+1);
        messages.put(message.getId(), message);
        return message;
    }

    public Message updateMessage(Message message){

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

    public Message removeMessage(long id){
        return messages.remove(id);
    }
}






package org.koushik.javabrains.messenger.database;

import java.util.HashMap;
import java.util.Map;

import org.koushik.javabrains.messenger.model.Message;
import org.koushik.javabrains.messenger.model.Profile;

public class DatabaseClass {

    private static Map<Long , Message> messages = new HashMap();

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

    public static Map<Long , Message> getMessages() {

        return messages;
    }

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

}

except delete other requests are working fine. on sending DELETE the server is returning HTTP -405 method not allowed.

also on sending delete request then in the header values postman is showing allow →GET,OPTIONS,PUT

Thanks

Gaurabh
  • 21
  • 1
  • 5
  • Normally people would ask you if you have access to the server, or to recheck your url, and that perhaps the server doesn't allow DELETE operations. But I'm quite sure you're following Javabrains' tutorial, which I went through myself, and I know your url is fine. It looks like the error is somewhere in your server code.. mind showing that as well? – Ray Nov 12 '17 at 02:38
  • Hi Ray, I have attached server code please look at this. i am able to perform other operations except delete. – Gaurabh Nov 12 '17 at 05:31
  • as mentioned in the post ... write the application type as *application/json* and not *text/html* – Rohit Kumar Nov 13 '17 at 08:16

1 Answers1

0

Make sure you do not have any URL Parameters entered in Postman, also make sure you do not pass any headers in postman. The delete request should be plain without and of these.

Also, the string message ID need to be put in curly brackets. That would be the reason for your 405 mostly. Change path annotation as below and try.

@Path("/{messageId}")

Awanish
  • 327
  • 1
  • 3
  • 14