首页>代码>SpringBoot+Mybatis+thymeleaf搭建开源个人博客系统>/blog/src/main/java/springboot/client/codec/HttpJsonRequestEncoder.java
package springboot.client.codec; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.http.*; import springboot.server.codec.AbstractHttpJsonEncoder; import springboot.server.codec.HttpJsonRequest; import java.net.InetAddress; import java.util.List; /** * @author tangj * @date 2018/4/15 21:46 */ public class HttpJsonRequestEncoder extends AbstractHttpJsonEncoder<HttpJsonRequest> { @Override protected void encode(ChannelHandlerContext ctx, HttpJsonRequest msg, List<Object> out) throws Exception { //(1)调用父类的encode0,将业务需要发送的对象转换为Json ByteBuf body = jsonEncode(ctx, msg.getBody()); //(2) 如果业务自定义了HTTP消息头,则使用业务的消息头,否则在这里构造HTTP消息头 // 这里使用硬编码的方式来写消息头,实际中可以写入配置文件 FullHttpRequest request = msg.getRequest(); if (request == null) { request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/do", body); HttpHeaders headers = request.headers(); headers.set(HttpHeaderNames.HOST, InetAddress.getLocalHost() .getHostAddress()); headers.set(HttpHeaderNames.CONNECTION, HttpHeaders.Values.CLOSE); headers.set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP.toString() + ',' + HttpHeaderValues.DEFLATE.toString()); headers.set(HttpHeaderNames.ACCEPT_CHARSET, "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); headers.set(HttpHeaderNames.ACCEPT_LANGUAGE, "zh"); headers.set(HttpHeaderNames.USER_AGENT, "Netty json Http Client side"); headers.set(HttpHeaderNames.ACCEPT, "text/html,application/json;q=0.9,*/*;q=0.8"); } HttpUtil.setContentLength(request, body.readableBytes()); // (3) 编码后的对象 out.add(request); } }

陈小灏 LV18
2月18日
ghjgjhgjh LV1
2024年6月17日
微信网友_7015933945958400 LV1
2024年5月30日
zhouenyi LV6
2024年3月20日
酒酒清欢 LV8
2023年12月5日
微信网友_6724618780348416 LV3
2023年12月4日
yqyqyqyqy LV4
2023年7月23日
3416059514 LV1
2023年6月23日
try8023 LV19
2023年5月4日
ycyycy LV1
2022年10月24日

求学的熊猫 LV11
10月10日
ES大兵 LV11
8月15日
denliv_hui LV14
7月1日
f22m1a2b2 LV17
2月5日
nightstarshii
1月26日
暂无贡献等级
Duangi LV1
2024年12月18日
微信网友_6797029372219392 LV2
2024年10月29日
Peny_ZH LV5
2024年9月21日
xiaoxindu8 LV10
2024年9月13日
yellow_flowers LV8
2024年7月8日