首页 技术 正文
技术 2022年11月16日
0 收藏 895 点赞 4,496 浏览 2816 个字

一.认识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中实现的一个读取输入流数据的方法

     //解析请求的Json数据
private String getRequestPostData(HttpServletRequest request) throws IOException {
int contentLength = request.getContentLength();
if(contentLength<0){
return null;
}
byte buffer[] = new byte[contentLength];
for (int i = 0; i < contentLength;) {
int len = request.getInputStream().read(buffer, i, contentLength - i);
if (len == -1) {
break;
}
i += len;
}
return new String(buffer, "utf-8");
}

在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类型
 String contentType = request.getHeader("content-type");
...
if ((contentType != null) && contentType.equalsIgnoreCase("application/json")) {
// load JSON object
Object obj = JSONUtil.deserialize(request.getReader());
...
}
  • 如果数据类型为JSON,从输入流中读取JSON数据,以下为JSONUtil类的deserialize方法
     public static Object deserialize(Reader reader) throws JSONException {
// read content
BufferedReader bufferReader = new BufferedReader(reader);
String line;
StringBuilder buffer = new StringBuilder(); try {
while ((line = bufferReader.readLine()) != null) {
buffer.append(line);
}
} catch (IOException e) {
throw new JSONException(e);
} return deserialize(buffer.toString());
}
  • 解析得到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)

上一篇: Java 变量类型
下一篇: 使用c#解析json库
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,945
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,471
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,284
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,100
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,732
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,767