【问题标题】:Struts2-Rest: Manual HTTP 5xx doesn't return contentStruts2-Rest:手动 HTTP 5xx 不返回内容
【发布时间】:2014-10-19 22:56:36
【问题描述】:

我正在尝试手动返回带有自定义错误消息的 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。

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

订单控制器

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

【问题讨论】:

  • +1,第一个问题很好
  • 在url中添加第二个参数如PUT /orders/1/???就可以了
  • @RomanC 你能详细说明一下吗?将PUT /webapp/orders/1.json/aa 发送到网络服务会导致java.lang.NoSuchMethodException: com.test.controller.OrdersController.aa()
  • 这个{ message: \"An error occurred.\"} 不是正确的json。
  • @AleksandrM 我假设您指的是“消息”周围缺少双引号。谢谢,我已将该行更正为 return new String("{ \"message\": \"An error occurred.\"}"); 不幸的是,PUT 仍然返回 content-length=0。

标签: java rest struts2 http-headers struts2-rest-plugin


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-01-26
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多