在 Struts2 RESTful 插件 POST 请求中返回响应 [英] Returning response in Struts2 RESTful plugin POST request

查看:32
本文介绍了在 Struts2 RESTful 插件 POST 请求中返回响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<块引用>

你可以看到我得到的回应.我为post写的方法上面写着名字create.我确实在正文中有数据,并且我确实在 create 方法中完美地获取了数据.

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

<块引用>

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

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

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

我要回来

 代码:1xx,2xx,4xx消息:成功,失败

在接受 POST 请求时使用 XML 或 JSON.

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

解决方案

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

<块引用>

内容类型

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

  • HTML
  • XML
  • JSON

这里没有配置,只是添加内容类型扩展到您的 RESTful 网址.框架将负责其余的工作.因此对于例如,假设有一个名为 Movies 的控制器和一个带有supermanid,下面的网址都会打到

http://my.company.com/myapp/movies/supermanhttp://my.company.com/myapp/movies/superman.xmlhttp://my.company.com/myapp/movies/superman.xhtmlhttp://my.company.com/myapp/movies/superman.json

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

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

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


演示应用程序随名为 struts2 的 Struts 发行版提供-休息展示.添加上面的常量后,您可以使用一些 http 客户端测试应用程序.

POST http://localhost:8080/orders接受:应用程序/json内容类型:应用程序/json{id":23423423",clientName":罗马 C",数量":1000}-  回复  -201内容语言:en-US内容长度:54内容类型:application/json;charset=UTF-8日期:2017 年 10 月 7 日星期六 20:44:39 GMT电子标签:-735433458位置:http://localhost:8080/orders/23423423{amount":1000,clientName":Roman C",id":23423423"}

I have implemented Struts2 REST API from getting info from here

Struts2 Rest Plugin

Is there any way to return custom response in in restful plugin in Struts2. I did all the required changes like

struts.rest.content.restrictToGET = false

Got from This Question. Still I'm getting this error

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

If I don't add the above line I still get the same response.

This is the action i have provided in struts.xml:

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

This serves all the request GET,POST,PUT,UPDATE.

After Changing return type of post method from HttpHeader to String I'm still getting the same error

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>

This is the code i have written for POST:

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

this is the POST request method with return type 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>";
}

I'm getting perfect response for get either if i send request for xml or json, I do get xml and json based on extension. like http://localhost:8080/restdemoapplication/restdemo.xml, http://localhost:8080/restdemoapplication/restdemo.json

for POST request i do post request like

and you can see the response i get. The method i have written for post is written above with name create. I do have data in body and I do get the data in create method perfectly.

Now in post as i have seen in multiple example like

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

I don't want to return response for post request like these applications do. I want to return my own response, It will be a status code and a message like this

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

After some debugging I found that DefaultContentTypeHandlerManager in struts2-rest-plugin consider xhtml as default template. While it should be either XML or JSON.

I want to return

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

in either XML or JSON when a POST request is entertained.

(This is application entertains both non-restful request and restful requests. I can make xml or json as default template but I don't want as it will effect non-restful requests.)

解决方案

You misunderstood the concept of content types used with 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.

The plugin servers request by content type, either provided as action extension or by providing a data type. Incoming data type is defined by "Content-Type" header, and outgoing data type defined by "Accept" header.

To make POST request returning data you need to add a constant to Struts configuration file struts.xml:

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


Demo application is provided with the Struts distro named struts2-rest-showcase. After adding a constant above you can test the application with some http client.

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"}

这篇关于在 Struts2 RESTful 插件 POST 请求中返回响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆