[英]How to properly send an HTTP message to the client
我正在使用Java的RESTful Web服務。 如果出現問題,我需要一種向客戶端發送錯誤消息的好方法。
根據Javadoc , HttpServletResponse.setStatus(int status, String message)
由於消息參數的含義不明確而被棄用。
是否有一種首選方式來設置響應的狀態消息或“ 原因短語 ”? sendError(int, String)
方法不會這樣做。
編輯:澄清一下,我想修改HTTP狀態行,即"HTTP/1.1 404 Not Found"
,而不是正文內容。 具體來說,我想發送像"HTTP/1.1 400 Missing customerNumber parameter"
這樣的回復。
我不認為任何RESTful客戶端會期望查看原因,以找出問題所在; 我見過/使用過的大多數RESTful服務都會在響應正文中發送標准狀態信息和擴展消息。 sendError(int, String)
非常適合這種情況。
如果您使用的是Tomcat,請參閱設置org.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER:
http://tomcat.apache.org/tomcat-5.5-doc/config/systemprops.html
有關原始漏洞的一些詳細信息,請參閱此頁面:
http://www.securityfocus.com/archive/1/archive/1/495021/100/0/threaded
澄清之后,我在Tomcat中嘗試了這個。 執行
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "message goes here");
回報
HTTP/1.1 400 message goes here
作為回應的第一行。
您正在使用的servlet容器一定存在問題。
我對REST的“最佳實踐”並不熟悉。 但我知道這個概念是基於HTTP以及它應該如何自然地解決。 那么如何在主體內部使用mime類型和簡單文本來解決應用程序錯誤,例如'application / myapp-exception'和一些'Bla bla'? 您可以為此提供客戶端庫。
我不會將HTTP響應代碼用於應用程序錯誤。 因為我想知道什么是失敗的:無論是我的應用程序還是我的HTTP服務器。
(我希望,我也會在這里看到一些最佳實踐建議。)
你要完成什么並不是很清楚。 我的第一個想法是sendError,但是你說它沒有做你想要的...你看過創建一組“錯誤響應”,意思是特定的xml或JSON內容(或者你用作轉移語言的任何東西)包含錯誤消息或代碼以及任何其他有用信息?
我曾經為基於Spring-mvc的RESTful服務做了類似的事情,但它運行良好,但你必須抓住並處理每個異常,以防止客戶端獲得通用的500消息或其他東西。 Spring Exception Resolvers在這方面做得很好。
希望這會有所幫助......如果沒有,也許可以更清楚一點,你想要完成什么。 對不起,如果我是密集的,並且遺漏了明顯的東西
我認為sendError
應該這樣做,但你的應用服務器可能會失敗...很久以前IBM WebSphere 3.5在我身上失敗了,而Tomcat會傳播消息就好了; 請參閱JavaServer Pages(JSP)和JSTL - 錯誤頁面:保留標題“HTTP / 1.x 400我的消息”? 在Sun論壇上。
最終我使用了以下解決方法,但這是一種特定於JSP的,實際上可能是舊的:
<%@ page isErrorPage="true" %>
<%
// This attribute is NOT set when calling HttpResponse#setStatus and then
// explicitely incuding this error page using RequestDispatcher#include()
// So: only set by HttpResponse#sendError()
Integer origStatus =
(Integer)request.getAttribute("javax.servlet.error.status_code");
if(origStatus != null) {
String origMessage =
(String)request.getAttribute("javax.servlet.error.message");
if(origMessage != null) {
response.reset();
response.setContentType("text/html");
// deprecated, but works:
response.setStatus(origStatus.intValue(), origMessage);
// would yield recursive error:
// response.sendError(origStatus, origMessage);
}
}
%>
如果您碰巧使用Internet Explorer進行測試:禁用“顯示友好的HTTP錯誤消息”。 (當沒有禁用它時,IE有一些奇怪的要求HTML內容的某些最小長度,如果不滿足,將 - 或將 - 使IE顯示自己的錯誤消息。另請參閱注冊表項HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Internet Explorer\\Main\\ErrorThresholds
Microsoft的超文本傳輸協議錯誤消息說明中的 HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Internet Explorer\\Main\\ErrorThresholds
。)
在Spring驅動的Web應用程序中,在Tomcat上運行我使用以下bean:
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.springframework.beans.factory.InitializingBean;
public class SystemPropertiesInitializingBean implements InitializingBean {
private Map<String, String> systemProperties;
@Override
public void afterPropertiesSet() throws Exception {
if (null == systemProperties || systemProperties.isEmpty()) {
return;
}
final Set<Entry<String, String>> entrySet = systemProperties.entrySet();
for (final Entry<String, String> entry : entrySet) {
final String key = entry.getKey();
final String value = entry.getValue();
System.setProperty(key, value);
}
}
public void setSystemProperties(final Map<String, String> systemProperties) {
this.systemProperties = systemProperties;
}
}
在applicationContext.xml中:
<bean class="....SystemPropertiesInitializingBean">
<property name="systemProperties">
<map>
<entry key="org.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER" value="true"/>
</map>
</property>
</bean>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.