2013-02-27 48 views
4

我在linux發行版(Raspbian,Tomcat 7)上運行我的代碼時出現問題。這個問題沒有出現在Windows/Tomcat 7/Eclipse下的測試環境中:REST服務返回HTTP 204(Tomcat/Linux)

我的web服務只是返回HTTP 204而沒有其他東西。該日誌不包含任何請求期間出現錯誤的跡象。它應該像在測試環境中那樣使用json進行響應。

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
version="3.0"> 
<display-name>***</display-name> 
<welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
</welcome-file-list> 
<listener> 
    <listener-class>**.useravailability.UserAvailabilityServletContextListener</listener-class> 
</listener> 
<servlet-mapping> 
    <servlet-name>REST Service</servlet-name> 
    <url-pattern>/REST/*</url-pattern> 
</servlet-mapping> 
<servlet> 
    <servlet-name>REST Service</servlet-name> 
    <servlet-class>com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class> 
    <init-param> 
     <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> 
     <param-value>true</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

</web-app> 

usersService.xml

package **.webservice; 

import java.util.ArrayList; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import com.google.gson.Gson; 

import **.model.AccessManager; 

@Path("/usersService") 
public class UsersService { 
@GET 
@Produces("application/json") 
public String getUser() {  
    String user = null; 
    ArrayList<String> userList = new ArrayList<String>(); 
    try {  
     userList = new AccessManager().getUser(); 
     Gson gson = new Gson(); 
     user = gson.toJson(userList);   
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return user; 
} 


} 

我缺少什麼?爲什麼它在測試環境中工作? 謝謝!

+0

您能告訴我們您是如何從客戶端調用Web服務的嗎? HTTP 204意味着請求沒有問題,服務器正確處理了它,但在標題後沒有內容可以返回。也許你正在申請除application/json以外的內容類型? [http codes and meaningings](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html) – David

+0

感謝您的回答: 我使用POSTMAN,一個REST客戶端。標題是:Content-Type:application/json;接受:application/json。 結果爲空。 – user1259201

+2

是否有可能發生異常並返回null,但堆棧跟蹤沒有進入日誌文件? –

回答

0

謝謝大家! 我錯過了一條錯誤消息,指出缺少jdbc驅動程序。 我忘了在上傳之前將它添加到構建路徑。

5

如果返回null,Jersey將返回HTTP 204。

我認爲這是最好的行爲。

就你而言,你可能會引發異常,所以用戶爲空。

,如果你想返回錯誤代碼(這可能是更合乎邏輯的),你可以替換

catch (Exception e) { 
     e.printStackTrace(); 
    } 

通過

catch (Exception e) { 
     throw new WebApplicationException(404); 
    } 
如果你想聲明的是,用戶無法找到

,或者如果你想返回一個服務器端的錯誤代碼,則使用5XX HTTp。