package com.ivan.frame.dto.common;public final class RequestDTO extends BaseDTO {private static final long serialVersionUID = -2780042604928728379L;/*** 调用方的名称*/private Stringcaller;/*** 请求参数*/private Tparam;public String getCaller() {return caller;}public void setCaller(String caller) {this.caller = caller;}/*** 获取请求参数*/public T getParam() {return param;}/*** 设置请求参数* * @param param 请求参数*/public void setParam(T param) {this.param = param;}}
定义一个具体的业务对象 , 代码如下:
package com.ivan.frame.dto;import com.ivan.frame.dto.common.BaseDTO;public class PersonDTO extends BaseDTO {private static final long serialVersionUID = 4637634512292751986L;private int id;private int age;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
通过JSON.传入对象进行泛型转换 , 代码如下:
package com.ivan.json;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.TypeReference;import com.ivan.frame.dto.PersonDTO;import com.ivan.frame.dto.common.RequestDTO;public class GenericTest {public static void main(String[] args) {RequestDTO requestDTO = new RequestDTO();requestDTO.setCaller("callerId");PersonDTO personDTO = new PersonDTO();personDTO.setAge(11);personDTO.setName("张三");requestDTO.setParam(personDTO);String jsonString = JSON.toJSONString(requestDTO);System.out.println(jsonString);//这行是关键代码requestDTO = JSON.parseObject(jsonString, new TypeReference
各种概念
序列化相关的概念
public static String toJSONString(Object object, // SerializeConfig config, // SerializeFilter[] filters, // String dateFormat, //int defaultFeatures, // SerializerFeature... features) {SerializeWriter out = new SerializeWriter(null, defaultFeatures, features);try {JSONSerializer serializer = new JSONSerializer(out, config);if (dateFormat != null && dateFormat.length() != 0) {serializer.setDateFormat(dateFormat);serializer.config(SerializerFeature.WriteDateUseDateFormat, true);}if (filters != null) {for (SerializeFilter filter : filters) {serializer.addFilter(filter);}}serializer.write(object);return out.toString();} finally {out.close();}}
反序列化相关的概念
@SuppressWarnings("unchecked")public static T parseObject(String input, Type clazz, ParserConfig config, ParseProcess processor,int featureValues, Feature... features) {if (input == null) {return null;}if (features != null) {for (Feature feature : features) {featureValues |= feature.mask;}}DefaultJSONParser parser = new DefaultJSONParser(input, config, featureValues);if (processor != null) {if (processor instanceof ExtraTypeProvider) {parser.getExtraTypeProviders().add((ExtraTypeProvider) processor);}if (processor instanceof ExtraProcessor) {parser.getExtraProcessors().add((ExtraProcessor) processor);}if (processor instanceof FieldTypeResolver) {parser.setFieldTypeResolver((FieldTypeResolver) processor);}}T value = http://www.kingceram.com/post/(T) parser.parseObject(clazz, null);parser.handleResovleTask(value);parser.close();return (T) value;}
与 MVC整合
提供了用于将 mvc里的body数据(必须是json格式)转成里的请求参数或者将输出的对象转成json格式的数据 。mvc里的核心配置如下:
- 重庆社保卡补办指南
- bert使用指南
- 萌新的HTML5 入门指南
- 1 Android音视频开发,android驱动开发权威指南pdf
- 对话小牛电动CEO李彦:更看重长期价值 上市募资用于三方面
- 单调有界定理适用于函数吗_实数完备性定理|期末不考, 考研要考
- hexo 添加html项目,Hexo 主题制作指南_html/css_WEB
- 三唑醇常用于防治什么
- 3 爬虫入门指南:Python网络请求及常见反爬虫策略应对方法
- 或者可以生成 一个glyph包含一直字符图片image和许多用于渲染文本的参数