Springmvc 组合jackson与fastjson使用心得.docx
《Springmvc 组合jackson与fastjson使用心得.docx》由会员分享,可在线阅读,更多相关《Springmvc 组合jackson与fastjson使用心得.docx(15页珍藏版)》请在三一办公上搜索。
1、Springmvc 组合jackson与fastjson使用心得Springmvc 组合jackson与fastjson使用心得 1、解决方案 可以将其中的main方法删掉。测试用的。我测试的结果是,jackson比fastjson快。 fastjson是1.1.36 jackson是2.2.3 jdk是1.7.40,client cpu是intel i3 2、主要代码 package org.springframework.web.servlet.view.json; import java.io.ByteArrayOutputStream; import java.io.IOExcepti
2、on; import java.io.OutputStream; import java.io.StringReader; import java.io.StringWriter; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import javax.servlet.http
3、.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.util.CollectionUtils; import org.springframework.validation.BindingResult; import org.springframework.web.servlet.view.AbstractView; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.parser.Fea
4、ture; import com.alibaba.fastjson.serializer.SerializerFeature; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackso
5、n.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.MappingIterator; import com.fasterxml.jackson.databind.ObjectMapper; import mon.collect.Lists; import mon.collect.Maps; import mon.collect.Sets; import com.test.User; public c
6、lass MappingFastJsonView extends AbstractView /* * Default content type: application/json. * Overridable through link #setContentType. */ public static final String DEFAULT_CONTENT_TYPE = application/json; private String encoding = UTF-8; private String jsonPrefix; private Boolean prettyPrint; priva
7、te Set modelKeys; private boolean extractValueFromSingleKeyModel = false; private boolean disableCaching = true; private boolean updateContentLength = false; /* * Construct a new code MappingFastJsonView, setting the content type to code application/json. */ public MappingFastJsonView setContentType
8、(DEFAULT_CONTENT_TYPE); setExposePathVariables(false); public String getEncoding return encoding; public void setEncoding(String encoding) this.encoding = encoding; /* * Specify a custom prefix to use for this views JSON output. * Default is none. * see #setPrefixJson */ public void setJsonPrefix(St
9、ring jsonPrefix) this.jsonPrefix = jsonPrefix; /* * Indicates whether the JSON output by this view should be prefixed with & . * Default is code false. * Prefixing the JSON string in this manner is used to help prevent JSON Hijacking. * The prefix renders the string syntactically invalid as a script
10、 so that it cannot be hijacked. * This prefix does not affect the evaluation of JSON, but if JSON validation is performed * on the string, the prefix would need to be ignored. * see #setJsonPrefix */ public void setPrefixJson(boolean prefixJson) / if (prefixJson) / if (jsonPrefix = null) / jsonPrefi
11、x = & ; / / this.jsonPrefix = (prefixJson ? & : null); /* * Whether to use the default pretty printer when writing JSON. * This is a shortcut for setting up an code JSON * The default value is code false. */ public void setPrettyPrint(boolean prettyPrint) this.prettyPrint = prettyPrint; /* * Set the
12、 attribute in the model that should be rendered by this view. * When set, all other model attributes will be ignored. */ public void setModelKey(String modelKey) this.modelKeys = Collections.singleton(modelKey); /* * Set the attributes in the model that should be rendered by this view. * When set, a
13、ll other model attributes will be ignored. */ public void setModelKeys(Set modelKeys) this.modelKeys = modelKeys; /* * Return the attributes in the model that should be rendered by this view. */ public final Set getModelKeys return this.modelKeys; /* * Set whether to serialize models containing a si
14、ngle attribute as a map or whether to * extract the single value from the model and serialize it directly. * The effect of setting this flag is similar to using code MappingJacksonHttpMessageConverter * with an code ResponseBody request-handling method. * Default is code false. */ public void setExt
15、ractValueFromSingleKeyModel(boolean extractValueFromSingleKeyModel) this.extractValueFromSingleKeyModel = extractValueFromSingleKeyModel; /* * Disables caching of the generated JSON. * Default is code true, which will prevent the client from caching the generated JSON. */ public void setDisableCachi
16、ng(boolean disableCaching) this.disableCaching = disableCaching; /* * Whether to update the Content-Length header of the response. When set to * code true, the response is buffered in order to determine the content * length and set the Content-Length header of the response. * The default setting is
17、code false. */ public void setUpdateContentLength(boolean updateContentLength) this.updateContentLength = updateContentLength; Override protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) setResponseContentType(request, response); response.setCharacterEncoding(en
18、coding); if (this.disableCaching) response.addHeader(Pragma, no-cache); response.addHeader(Cache-Control, no-cache, no-store, max-age=0); response.addDateHeader(Expires, 1L); Override protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws E
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Springmvc 组合jackson与fastjson使用心得 组合 jackson fastjson 使用 心得

链接地址:https://www.31ppt.com/p-3166093.html