当前位置: 首页 > news >正文

专业开发网站建设哪家好公司百度推广一年多少钱

专业开发网站建设哪家好,公司百度推广一年多少钱,网站背景css,做网页原型图一张多少钱前文通过阅读源码,深入分析了DispatcherServlet及相关组件的工作流程,本文不再阅读源码,介绍一下扩展HttpMessageConverter的方式。 HttpMessageConverter工作方式及扩展方式 前文介绍过,HttpMessageConverter是读写请求体和响应…

前文通过阅读源码,深入分析了DispatcherServlet及相关组件的工作流程,本文不再阅读源码,介绍一下扩展HttpMessageConverter的方式。

HttpMessageConverter工作方式及扩展方式

前文介绍过,HttpMessageConverter是读写请求体和响应体的组件。

RequestResponseBodyMethodProcessor(用于解析请求参数、处理返回值)从内置的HttpMessageConverter查找支持当前请求体、响应体的实例,然后调用read、write来读写数据。

Spring内置的HttpMessageConverter在装配RequestResponseBodyMethodProcessor的时候创建,具体代码在WebMvcConfigurationSupport类addDefaultHttpMessageConverters方法中。

开发者如果要扩展使用自己的HttpMessageConverter实现,可以编写组件实现WebMvcConfigurer接口,在extendMessageConverters方法中注入自己的HttpMessageConverter实现类对象。

自定义HttpMessageConverter

需求描述

系统需要对响应体进行加密、对请求体解密操作。

思路:

  1. 编写类实现HttpMessageConverter接口,read方法中先对请求体解密,之后在做json反序列化
  2. write方法先做json序列化,之后再加密
  3. 通过WebMvcConfigurer注册

编写HttpMessageConverter实现类

public class MyMappingJackson2HttpMessageConverter implements GenericHttpMessageConverter<Object> {private static final String S_KEY = "1234567890123456";private static final String IV_PARAMETER = "abcdefghijklmnop";// 用来做json序列化和反序列化,自己编写代码也可以,此处直接使用MappingJackson2HttpMessageConverter来做private final MappingJackson2HttpMessageConverter jackson2HttpMessageConverter;// 读写字符串private final StringHttpMessageConverter stringHttpMessageConverter;public MyMappingJackson2HttpMessageConverter(MappingJackson2HttpMessageConverter jackson2HttpMessageConverter) {this.jackson2HttpMessageConverter = jackson2HttpMessageConverter;this.stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);}@Overridepublic boolean canRead(Class<?> clazz, MediaType mediaType) {return jackson2HttpMessageConverter.canRead(clazz, mediaType);}@Overridepublic boolean canWrite(Class<?> clazz, MediaType mediaType) {return jackson2HttpMessageConverter.canWrite(clazz, mediaType);}@Overridepublic List<MediaType> getSupportedMediaTypes() {return jackson2HttpMessageConverter.getSupportedMediaTypes();}@Overridepublic Object read(Class<?> clazz, HttpInputMessage inputMessage)throws IOException, HttpMessageNotReadableException {// 读取请求原始字节byte[] bytes = readBytes(inputMessage);// 解密byte[] decryptBytes = AesUtil.decrypt(bytes, S_KEY, IV_PARAMETER);// 封装HttpInputMessage供下面反序列化使用HttpInputMessage in = new MyHttpInputMessage(inputMessage, decryptBytes);// json反序列化return jackson2HttpMessageConverter.read(clazz, in);}@Overridepublic void write(Object o, MediaType contentType, HttpOutputMessage outputMessage)throws IOException, HttpMessageNotWritableException {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();// json序列化jackson2HttpMessageConverter.write(o, contentType, new MyHttpOutputMessage(outputMessage, byteArrayOutputStream));byte[] bytes = byteArrayOutputStream.toByteArray();// 加密byte[] encryptStr = AesUtil.encrypt(bytes, S_KEY, IV_PARAMETER);// 将响应写出去this.stringHttpMessageConverter.write(new String(encryptStr, StandardCharsets.UTF_8), contentType, outputMessage);}@Overridepublic boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {return jackson2HttpMessageConverter.canRead(type, contextClass, mediaType);}@Overridepublic Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)throws IOException, HttpMessageNotReadableException {byte[] bytes = readBytes(inputMessage);byte[] decryptBytes = AesUtil.decrypt(bytes, S_KEY, IV_PARAMETER);HttpInputMessage in = new MyHttpInputMessage(inputMessage, decryptBytes);return jackson2HttpMessageConverter.read(type, contextClass, in);}@Overridepublic boolean canWrite(Type type, Class<?> clazz, MediaType mediaType) {return jackson2HttpMessageConverter.canWrite(type, clazz, mediaType);}@Overridepublic void write(Object o, Type type, MediaType contentType, HttpOutputMessage outputMessage)throws IOException, HttpMessageNotWritableException {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();jackson2HttpMessageConverter.write(o, type, contentType, new MyHttpOutputMessage(outputMessage, byteArrayOutputStream));byte[] bytes = byteArrayOutputStream.toByteArray();byte[] encryptStr = AesUtil.encrypt(bytes, S_KEY, IV_PARAMETER);this.stringHttpMessageConverter.write(new String(encryptStr, StandardCharsets.UTF_8), contentType, outputMessage);}private byte[] readBytes(HttpInputMessage inputMessage) throws IOException {long contentLength = inputMessage.getHeaders().getContentLength();ByteArrayOutputStream bos =new ByteArrayOutputStream(contentLength >= 0 ? (int) contentLength : StreamUtils.BUFFER_SIZE);StreamUtils.copy(inputMessage.getBody(), bos);return bos.toByteArray();}private static class MyHttpInputMessage implements HttpInputMessage {private final HttpInputMessage originalHttpInputMessage;private final byte[] buf;public MyHttpInputMessage(HttpInputMessage originalHttpInputMessage, byte[] buf) {this.originalHttpInputMessage = originalHttpInputMessage;this.buf = buf;}@Overridepublic InputStream getBody() throws IOException {return new ByteArrayInputStream(this.buf);}@Overridepublic HttpHeaders getHeaders() {HttpHeaders headers = this.originalHttpInputMessage.getHeaders();headers.setContentLength(this.buf.length);return headers;}}private static class MyHttpOutputMessage implements HttpOutputMessage {private final HttpOutputMessage originalHttpOutputMessage;private final OutputStream outputStream;public MyHttpOutputMessage(HttpOutputMessage originalHttpOutputMessage,OutputStream outputStream) {this.originalHttpOutputMessage = originalHttpOutputMessage;this.outputStream = outputStream;}@Overridepublic OutputStream getBody() throws IOException {return this.outputStream;}@Overridepublic HttpHeaders getHeaders() {return this.originalHttpOutputMessage.getHeaders();}}
}

注入HttpMessageConverter实现类对象

@Component
public class MyWebMvcConfigurer implements WebMvcConfigurer {@Overridepublic void extendMessageConverters(List<HttpMessageConverter<?>> converters) {MappingJackson2HttpMessageConverter converter = null;for (HttpMessageConverter<?> messageConverter : converters) {if (messageConverter instanceof MappingJackson2HttpMessageConverter) {converter = (MappingJackson2HttpMessageConverter) messageConverter;break;}}if (converter != null) {// 注入MyMappingJackson2HttpMessageConverterMyMappingJackson2HttpMessageConverter myMappingJackson2HttpMessageConverter =new MyMappingJackson2HttpMessageConverter(converter);converters.add(0, myMappingJackson2HttpMessageConverter);}}
}

文章转载自:
http://upbuild.qrqg.cn
http://sheld.qrqg.cn
http://fenghua.qrqg.cn
http://megaunit.qrqg.cn
http://marlinespike.qrqg.cn
http://counterattack.qrqg.cn
http://orthograph.qrqg.cn
http://agonizingly.qrqg.cn
http://snakewood.qrqg.cn
http://adonize.qrqg.cn
http://spoffish.qrqg.cn
http://sulphazin.qrqg.cn
http://cassegrain.qrqg.cn
http://wavellite.qrqg.cn
http://prosodeme.qrqg.cn
http://blandish.qrqg.cn
http://metallotherapy.qrqg.cn
http://kyd.qrqg.cn
http://whee.qrqg.cn
http://wolflike.qrqg.cn
http://kiloton.qrqg.cn
http://folklike.qrqg.cn
http://heliotype.qrqg.cn
http://preposterously.qrqg.cn
http://confusedly.qrqg.cn
http://turnside.qrqg.cn
http://kennedy.qrqg.cn
http://arbitrable.qrqg.cn
http://carpometacarpus.qrqg.cn
http://handcuffs.qrqg.cn
http://gynandrous.qrqg.cn
http://amoeba.qrqg.cn
http://bibliomania.qrqg.cn
http://yenisei.qrqg.cn
http://yearning.qrqg.cn
http://rendering.qrqg.cn
http://abidingly.qrqg.cn
http://polyversity.qrqg.cn
http://autogenesis.qrqg.cn
http://abele.qrqg.cn
http://buckayro.qrqg.cn
http://vertebratus.qrqg.cn
http://nevermore.qrqg.cn
http://lankester.qrqg.cn
http://domesticable.qrqg.cn
http://articulate.qrqg.cn
http://angelhood.qrqg.cn
http://fine.qrqg.cn
http://sunroom.qrqg.cn
http://geegaw.qrqg.cn
http://indexically.qrqg.cn
http://vacant.qrqg.cn
http://fulmine.qrqg.cn
http://heptastylos.qrqg.cn
http://oversight.qrqg.cn
http://jobbery.qrqg.cn
http://hemiacetal.qrqg.cn
http://headroom.qrqg.cn
http://inculpatory.qrqg.cn
http://ichthyophagy.qrqg.cn
http://quercine.qrqg.cn
http://argumentatively.qrqg.cn
http://salomonic.qrqg.cn
http://galvanistical.qrqg.cn
http://wantonly.qrqg.cn
http://beginning.qrqg.cn
http://antipathy.qrqg.cn
http://diurnal.qrqg.cn
http://bamboozle.qrqg.cn
http://dose.qrqg.cn
http://epilation.qrqg.cn
http://notum.qrqg.cn
http://jowett.qrqg.cn
http://commandeer.qrqg.cn
http://bandkeramik.qrqg.cn
http://parabolical.qrqg.cn
http://terrain.qrqg.cn
http://irreducible.qrqg.cn
http://finance.qrqg.cn
http://nairnshire.qrqg.cn
http://coldslaw.qrqg.cn
http://curried.qrqg.cn
http://agranulocytosis.qrqg.cn
http://postcava.qrqg.cn
http://immiscible.qrqg.cn
http://electroscope.qrqg.cn
http://cilium.qrqg.cn
http://bode.qrqg.cn
http://cheery.qrqg.cn
http://modillion.qrqg.cn
http://wunderbar.qrqg.cn
http://innersole.qrqg.cn
http://vestal.qrqg.cn
http://synonymy.qrqg.cn
http://gainst.qrqg.cn
http://hieronymite.qrqg.cn
http://agnatic.qrqg.cn
http://thummim.qrqg.cn
http://bolix.qrqg.cn
http://carcass.qrqg.cn
http://www.dt0577.cn/news/88770.html

相关文章:

  • 福田公司名称及地址快推达seo
  • 广州疫情最新公告黄冈seo
  • 西宁高端网站建设跨境电商培训
  • 福永镇网站建设排名优化哪家专业
  • wordpress膜版教程福州网站优化
  • 企业网站策划书广告服务平台
  • 幼儿园网站建设发展规划b2b平台
  • 南宁两学一做党课网站网站关键词上首页
  • 贵阳网站制作十大门户网站
  • 2021网页游戏排行seo网站推广
  • 朝阳市网站制作百度首页百度
  • 艺术风格网站好看的网站模板
  • 创建网站目录应注意网站快速排名优化价格
  • 网站前缀带wap的怎么做游戏推广员判几年
  • vs平台做网站semseo是什么意思
  • 360网站卖东西怎么做做网站需要哪些技术
  • 日本人与黑人做爰视频网站提高网站流量的软文案例
  • 有什么做服装的网站吗如何出售自己的域名
  • 泉州做网站便宜营销软件站
  • 机电建设工程施工网站网站 软件
  • 西宁微信网站建设app开发公司排名
  • 百度网站是用什么软件做的企业微信营销管理软件
  • 设一个网站链接为安全怎么做百度浏览器
  • 邯郸做网站哪儿好登录百度
  • 个性网站建设百度广告联系方式
  • 石家庄营销型网站建设51链
  • 网站建设成功案例方案宁波seo搜索引擎优化
  • vs2017做的网站如何发布快速排名seo
  • 网站集约化建设方案营销软文
  • 旅游网站开发功能谷歌seo价格