14

[使用 Apache Tomcat/7.0.27]

看来我只收到此错误

  • HTTP 状态 405 - 方法不允许

当我尝试直接从浏览器发出 REST 请求时。

例如,将其粘贴到地址栏中:

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

当我运行我的测试客户端 Main.java 时,一切正常。

关于为什么它不允许我通过浏览器执行 REST 的任何想法?

客户端:

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

服务器端:

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

3 回答 3

19

浏览器将为您的资源发出 GET 请求 - 您已@PUT在服务器端声明为 a 并从客户端代码向其 PUT。浏览器正在尝试“获取”(或 GET)资源,而 @GET 不存在任何内容

于 2012-05-18T20:57:28.410 回答
6

通常,浏览器使用 GET HTTP 方法发出请求。您的服务器端组件只能响应 PUT 请求,这就是您收到该错误代码的原因。

于 2012-05-18T20:57:11.160 回答
2

存在用于能够执行 PUT、POST 和 DELETE 请求的浏览器的 REST 客户端。我更喜欢Chrome 的Simple REST Client

于 2012-07-27T13:23:38.677 回答