2014-02-11 131 views
8

我想設置一個簡單的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.BUT和POST會給出「405方法不允許」的錯誤。我正在嘗試使用Chrome擴展高級休息客戶端進行測試。下面是Response頭。PUT和POST獲取405方法不允許錯誤的Restful Web服務

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 

,我給請求體低於:現在我沒有在放東西

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

:如下圖所示

{ 
id: 1 
name: "manga" 
} 

我的控制器類。

+1

** Allow **響應只顯示「GET,DELETE」。這是一個服務器問題... –

回答

4

好吧,顯然我不得不改變我的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; 
} 

它的工作!謝謝大家的迴應。

9

通知允許的方法在響應

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 
+0

準確地說我的問題。在響應中檢查非常好允許的方法:) – Bankin

+0

謝謝,這是我需要的線索。 – djangofan

1

我不知道如果我是正確的,但是從請求頭,你後:

請求頭

接受:應用/ JSON

產地:chrome-extension:// hgmloofddffdnphfgcellkdfbfbjeloo

User-A gent:Mozilla/5.0(Windows NT 6.1; WOW64)爲AppleWebKit/537.36(KHTML,例如Gecko)鉻/ 29.0.1547.76 Safari瀏覽器/ 537.36

內容類型:應用/ X WWW的窗體-urlencoded

接受編碼:gzip,放氣, sdch Accept-Language:en-US,en; q = 0.8

好像你沒有將你的請求體配置成JSON類型。

相關問題