2011-02-07 266 views
0

我正在使用Jersey/Java來開發我的REST服務。我需要返回的XML表示我CarStore:REST Web服務中的Http 204錯誤(澤西島)

@XmlRootElement 
public class CarStore { 
private List<Car> cars; 

public List<Car> getCars() { 
    return cars; 
} 
public void setCars(List<Car> cars) { 
    this.cars = cars; 
} 

這是我的車對象:

@XmlRootElement 
> public class Car { 
private String carName; 
private Specs carSpecs; 
private Category carCategory; 
public String getCarName() { 
    return carName; 
} 
public void setCarName(String carName) { 
    this.carName = carName; 
} 
public Specs getCarSpecs() { 
    return carSpecs; 
} 
public void setCarSpecs(Specs carSpecs) { 
    this.carSpecs = carSpecs; 
} 
public Category getCarCategory() { 
    return carCategory; 
} 
public void setCarCategory(Category carCategory) { 
    this.carCategory = carCategory; 
} 

} 

規格和類別枚舉這樣的:

@XmlRootElement 
> public enum Category { 

SEDANS, COMPACTS, WAGONS, HATCH_HYBRIDS, SUVS, CONVERTIBLES, COMPARABLE; 
} 

我的資源類:

@GET 
@Produces({MediaType.APPLICATION_XML}) 
public CarStore getCars() 
{ 
    return CarStoreModel.instance.getAllCars(); 
} 

我的球衣的客戶是:

WebResource service = client.resource(getBaseURI()); 
System.out.println(service.path("rest").path("cars").accept(
MediaType.APPLICATION_XML).get(String.class)); 

我收到的Http 204錯誤在非常久遠的客戶端異常訪問:

com.sun.jersey.api.client.UniformInterfaceException

任何想法?謝謝 !

編輯:我還沒有開發模型類......我剛剛初始化一些汽車對象作爲虛擬數據,並把它們放在carstore。在這裏顯示所有的課程將非常笨拙。 順便說一句,抱歉寫204錯誤..這只是我得到一個例外,讓我這麼認爲。

+5

但HTTP 204不指示錯誤。 – Arvin 2011-02-07 17:52:28

回答

0

我相信你已經看到了UniformInterfaceException因爲你getCars()功能不返回一個HTTP響應主體。根本問題是您的Car List沒有被JAXB轉換爲XML,因爲它缺少@XmlElement註釋。

你getCars()函數應該是:

@GET 
@Produces(MediaType.APPLICATION_XML) 
public CarStore getCars() { 
    // myCarStore is an instance of CarStore   
    return myCarStore.getCars(); 
} 

和CarStore您的汽車名單應該定義:

@XmlElement(name="car") 
private List<Car> cars; 
3

我猜這個異常與響應代碼(204)無關,因爲204是一個成功的條件,指示「無內容」。

+0

UniformInterfaceException被拋出[「當HTTP響應的狀態碼指示不是預期的響應時]](http://jersey.java.net/nonav/apidocs/1.1.1-ea/jersey/com/sun /jersey/api/client/UniformInterfaceException.html),所以基本上任何響應,但200會導致異常被拋出。 – tronman 2012-04-09 21:47:12

0

是什麼你回來的XML格式?我不確定getAllCars是做什麼的,但是你可以使用像Fiddler這樣的東西來幫助你查看流量並查看返回給客戶端的內容以及它是否在適當的格式等。

0

在你的客戶端代碼中,資源路徑正確?確保getBaseURI正在返回一個值。

也許嘗試:

Client client = new Client(); 
WebResource resource = client.resource(getBaseURI()); 
CarStore carStore = resource.path("/rest/cars").accept(MediaType.APPLICATION_XML).get(CarStore.class); 
0

你是不是缺少的資源類@Path註釋?

@GET 
@Path("cars") 
@Produces({ MediaType.APPLICATION_XML }) 
public CarStore getCars() { 
    return CarStoreModel.instance.getAllCars(); 
} 

檢查處的REST WS是通過把一個斷點在你getCars()方法(或放一個的System.out.println)安裝你所期望的一個網址,以確保它實際上被調用。

0

在返回HTTP 204時,似乎在澤西島有一個硬編碼檢查來拋出UniformInterfaceException。

最好的解決方案是'修復'其餘的服務器,以便它永遠不會返回null。例如返回空列表或未設置值的類。

否則你將需要捕捉UniformInterfaceException這實在是太醜了

if (getStatus() == 204) { 
     throw new UniformInterfaceException(this); 
    } 

此處瞭解詳情: http://grepcode.com/file/repo1.maven.org/maven2/com.sun.jersey/jersey-client/1.17.1/com/sun/jersey/api/client/ClientResponse.java#ClientResponse.getEntity%28java.lang.Class%2Cjava.lang.reflect.Type%29