Java PUT 和 POST 为 Restful Web 服务获取 405 Method Not Allowed 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21706078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PUT and POST getting 405 Method Not Allowed Error for Restful Web Services
提问by Some Guy Really
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.htmland 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.
我正在尝试设置一个简单的 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 Method Not Allowed”错误。我正在尝试使用 Chrome Extension Advanced Rest Client 对此进行测试。下面是响应头。
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");}
}
}
Right now I do not have anything in PUT.
现在我在 PUT 中没有任何东西。
采纳答案by Some Guy Really
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:
好吧,显然我不得不改变我的 PUT 调用函数updateUser
。我删除了@Consumes
,@RequestMapping
并且还在@ResponseBody
函数中添加了 a 。所以我的方法是这样的:
@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.
它奏效了!!!谢谢大家的回应。
回答by aNish
Notice Allowed methods in the response
注意响应中允许的方法
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
It accepts only GET and DELETE. Hence, you need to tweak the server to enable PUT and POST as well.
它只接受 GET 和 DELETE。因此,您还需要调整服务器以启用 PUT 和 POST。
Allow: GET, DELETE
回答by weiyueli
I'm not sure if I am correct, but from the request header that you post:
我不确定我是否正确,但是从您发布的请求标头来看:
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
请求头
接受:申请/json
来源:chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
用户代理: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
内容类型:应用程序/x-www-form-urlencoded
Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8
it seems like you didn't config your request body to JSON type.
您似乎没有将请求正文配置为 JSON 类型。
回答by Solanki Vaibhav
I same thing happen with me, If your code is correct and then also give 405 error. this error due to some authorization problem. go to authorization menu and change to "Inherit auth from parent".
我也发生了同样的事情,如果你的代码是正确的,然后也会给出 405 错误。由于某些授权问题而导致此错误。转到授权菜单并更改为“从父级继承身份验证”。
回答by Zon
The problem is that POST method is forbidden for Nginx server's static files requests. Here is the workaround:
问题是 POST 方法被禁止用于 Nginx 服务器的静态文件请求。这是解决方法:
# Pass 405 as 200 for requested address:
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 404 /404.html;
error_page 403 /403.html;
error_page 405 =200 $uri;
}
If using proxy:
如果使用代理:
# If Nginx is like proxy for Apache:
error_page 405 =200 @405;
location @405 {
root /htdocs;
proxy_pass http://localhost:8080;
}
If using FastCGI:
如果使用 FastCGI:
location ~\.php(.*) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include /etc/nginx/fastcgi_params;
}
Browsers usually use GET, so you can use online tools like ApiTesterto test your requests.
浏览器通常使用 GET,因此您可以使用ApiTester等在线工具来测试您的请求。
Source
来源
回答by RAM237
In my case the form (which I cannot modify) was always sending POST.
While in my Web Service I tried to implement GET method (due to lack of documentation I expected that both are allowed).
在我的情况下,表单(我无法修改)总是发送 POST。
在我的 Web 服务中,我尝试实现 GET 方法(由于缺乏文档,我希望两者都被允许)。
Thus, it was failing as "Not allowed", since there was no method with POST type on my end.
因此,它失败为“不允许”,因为我没有使用 POST 类型的方法。
Changing @GET
to @POST
above my WS method fixed the issue.
更改@GET
为@POST
上面我的 WS 方法解决了这个问题。