2013-02-27 21 views
0

我的RESTful Web服務這給JSON響應工作的錯誤代碼的RESTful Web服務的響應,如果ID不存在

@GET 
@Produces("application/json") 
public Site getSite() { 
    return (Site)siteFacade.find(Integer.parseInt(id)); 

} 

這是我的ID

讓現場信息的方法,我得到了以下輸出當存在用於輸入ID NO數據= 11

GET Request Failed Request Failed --> Status: (204) Response: { 

} 

現在我想即響應字段包含錯誤代碼正確消息= 204,如「無效請求」或「ID不存在」,其中是否需要更改,請幫助我們

+0

[對錯誤代碼的RESTful web服務響應]的可能重複(http://stackoverflow.com/questions/15111231/restful-webservices-response-for-error-code) – TheWhiteRabbit 2013-02-27 12:13:47

回答

0

這個怎麼樣。

一般來說,如果你要求的東西,就是沒有找到你給404來表示(這也是在REST風格的靈):

@GET 
@Produces("application/json") 
public Site getSite() { 
    Site site = (Site) siteFacade.find(Integer.parseInt(id)); 
    if (site == null) { 
    return Response.status(Response.Status.NOT_FOUND).build(); 
    } 
    return site; 
} 

如果您在迴應主體中,你會添加一些東西需要一個消息像這樣,筆者認爲:

@GET 
@Produces("application/json") 
public Site getSite() { 
    Site site = (Site) siteFacade.find(Integer.parseInt(id)); 
    if (site == null) { 
    return Response.status(Response.Status.NOT_FOUND).entity("No item with this id found").build(); 
    } 
    return site; 
} 
相關問題