非常奇怪:HTTP状态405 - 不允许的方法

Fab*_*bii 13 java rest jaxb jersey

[使用Apache Tomcat/7.0.27]

我似乎只得到这个错误

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

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

例如,通过在地址栏中粘贴它:

http://localhost:8080/restExample/rest/catalog/video/14951/hello
Run Code Online (Sandbox Code Playgroud)

当我运行我的测试客户端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);
    }
Run Code Online (Sandbox Code Playgroud)

服务器端:

@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");
    }
}
Run Code Online (Sandbox Code Playgroud)

PhD*_*PhD 18

浏览器将为您的资源发出GET请求 - 您已@PUT在服务器端声明该资源并从客户端代码发送到该资源.浏览器正在尝试"获取"(或获取)资源,但@GET不存在任何内容


Car*_*ron 6

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