非常特殊:HTTP状态405-不允许的方法 [英] Very peculiar :HTTP Status 405 - Method Not Allowed

查看:633
本文介绍了非常特殊:HTTP状态405-不允许的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[使用Apache Tomcat/7.0.27]

看来我只会收到这个错误

It seems I only get this error

  • ( HTTP状态405-不允许的方法)

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

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

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

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

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

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

客户端:

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

推荐答案

浏览器将为您的资源发出GET请求-您已在服务器端将其声明为@PUT并从中将其放入您的客户端代码.浏览器正在尝试获取"(或GET)资源,而@GET则不存在

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

这篇关于非常特殊:HTTP状态405-不允许的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆