The JSO N p lug in p ro vid es a "js on" re sult type that se ria lizes a ctio ns into JSO N. The se ria liza tion p ro cess is re curs ive, me aning that the w ho le ob je ct g r a ph, s ta rting on the a ctio n class (b ase class not include d) w ill b e s e ria lize d (r o ot ob je ct ca n be cus to mize d us ing the "ro ot" a ttribute). If the inte rcep to r is used, the a ction w ill b e pop ulate d fro m the JSO N co ntent in the re ques t, thes e a re the rules o f the inte rce pto r:
1?The "conte nt-type" must be "a pp lica tio n/jso n"
2?The JSO N co nte nt mus t b e we ll fo rme d, s ee js on.o rg fo r g ra mma r.
3?Actio n mus t ha ve a p ub lic "s ette r" me tho d fo r fie ld s tha t mus t b e p o pula ted.
4?Sup po rte d type s fo r p op ula tio n a re: P rimitive s (int,long...String), D
a te, Lis t, Map, P rimitive Arra ys, O the r cla ss (mo re o n this la te r), a nd Arra
y o f O the r clas s.
5?Any o b je ct in JSO N, tha t is to b e p op ula ted ins ide a lis t, o r a ma p, w ill b e o f type Map (ma p p ing fro m p rope rtie s to va lues), a ny who le numbe r w ill b e o f type Long, a ny de cima l numbe r w ill be o f type Doub le, and a ny
a rra y o f type Lis t.
JSON插件提供一种叫做“json”返回类型,来将action序列化成JSON 格式。序列化过程是递归的,这意味着整个对象图(从action开始,父类不会被序列化)都会被序列化(根对象可以通过“roo t”属性来自定义)。如果设置了拦截器,actio n将被请求中的JSON填充,下面是拦截器的规则:
1.请求的"co ntent-typ e"必须为"app licatio n/jso n"
2.JSON内容必须是格式良好的,语法参见https://www.wendangku.net/doc/cf8435968.html,
3.Action中被填充的属性必须要有一个setter方法
4.可以被填充的类型包括:原生类型(),Date,List, Map, 原生数组,其他class,和其他class的数组
5.被填充到一个list或者一个map中的任何JSON对象,将是Map类型的(将属性映射为值),任何数值将被认为是lo ng类型的,任何小数数值被认为是doub le类型,任何数组被认为是List类型。
Give n this JSO N s tring:
{
"dou bleVa lue": 10.10,
"nes tedBe an": {
"name": "Mr Bea n"
},
"lis t": ["A", 10, 20.20, {
"first Name": "E l Zor ro"
}],
"arr ay": [10, 20]
}
The a ctio n must ha ve a "se tDo ub le Va lue" me thod, ta king e ithe r a "floa t" o r a "flo at" a rgume nt (the inte rce p to r w ill co nve rt the va lue to the right one). The re mus t be a "s etNe sted Be an" whose a rg ument typ e ca n be a ny class, tha t has a "s etNa me" me thod ta king as a rg ument an "String". The re must be a "s e tLis t" me tho d tha t ta ke s a "Lis t" as a rg ume nt, tha t list w ill co nta in: "A" (Stri ng), 10 (Lo ng), 20.20 (Do ub le), Ma p ("firstNa me" -> "El Zo rro"). The "se tArra y" me tho d ca n ta ke as pa ra me te r e ithe r a "Lis t", o r any nume ric a rra y.
action必须要有一个"setDo ub leValue"方法,来接收一个"float" 或者参数"float" 参数(拦截器将值转换转换为合适的类型)。还必须要有一个"setNestedBean" 方法来设置对象,这个对象还必须要有一个"setName"方法参数类型为String。还必须要有一个"setList"方法来接收一个"List" 作为参数,这个列表将包
a t化/解析一个Da te字段m:ss"s
Exclud ing properties
A co mma-d e limite d lis t o f reg ula r e xp ress ions ca n b e p asse d to the JSO N Re sult and Inte rce pto r, p ro pe rties ma tching a ny o f thes e re gula r e xp ress io ns w ill b e igno red on the s e ria liza tion p ro cess:
log in.pa sswor d,
stu dentL ist.*\.si n
log in.pa sswor d,
stu dentL ist.*\.si n
Including properties
A co mma-d e limite d lis t o f reg ula r e xp ress ions ca n b e p asse d to the JSO N
Re sult to res trict which p ro pe rtie s w ill be se ria lized. O NLY p ro pe rtie s matchin
g a ny o f these reg ula r e xp ress io ns w ill b e includ ed in the se ria lize d o utp ut.
No te
Ex clude property ex pressions tak e pre ce de nce ov er include prope rty e xpre ssions.
Tha t is, if you use include a nd exclude property ex pressions on the sa me result, include prope rty ex pre ssions will not be applie d if a n e xclude ex clude prope rty ex pre ssion ma tche
first.
^en tries\[\d+\]\.clien tNumb er,
^en tries\[\d+\]\.sched uleNu mber,
^en tries\[\d+\]\.creat eUser Id
排除属性
一个以逗号分隔的正则表达式列表,可以传递给JSON结果和拦截器,任何符合正则表
达式的属性将在序列化过程中被忽略:
log in.pa sswor d,
stu dentL ist.*\.si n
log in.pa sswor d,
stu dentL ist.*\.si n
包含属性
一个以逗号分隔的正则表达式列表将传递给JSON结果来限制哪些属性可以被序列化。任何只有符合正则表达式的属性会被包含在序列化输出中。
注意
排除属性表达式优先于包含属性表达式。也就是说,如果你使用了包含和排除属性表达式在同一个r es ult上,且排除属性表达式匹配了一个属性,同时包含属性表达式也匹配这个属性,
那么只会应用排除表达式。
^en tries\[\d+\]\.clien tNumb er,
^en tries\[\d+\]\.sched uleNu mber,
^en tries\[\d+\]\.creat eUser Id
Root Object
Use the "roo t" a ttrib ute(O GNL e xp ress io n) to spe cify the roo t o b je ct to b e se ria lize d.
per son.j ob
The "ro ot" a ttribute(O GNL e xp res s io n) ca n a lso be used o n the inte rcep to r to spe cify the ob je ct tha t must be po pulated, make sure thi s objec t is not null.
根对象
使用"roo t"属性(OGNL表达式)来指定被序列化的根
per son.j ob
"roo t"属性(OGNL表达式)还可以为拦截器指定必须被的填充的对象,确保这个对象不为空。
Wrapping
Fo r se ve ra l re aso ns you mig ht wa nt to w ra p the JSO N o utp ut w ith so me t e xt, like w rap p ing w ith co mme nts, a dd ing a p re fix, o r to use file up lo ads w hic h re quire the res ult to be w ra ppe d in a te xta rea. Use wrapPrefix to a d d co nten t in the be g inning a nd wrapPos tfix to ad d conte nt a t the end. This s etting s ta k e p re cede nce o ve r "w rapW ithC o mme nts" and "p re fix" w hich a re dep re ca ted fro m 0.34 o n. Exa mp le s:
W ra p w ith co mme nts:
Ad d a p re fix:
W ra p fo r file up load: