我正在尝试设置一个简单的Restful Web-Service,它根据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.But 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
我给出的请求正文如下:
{
id: 1
name: "manga"
}
我的控制器类如下所示:
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中没有任何东西。
答案 0 :(得分:10)
注意响应中允许的方法
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
它只接受GET和DELETE。因此,您需要调整服务器以启用PUT和POST。
Allow: GET, DELETE
答案 1 :(得分:6)
好吧,显然我必须改变我的PUT调用函数updateUser
。我删除了@Consumes
,@RequestMapping
,并在函数中添加了@ResponseBody
。所以我的方法看起来像这样:
@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;
}
它有效!!!谢谢大家的回复。
答案 2 :(得分:2)
我不确定我是否正确,但是您发布的请求标题:
请求标题
接受:Application / json
原产地:chrome-extension:// hgmloofddffdnphfgcellkdfbfbjeloo
User-Agent:Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 29.0.1547.76 Safari / 537.36
内容类型:application / x-www-form-urlencoded
Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en; q = 0.8
好像你没有将你的请求体配置为JSON类型。
答案 3 :(得分:0)
我也发生了同样的事情, 如果您的代码正确,则还会给出405错误。由于某些授权问题导致此错误。 转到授权菜单,然后更改为“从父级继承身份验证”。
答案 4 :(得分:0)
问题是Nginx服务器的静态文件请求禁止使用POST方法。解决方法如下:
# 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 Nginx is like proxy for Apache:
error_page 405 =200 @405;
location @405 {
root /htdocs;
proxy_pass http://localhost:8080;
}
如果使用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;
}
浏览器通常使用GET,因此您可以使用ApiTester之类的在线工具来测试您的请求。
答案 5 :(得分:0)
就我而言,表格(我无法修改)一直在发送POST。
在我的Web服务中,我尝试实现GET方法(由于缺少文档,我希望两者都允许)。
因此,由于没有“ POST”类型的方法,因此失败了,因为“不允许”。
将WS方法上方的@GET
更改为@POST
可以解决此问题。