Search code examples
javahttptomcatjerseyput

@PUT Jersey Error 405: Method not allowed


I am getting

Error 405: Method not allowed

MessageEnd.java:

package com.example.wordcount;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
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 javax.ws.rs.core.Response;

@Path("/jersey")
public class MessageEnd {

    @GET
    @Path("/getvalue/{word}")
    @Produces(MediaType.TEXT_PLAIN)
    public Response sayHello(@PathParam("word") String word){

        String output = " Count of word " + word;
        return Response.status(200).entity(output).build();
    }

    @PUT
    @Path("/sendvalue/{msg}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.TEXT_PLAIN)
    public Response sendMsg(@PathParam("msg") String msg){

        String output = "Received message= " + msg;
        return Response.status(200).entity(output).build();
    }

}

FYI, @GET is working fine.

I am using following URI:

http://localhost:8080/message/jersey/sendvalue/hi

Solution

  • You browser only sends GET requests when you type anything in the address bar. You can learn the difference between HTTP methods here: http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods

    You should use proper REST client in order to create PUT requests. The one I'm using and which is pretty good is Postman extension for Chrome: link

    The reason for the error above is that you're trying to send GET request to /sendvalue and there is nothing mapped for this method/path pair.