静态Web服务的PUT和POST获取405方法不允许错误 [英] PUT and POST getting 405 Method Not Allowed Error for Restful Web Services

查看:73
本文介绍了静态Web服务的PUT和POST获取405方法不允许错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个简单的Restful Web服务,该服务根据Accept标头返回JSON或XML.我正在使用Spring,Maven和WebLogic Server.我从这篇帖子 http:中获取了示例://software.sawano.se/2012/03/combining-json-and-xml-in-restful-web.html 并尝试对其进行改进.GET和DELETE都适用于JSON和XML.但是PUT和POST给出"405不允许的方法"错误.我正在尝试使用Chrome Extension Advanced Rest Client对此进行测试.下面是Response标头.

I am trying to set up a simple Restful Web-Service which returns either JSON or XML according to the Accept header. I am using Spring, Maven and WebLogic Server. I took the example from this post http://software.sawano.se/2012/03/combining-json-and-xml-in-restful-web.html and tried to improve on it. GET and DELETE is working for both JSON and XML.But PUT and POST gives a "405 Method Not Allowed" Error. I am trying to test this with the Chrome Extension Advanced Rest Client. below is the Response headers.

Status
405 Method Not Allowed Show explanation Loading time: 327

Request headers 
Accept: Application/json
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
Content-Type: application/x-www-form-urlencoded 
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8


Response headers 
Connection: close
Date: Tue, 11 Feb 2014 15:17:24 GMT 
Content-Length: 34 
Content-Type: text/html 
Allow: GET, DELETE 
X-Powered-By: Servlet/2.5 JSP/2.1
Raw
Parsed

我给出的请求正文如下:

the request body that i give is below:

{
id: 1
name: "manga"
}

我的控制器类如下所示:

My Controller Class is as shown below:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.Set;

@Controller
@RequestMapping("/users")
public class RESTController {
Userlist obj2;
boolean flag=false;
private Logger logger = LoggerFactory.getLogger(getClass());

@RequestMapping(value = "/{id}",method = RequestMethod.GET)
@ResponseBody
public User getUser(@PathVariable int id, @RequestHeader("Accept") String acceptHeader) {
    User temp = new User();
    if(obj2==null)
    {
        temp= new User(0, "Null");
    }
    else   {
        Set<User> set1= obj2.getUsers();
        for(User a:set1)
        {
            if(id==a.getId()) temp=a;
        }
      }
    logger.trace("Serving resource for Accept header: {}", acceptHeader);
    return temp;
}

@RequestMapping(value="",method = RequestMethod.GET)
@ResponseBody
public Userlist getUsers(){
    if(flag==false){
    User new1=new User(1,"Rob");
    User new2=new User(2,"VAN");
    User new3=new User(3,"DAM");
    User new4=new User(4,"Helio");
 Set<User> obj1 =new HashSet<User>();
    obj1.add(new1);
    obj1.add(new2);
    obj1.add(new3);
    obj1.add(new4);
    obj2=new Userlist(obj1);
    flag=true;
    }
    return obj2;
}

@RequestMapping(value="/{id}",method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.OK)
public void deleteUser(@PathVariable int id){
    Set<User> set1= obj2.getUsers();
   for(User a:set1)
    {
        if(id==a.getId()) set1.remove(a);
     }
    Userlist obj3=new Userlist(set1);
    obj2=obj3;
    //return obj3;
}

@RequestMapping(value="/{id}",method = RequestMethod.PUT, consumes = "Application/json")
@ResponseStatus(HttpStatus.OK)
public void updateUser(@PathVariable int id, @RequestBody User temp){
    System.out.println("Inside the put function");
        if(temp==null){System.out.println("This is a Null for PUT");}
 }
}

现在我的PUT中没有任何东西.

Right now I do not have anything in PUT.

推荐答案

好吧,显然我不得不更改PUT调用函数 updateUser .我删除了 @Consumes @RequestMapping ,并且还向函数添加了 @ResponseBody .所以我的方法看起来像这样:

Well, apparently I had to change my PUT calling function updateUser. I removed the @Consumes, the @RequestMapping and also added a @ResponseBody to the function. So my method looked like this:

@RequestMapping(value="/{id}",method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public void updateUser(@PathVariable int id, @RequestBody User temp){
    Set<User> set1= obj2.getUsers();
    for(User a:set1)
    {
        if(id==a.getId())
        {
            set1.remove(a);
            a.setId(temp.getId());
            a.setName(temp.getName());
            set1.add(a);
        }
    }
    Userlist obj3=new Userlist(set1);
    obj2=obj3;
}

它奏效了!!!谢谢大家的答复.

And it worked!!! Thank you all for the response.

这篇关于静态Web服务的PUT和POST获取405方法不允许错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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