Very peculiar :HTTP Status 405 - Method Not Allowe

2020-06-09 00:44发布

[using Apache Tomcat/7.0.27]

It seems I only get this error

  • (HTTP Status 405 - Method Not Allowed)

when I try to make a REST request directly from the browser.

E.g by pasting this in the address bar :

http://localhost:8080/restExample/rest/catalog/video/14951/hello

When I run my test client Main.java everything works fine.

Any ideas as to why it wont let me execute a REST through the browser?

Client Side:

public class Main{
    public static void main(String [] args){
       ClientConfig config = new DefaultClientConfig();
       Client client = Client.create(config);   
       WebResource service = client.resource(getBaseURI(_package));
       runPutRequest(service,"video/128/This is the content with the new description");
    }
}

...
private static void runPutRequest(WebResource service,String path){
        String response = service.path("rest/catalog/"+path).accept(MediaType.APPLICATION_XML).put(String.class);
        System.out.println("Post Response :"+response);
    }

Server side:

@PUT
@Path("/video/{video-id}/{short-descr}")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_XML)
public Video updateVideo(@PathParam("video-id") int contentid,
                         @PathParam("short-descr") String descr)
{       
    //Video video = searchByContentId(contentid);
    Video video = videoMap.get(contentid);
    video.setDescription(descr);

    videoMap.put(contentid,video);

    if( videoMap.get(contentid) != null){
        return videoMap.get(contentid);
    }else{
         throw new UnsupportedOperationException("NO object found");
    }
}

3条回答
小情绪 Triste *
2楼-- · 2020-06-09 01:30

The browser will issue a GET request for your resource - which you have declared as a @PUT on the server-side and are PUT-ing to it from your client-side code. The browser is trying to 'fetch' (or GET) the resource and nothing exists for @GET

查看更多
别忘想泡老子
3楼-- · 2020-06-09 01:37

Generally, the Browser uses GET HTTP method to make requests. Your server side component is only capable to response to PUT requests, and that’s why you get that error code.

查看更多
We Are One
4楼-- · 2020-06-09 01:38

There exist REST clients for browsers that are capable of doing PUT, POST, and DELETE requests. I prefer Simple REST Client for Chrome.

查看更多
登录 后发表回答