Struts2 Action接收POST请求JSON数据及其实现解析

一.认识JSON

JSON是一种轻量级、基于文本、与语言无关的数据交换格式,可以用文本格式的形式来存储或表示结构化的数据。

二.POST请求与Content-Type: application/json

常用的HTTP请求方法有GET, POST, PUT, DELETE等。在提交POST请求时,请求数据放在消息体(Body)中,请求数据的格式及编码方式用Content-Type来指定。如我们常用的表单<form>提交,其Content-Type默认为application/x-www-form-urlencoded,提交的数据按照key1=val1&key2=val2进行编码,服务器端也能很容易地解析K-V值。

JSON的出现,让交换的数据不再仅限于简单的K-V结构,而可以有更加复杂的层级,特别适合于RESTful接口。在发送请求时,指定Content-Type为application/json,即可使用JSON字符串作为请求的数据。而在服务器端接收到该请求后,将按照JSON字符串对请求数据进行处理。

三.Struts2接收JSON请求

在Struts2的Action中提取Content-Type为application/x-www-form-urlencoded的POST参数,我们非常熟悉:在Action中定义属性及其getter, setter方法,接收到请求时,默认会将与属性同名的参数值赋予该属性

但是对Content-Type为application/json的请求数据,Struts2默认无法解析。因为请求的JSON数据需从输入流中读取出来,无法直接从ServletRequest的请求参数中解析。很容易想到,要读取JSON请求数据,最直接的方式就是从输入流读取。而同时,Struts2的strus2-json-plugin也提供了有关的拦截器,对JSON请求数据进行解析。下面将对两种方案进行分析:

1.从输入流中读取JSON请求数据,以下是在Action中实现的一个读取输入流数据的方法

 1     //解析请求的Json数据
 2     private String getRequestPostData(HttpServletRequest request) throws IOException {
 3         int contentLength = request.getContentLength();
 4         if(contentLength<0){
 5             return null;
 6         }
 7         byte buffer[] = new byte[contentLength];
 8         for (int i = 0; i < contentLength;) {
 9             int len = request.getInputStream().read(buffer, i, contentLength - i);
10             if (len == -1) {
11                 break;
12             }
13             i += len;
14         }
15         return new String(buffer, "utf-8");
16     }

在Action的execute方法中调用该方法,即可获取到请求的JSON数据。

2.使用struts2-json-plugin配置

  • 添加struts2-json-plugin的依赖,以maven配置为例:
<dependencies>
   ...
   <dependency>
       <groupId>org.apache.struts</groupId>
       <artifactId>struts2-json-plugin</artifactId>
       <version>STRUTS_VERSION</version>
   </dependency>
   ...
</dependencies>
  • struts.xml配置文件添加JSON配置(粗体部分)
<package name="example"  extends="struts-default,json-default">
    ...
    <interceptor-ref name="json"/>
    ...
 </package>
  • 在Action中指定JSON数据中各个key及getter, setter,如请求的JSON数据如下,则在Action中定义名为type, message, code的属性及其getter, setter
{
    "type":10,
    "message": "this is a test msg",
    "code": 200
}

这样,在接收到以上JSON请求数据时,Struts会默认将type, message, code的值解析出来。

3.struts2-json-plugin解析JSON请求数据的分析

经过分析,struts2-json-plugin解析JSON请求数据,最核心的一个类是JSONIntercepter类。该拦截器的主要工作就是:读取JSON请求数据,将JSON数据提取出K-V值并设置到Action的属性中。步骤如下:

  • 判断当前请求数据类型是否为JSON类型
1 String contentType = request.getHeader("content-type");
2 ...
3 if ((contentType != null) && contentType.equalsIgnoreCase("application/json")) {
4     // load JSON object
5     Object obj = JSONUtil.deserialize(request.getReader());
6     ...
7 }
  • 如果数据类型为JSON,从输入流中读取JSON数据,以下为JSONUtil类的deserialize方法
 1     public static Object deserialize(Reader reader) throws JSONException {
 2         // read content
 3         BufferedReader bufferReader = new BufferedReader(reader);
 4         String line;
 5         StringBuilder buffer = new StringBuilder();
 6 
 7         try {
 8             while ((line = bufferReader.readLine()) != null) {
 9                 buffer.append(line);
10             }
11         } catch (IOException e) {
12             throw new JSONException(e);
13         }
14 
15         return deserialize(buffer.toString());
16     }
  • 解析得到JSON对象后,遍历JSON对象,取出K-V,通过反射的V设置给予K相同的属性

开发者可根据自己的需求进行选择:从输入流直接读取JSON请求数据,或使用struts2-json-plugin对JSON请求数据进行处理。

参考资料:

四种常见的 POST 提交数据方式(https://imququ.com/post/four-ways-to-post-data-in-http.html)

JSON Plugin(https://struts.apache.org/docs/json-plugin.html)

posted @ 2017-05-12 19:52  钟Sir_Zzz  阅读(33663)  评论(3编辑  收藏  举报