简体   繁体   中英

Very peculiar :HTTP Status 405 - Method Not Allowed

[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.

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM