[英]How to return HTTP error status code *and* content in Struts2
我有一個接收 http 文件上傳並以 JSON 確認響應的控制器。 如果上傳處理有任何問題,我想返回一個 HTTP 錯誤狀態代碼(例如 403 表示格式錯誤的請求或 500 表示一般處理錯誤),但我也想發送詳細錯誤的 JSON 列表消息。 我知道如何返回 500 錯誤(感謝這篇文章),但我不知道如何返回 500 代碼並仍然發送內容。
這是我的代碼的一個片段(它沒有做我想要的):
@Action(value = "upload", results = {
@Result(name = SUCCESS, type = "freemarker", location = "results.ftl", params = { "contentType", "text/plain" }),
@Result(name = ERROR, type = "freemarker", location = "error.ftl", params = { "contentType", "text/plain" }),
@Result(name = ERROR, type = "httpheader", params = { "status", "500" })
})
public String upload() {
//do stuff
if(CollectionUtils.isEmpty(getActionErrors()) {
return SUCCESS;
} else {
return ERROR;
}
}
這是一篇舊帖子,但如果您使用的是我建議使用的 Struts-JSON,您可以簡單地返回一個包含您想要的錯誤對象並附加如下狀態代碼:
@Result(name = ERROR, type="json",
params = {"root","errorResponse", "statusCode", "500"}
)
2015 年 2 月 24 日更新: ty-danielson 的答案是正確的。 它適用於 JSON 響應,這是我想要的,即使我使用 freemarker 模板來生成它們(另一個壞主意)。
如果您真的想要一個帶有錯誤狀態代碼的 freemarker 模板:我的原始答案仍然是錯誤的方法,因為從操作方法內部訪問 ServletResponse 是一種錯誤的形式。 Struts 的內置 FreemarkerResult 不接受狀態代碼參數,但您可以通過對其進行子類化來輕松添加此功能(示例來自GBIF 項目)
/**
* Same as FreemarkerResult, but with added 'statusCode' parameter.
* (don't forget to register this result type in struts-config.xml)
*/
public class FreemarkerHttpResult extends FreemarkerResult {
private int status;
public int getStatusCode() {
return status;
}
public void setStatusCode(int status) {
this.status = status;
}
@Override
protected void postTemplateProcess(Template template, TemplateModel data) throws IOException {
super.postTemplateProcess(template, data);
if (status >= 100 && status < 600) {
HttpServletResponse response = ServletActionContext.getResponse();
response.setStatus(status);
}
}
}
然后像這樣聲明你的動作映射:
@Action(value = "myAction", results = {
@Result(name = SUCCESS, type = "freemarker", location = "results.ftl"),
@Result(name = ERROR, type = "freemarkerhttp", location = "error.ftl", params = { "statusCode", "500"})
})
public String myAction() {
//do stuff, then return SUCCESS or ERROR
}
所以,我不確定從 struts2 的角度來看這是否“正確”,但這里有一個解決方案,它實現了我返回 http 錯誤代碼的目標,同時仍然能夠呈現 freemarker 模板。 我會將此標記為答案,直到出現更好的答案。
@Action(value = "upload", results = {
@Result(name = SUCCESS, type = "freemarker", location = "results.ftl", params = { "contentType", "text/plain"}),
@Result(name = ERROR, type = "freemarker", location = "error.ftl", params = { "contentType", "text/plain"})
})
public String upload() {
try {
//do stuff
} Catch(SomeExceptionType ex) {
addActionError("you did something bad");
HttpServletResponse response = ServletActionContext.getResponse();
response.setStatus(400);
}
}
如果有人正在尋找已接受答案的struts.xml版本,我將把它留在這里:
<action name="upload"
class="...">
<exception-mapping exception="java.lang.Exception" result="error"/>
<result name="error" type="json">
<param name="statusCode">418</param>
</result>
<result name="Success" type="json" />
</action>
我沒有時間設置測試 Struts2 應用程序,但可能只有一個錯誤結果:
@Result(name = ERROR, type = "freemarker", location = "error.ftl",
params = { "contentType", "text/plain", "status", "500" })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.