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

2024-01-01

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

Struts2 Rest 插件 http://struts.apache.org/docs/rest-plugin.html

有没有办法在 Struts2 的 Restful 插件中返回自定义响应。 我做了所有必需的更改,例如

struts.rest.content.restrictToGET = false

来自这个问题。 https://stackoverflow.com/questions/7685555/post-request-to-struts2-with-rest-plugin-not-receiving-response/9403583#9403583我仍然收到这个错误

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.

将 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);
}

这是具有返回类型的 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 请求,我会得到完美的响应,我会根据扩展名获取 xml 和 json。 喜欢http://localhost:8080/restdemoapplication/restdemo.xml, http://localhost:8080/restdemoapplication/restdemo.json

for POST request i do post request like enter image description here

你可以看到我得到的回复。我为帖子编写的方法写在上面,名称为create。我的正文中确实有数据,并且我确实在创建方法中完美地获取了数据。

现在在帖子中,正如我在多个示例中看到的那样

支柱休息 https://github.com/Jacob110/struts2-rest, Stuts2-rest-样本 https://github.com/amolghotankar/struts2-rest-sample, struts2-rest-购物清单 https://github.com/darek/struts2-rest-shopping-list

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

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

经过一些调试我发现DefaultContentTypeHandlerManager在 struts2-rest-plugin 中考虑xhtml作为默认模板。虽然它应该是 XML 或 JSON。

我想回来

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

当处理 POST 请求时,以 XML 或 JSON 形式显示。

(这个应用程序同时接受非静态请求和静态请求。我可以将 xml 或 json 作为默认模板,但我不希望,因为它会影响非静态请求。)


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

内容类型 http://struts.apache.org/docs/rest-plugin.html#content-types

除了提供 RESTful URL 到控制器的映射( Action )调用,REST 插件还提供了以下功能: 产生资源数据的多种表示。默认情况下, 插件可以返回以下内容类型的资源:

  • HTML
  • XML
  • JSON

这里没有任何配置,只是添加内容类型扩展 到您的 RESTful URL。框架将处理剩下的事情。因此对于 例如,假设一个名为 Movies 的控制器和一部带有id of superman,以下网址都会命中

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

请注意,这些内容类型支持作为传入数据类型,如下所示 出色地。而且,如果需要,您可以通过编写来扩展功能 你自己的实现org.apache.struts2.rest.handler.ContentTypeHandler并注册 他们与系统。

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

To make POST请求返回数据需要在Struts配置文件中添加常量struts.xml:

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

演示应用程序随 Struts 发行版提供,名为struts2-rest-showcase https://github.com/apache/struts/tree/master/apps/rest-showcase。添加上面的常量后,您可以使用一些 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"}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Struts2 RESTful 插件 POST 请求中返回响应 的相关文章

随机推荐