在 Struts2 RESTful 插件 POST 请求中返回响应

Returning response in Struts2 RESTful plugin POST request

我已经通过从这里获取信息实现了 Struts2 REST API

Struts2 Rest Plugin

在 Struts2 的 restful 插件中有什么方法可以 return 自定义响应吗? 我做了所有必需的更改,例如

struts.rest.content.restrictToGET = false

来自 This Question. 我仍然收到此错误

No result defined for action `com.web.Controller.RestDemoController` and result create, 

如果我不添加以上行,我仍然会得到相同的响应。

这是我在 struts.xml 中提供的操作:

<action name="restdemo" class="com.web.Controller.RestDemoController">
    <interceptor-ref name="customRestStack"></interceptor-ref>
</action>

这满足了所有请求 GET,POST,PUT,UPDATE.

将 return 类型的 post 方法从 HttpHeader 更改为 String 后,我仍然遇到相同的错误

Error 404: No result defined for action com.web.Controller.RestDemoController and result <?xml version="1.0" encoding="UTF-8"?> <Status><code>200</code><message>Success</message></Status>

这是我为 POST:

编写的代码
public HttpHeaders create(){
    System.out.println(envision2Data.toString());
    return new DefaultHttpHeaders("create").withStatus(200);
}

这是 return 类型的 POST 请求方法 String:

public String create(){
    System.out.println(envision2Data.toString());
    return "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <Status><code>200</code><message>Success</message></Status>";
}

如果我发送 xml 或 json 的请求,我会得到完美的 get 响应,我会根据扩展名得到 xml 和 json。 像 http://localhost:8080/restdemoapplication/restdemo.xmlhttp://localhost:8080/restdemoapplication/restdemo.json

对于POST请求我做post请求喜欢

你可以看到我得到的回复。我为 post 编写的方法在上面用名称 create 编写。我在 body 中确实有数据,而且我在 create 方法中完美地获取了数据。

现在 post 正如我在多个示例中看到的那样

struts-rest, stuts2-rest-sample, struts2-rest-shopping-list

我不想像这些应用程序那样 return 响应 post 请求。我想 return 我自己的回应,这将是一个状态代码和这样的消息

<?xml version="1.0" encoding="UTF-8"?> <Status><code>200</code><message>Success</message></Status>

经过一些调试,我发现 struts2-rest-plugin 中的 DefaultContentTypeHandlerManagerxhtml 视为默认模板。虽然它应该是 XML 或 JSON.

我要return

 code : 1xx,2xx,4xx
 message: Success, Fail

在 XML 或 JSON 中,当 POST 请求被受理时。

(这是应用程序接受非 restful 请求和 restful 请求。我可以将 xml 或 json 作为默认模板,但我不想因为它会影响非 restful 请求。)

您误解了与 struts2-rest-plugin 一起使用的内容类型的概念。

Content Types

In addition to providing mapping of RESTful URL's to Controller ( Action ) invocations, the REST plugin also provides the ability to produce multiple representations of the resource data. By default, the plugin can return the resource in the following content types:

  • HTML
  • XML
  • JSON

There is nothing configure here, just add the content type extension to your RESTful URL. The framework will take care of the rest. So, for instance, assuming a Controller called Movies and a movie with the id of superman, the following URL's will all hit the

http://my.company.com/myapp/movies/superman
http://my.company.com/myapp/movies/superman.xml
http://my.company.com/myapp/movies/superman.xhtml
http://my.company.com/myapp/movies/superman.json

Note, these content types are supported as incoming data types as well. And, if you need, you can extend the functionality by writing your own implementations of org.apache.struts2.rest.handler.ContentTypeHandler and registering them with the system.

插件服务器按内容类型请求,作为操作扩展提供或通过提供数据类型提供。传入数据类型由 "Content-Type" header 定义,传出数据类型由 "Accept" header.

定义

要使 POST 请求返回数据,您需要将常量添加到 Struts 配置文件 struts.xml:

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

演示应用程序由名为 struts2-rest-showcase 的 Struts 发行版提供。在上面添加常量后,您可以使用一些 http 客户端测试应用程序。

POST http://localhost:8080/orders
Accept: application/json
Content-Type: application/json
{
  "id":"23423423",
  "clientName":"Roman C",
  "amount": 1000
}
 -- response --
201 
Content-Language:  en-US
Content-Length:  54
Content-Type:  application/json;charset=UTF-8
Date:  Sat, 07 Oct 2017 20:44:39 GMT
ETag:  -735433458
Location:  http://localhost:8080/orders/23423423

{"amount":1000,"clientName":"Roman C","id":"23423423"}

这就是我需要的解决方案。我所做的是

@Override
public Object getModel() {
        <!-- Changed name of the object overhere to mainObject-->
    if (mainObject == null) {
        if (responseObject != null) {
            return responseObject;
        } else if (mainObject != null) {
            return mainObject;
        }
        mainObject = new mainObject();
    }
    return mainObject;
}

然后在内容类型处理程序中。 Content-Type 处理程序是自定义的,因为我使用 Jackson 将 JSON 转换为对象,反之亦然。

if (paramObject instanceof MainObject) {
        MainObject mainObject = (MainObject) paramObject;
        writer.write(mapper.writeValueAsString(mainObject));
} else if (paramObject instanceof ResponseObject) {
        ResponseObject responeObject = (ResponseObject) paramObject;
        writer.write(mapper.writeValueAsString(responeObject));
}