gpt4 book ai didi

java - Struts2-Rest : Manual HTTP 5xx doesn't return content

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:55 26 4
gpt4 key购买 nike

我正在尝试手动返回带有自定义错误消息的 HTTP 4xx 或 5xx。它对 GET 非常有效,但对 PUT 和 POST 返回空内容。预先感谢任何可以建议如何让 PUT 和 POST 工作的人!

这些是我对网络服务的调用:
GET/webapp/orders/A.json - 返回 HTTP 500 和错误消息作为内容
PUT/webapp/orders/A.json - 返回 HTTP 500 和 content-length=0

getModel() 在这两个实例中都被调用,消息只是没有返回到 Chrome 进行 PUT。

这是产生错误的示例代码:

订单 Controller

package com.test.controller;

import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import org.apache.struts2.convention.annotation.Namespace;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/rest")
public class OrdersController implements ModelDriven<Object> {

// GET /orders/A
public HttpHeaders show() {
return new DefaultHttpHeaders().withStatus(500);
}

// PUT /orders/A
public HttpHeaders update() {
return new DefaultHttpHeaders().withStatus(500);
}

public void setId(String id) {
// Nothing
}

public Object getModel() {
return new String("{ \"message\": \"An error occurred.\"}");
}

}

结构

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<constant name="struts.convention.action.suffix" value="Controller"/>
<constant name="struts.convention.action.mapAllMatches" value="true"/>
<constant name="struts.convention.default.parent.package" value="rest-default"/>
<constant name="struts.convention.package.locators" value="controller"/>
</struts>

网络

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="starter" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Orders</display-name>

<filter>
<filter-name>action2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/rest/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

</web-app>

AngularJS 测试工具

var orderApp = angular.module('orderApp', []);

orderApp.run(function($http) {
$http.get("rest/orders/A.json");
$http.put("rest/orders/A.json", {"orderId":"A"} );
});

环境:Struts2 (2.3.16.3), Rest-Plugin, Convention, Java7, Tomcat7, Maven

最佳答案

添加<constant name="struts.rest.content.restrictToGET" value="false"/>到struts.xml就解决了问题。感谢@ontk 的 this回答另一个问题。感谢所有试图帮助我的人。

关于java - Struts2-Rest : Manual HTTP 5xx doesn't return content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26456325/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com