Struts2-Rest: manual HTTP 5xx returns no content

I am trying to manually return HTTP 4xx or 5xx with a custom error message. It works fine for GET, but returns empty content for PUT and POST. Thanks in advance to everyone who can suggest how PUT and POST work!

These are the calls I am making to the web service:
GET /webapp/orders/A.json

- returns HTTP 500 and an error message as content
PUT /webapp/orders/A.json

- returns HTTP 500 and content-length = 0

getModel()

gets called in both cases, the message just doesn't return it in Chrome for PUT.

Here's a sample code that raises the error:

OrdersController

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.\"}");
    }

} 

      

Spacers

<?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>

      

Web

<?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 Angular Sizing System

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

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

      

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

+3


source to share


1 answer


Adding <constant name="struts.rest.content.restrictToGET" value="false"/>

to struts.xml solved the problem. Kudos to @ontk for this will answer another question. Thanks to everyone who tried to help me.



+1


source







All Articles