JacksonClient.java
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.waylau.netty.demo.codec.jackcon;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
/**
* 说明:Jackson json-客户端
*
* @author <a href="http://www.waylau.com">waylau.com</a> 2015年11月7日
*/
public class JacksonClient {
public static void main(String[] args) throws Exception{
new JacksonClient("localhost", 8082).run();
}
private final String host;
private final int port;
public JacksonClient(String host, int port){
this.host = host;
this.port = port;
}
public void run() throws Exception{
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.handler(new JacksonClientHandlerInitializer());
Channel channel = bootstrap.connect(host, port).sync().channel();
// 发送对象
JacksonBean user = new JacksonBean();
user.setAge(27);
user.setName("waylau");
List<String> sons = new ArrayList<String>();
for (int i = 0;i <10; i++) {
sons.add("Lucy"+i);
sons.add("Lily"+i);
}
user.setSons(sons);
Map<String, String> addrs = new HashMap<String, String>();
for (int i = 0;i <10; i++) {
addrs.put("001"+i, "18998366112");
addrs.put("002"+i, "15014965012");
}
user.setAddrs(addrs);
channel.write(user);
channel.flush();
// 等待连接关闭
channel.closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
}