webflux databuffer输出

直接输出JSON对象 

@GetMapping("selectMy")
public Mono<Void> selectMy(ServerHttpRequest request, ServerHttpResponse response) {String pageNumber= request.getQueryParams().getFirst("pageNumber");AuthUser authUser= LoginProcess.getCurrentUser(request.getCookies());Map<String,String> result=new HashMap<String,String>(){{put("aa","1");put("bb","21dd");}};return response.writeWith(Flux.create(sink -> {NettyDataBufferFactory nettyDataBufferFactory = new NettyDataBufferFactory(new UnpooledByteBufAllocator(false));try {DataBuffer dataBuffer= nettyDataBufferFactory.wrap(JSON.toJSONString(result).getBytes("utf8"));//DataBuffer dataBuffer= nettyDataBufferFactory.wrap("asdf".getBytes("utf8"));sink.next(dataBuffer);} catch (UnsupportedEncodingException e) {e.printStackTrace();}sink.complete();}));
}

通用方法辅助类封装:


import com.alibaba.fastjson.JSON;
import io.netty.buffer.UnpooledByteBufAllocator;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;import java.io.UnsupportedEncodingException;/*** Created by wanghongqi on 2019/5/11* ServerHttpResponse辅助类*/
public class ServerHttpResponseUtil {/*** JSON输出形式输出对象* @param response* @param object* @return*/public static Mono<Void> writeObjectAsJson(ServerHttpResponse response,Object object){return response.writeWith(Flux.create(sink -> {NettyDataBufferFactory nettyDataBufferFactory = new NettyDataBufferFactory(new UnpooledByteBufAllocator(false));try {DataBuffer dataBuffer= nettyDataBufferFactory.wrap(JSON.toJSONStringWithDateFormat(object,JSON.DEFFAULT_DATE_FORMAT).getBytes("utf8"));//DataBuffer dataBuffer= nettyDataBufferFactory.wrap("asdf".getBytes("utf8"));sink.next(dataBuffer);} catch (UnsupportedEncodingException e) {e.printStackTrace();}sink.complete();}));}/*** html输出* @param response* @param html* @return*/public static Mono<Void> writeHtml(ServerHttpResponse response,String html){return response.writeWith(Flux.create(sink -> {NettyDataBufferFactory nettyDataBufferFactory = new NettyDataBufferFactory(new UnpooledByteBufAllocator(false));try {DataBuffer dataBuffer= nettyDataBufferFactory.wrap(html.getBytes("utf8"));sink.next(dataBuffer);} catch (UnsupportedEncodingException e) {e.printStackTrace();}sink.complete();}));}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/416931.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Debug Hook

命名 关于这个命名是我自己这样说的&#xff0c;至于这种HOOK技术&#xff0c;先前在一个开源项目中叫做RemoteHook&#xff0c;我比较喜欢自己的这种命名&#xff0c;所以就叫Debug Hook。如果有错误&#xff0c;请指出。 先来说说调试的原理 在Windows操作系统&#xff0c;有…

多种时间格式字符串转换为Date对象

maven引入包 org.apache.commons commons-lang3 3.8 使用类 org.apache.commons.lang3.time.DateUtils 调用示例 Date date DateUtils.parseDate(strDate, "yyyy-MM-dd","yyyy-MM-dd HH:mm:ss","yyyy/MM/dd","yyyy/MM/dd HH:mm:ss&q…

SPFA 算法详解

适用范围&#xff1a;给定的图存在负权边&#xff0c;这时类似Dijkstra等算法便没有了用武之地&#xff0c;而Bellman-Ford算法的复杂度又过高&#xff0c;SPFA算法便派上用场了。 我们约定有向加权图G不存在负权回路&#xff0c;即最短路径一定存在。当然&#xff0c;我们可以…

webflux上传下载文件

上传文件 PostMapping("addAttach") public Mono<JsonResult> addAttach(RequestPart("file") FilePart filePart,//获取文件参数RequestPart("dataId") String dataId,//获取其他参数 ){String strFileName filePart.filename();//获取…

js 数字千分位展示

str.replace(/\d{1,3}(?(\d{3})$)/g,function(s){ return s, })

TCP基础知识 复习

前言 说来惭愧&#xff0c;大二时候学的计算机网络好多都不太记得了&#xff0c;不过还好有认真学过&#xff0c;捡起来也挺快的&#xff0c;就是对于现在业界中使用的网络算法的不是很懂&#xff1b; 1 TCP报文段结构 1.1 序号和确认号 序号&#xff0c;是报文段首字节的字节流…

Ant Design Tabs切换控制

注意点&#xff1a; 1.需要绑定activeKey&#xff0c;而不是defaultActiveKey属性&#xff0c;否则加载完成后无法通过设置state值切换。 2.绑定activeKey后选项卡切换点击无效&#xff0c;需通过点击事件修改state值才行。 切换控制 this.setState({ defaultActiveKey:&qu…

linux安装mongodb(设置非root用户和开机启动)

官网地址&#xff1a;https://www.mongodb.com/ 在官网上选择不同的linux系统得到不同的下载地址&#xff0c;我们用的下载地址是&#xff1a;https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.3.tgz 1.mongodb的安装及连接 cd opt/ wget https://fastdl.mon…

Ant Design Tabs标签页隐藏的标签内元素无法获取到

Tabs.TabPane# 参数 说明 类型 默认值 forceRender 被隐藏时是否渲染 DOM 结构 boolean false 修改该值为true即可&#xff0c;该TabPane隐藏也可以渲染dom结构&#xff0c;示例&#xff1a; <TabPane tab"进度分析" key"2" forceRender"…

Mvc NuGet 数据迁移

网上有很多的ef code first 的使用的方式&#xff0c;很乱&#xff0c;下面是我自己整理出来的&#xff0c;有什么不正确的地方还请指正&#xff0c;本人菜鸟一枚&#xff01; 1、新建一个类库 》引用 右击 管理NuGet程序包 添加EntityFramework 类库中就会有添加一个App.conf…

Ant Design Form.Item的label中文字换行的替代方式

const FormItemForm.Item; 替换代码 <Row><div className{styles.label}>{checkItem.desc}</div><div className{styles.wrapper}><FormItem classNamedesc>{getFieldDecorator(check_is_do_${checkItem.id}, {rules: [{required: true,message…

新的编程语言研发契机思考

目前受制于中美贸易战影响&#xff0c;国家大力发展芯片、软件产业&#xff0c;各种类型芯片陆续流出&#xff0c;AI领域、IoT领域、PC和服务器领域芯片在不断出新品。指令集方面&#xff0c;ARM和Intel x86受制于人&#xff0c;RISC-V貌似谈论的很火但也不知是否是未来的趋势&…

numpy加权平均

1 import numpy as np 2 a np.arange(15).reshape(3,5) 3 a array([[ 0, 1, 2, 3, 4],    [ 5, 6, 7, 8, 9],    [10, 11, 12, 13, 14]]) np.average(a, axis0,weights(10, 5, 1)) array([ 2.1875, 3.1875, 4.1875, 5.1875, 6.1875]) axis0 对最外层维度元素计算…