Very peculiar :HTTP Status 405 - Method Not Allowed

夙愿已清 提交于 2020-03-17 10:16:29

问题


[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");
    }
}

回答1:


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




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/10659586/very-peculiar-http-status-405-method-not-allowed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!