注意:此文档描述的内容正在建设中或处于功能早期阶段,请持续关注文档更新!
本文主要介绍Triple对更多HTTP标准Content-Type的支持方式,以及服务该如何接收这些请求。
Triple目前支持两种序列化方式:Json和protobuf,对应的ContentType:
这在消费者和提供者都是后端服务时没有问题。但对于浏览器客户端,其可能发送更多类型的ContentType,需要服务端支持解码,如:
Rest已基本实现上述解码能力,使Triple实现这些能力是让Triple服务端与浏览器客户端完全互通的重要一步。
POST /org.apache.dubbo.samples.tri.noidl.api.PojoGreeter/greetPojo HTTP/1.1
Host: 192.168.202.1:50052
Content-Type: multipart/form-data; boundary=example-part-boundary
Accept: application/json
--example-part-boundary
Content-Disposition: form-data; name="username"
Content-Type: text/plain
LuYue
--example-part-boundary
Content-Disposition: form-data; name="userdetail"
Content-Type: application/json
{
"location":"beijing",
"username":"LuYue"
}
--example-part-boundary
Content-Disposition: form-data; name="userimg";filename="user.jpeg"
Content-Type: image/jpeg
<binary-image data>
--example-part-boundary--
接收:
@Override
public ServerResponse greetPojo(String username, User user, byte[] attachment) {
//LuYue
System.out.println(username);
//user.name=Luyue;user.location=beijing
System.out.println(user);
//<binary-image data>
System.out.println(new String(attachment, StandardCharsets.UTF_8));
return new ServerResponse("Server Received:"+username);
}
POST /org.apache.dubbo.samples.tri.noidl.api.PojoGreeter/greetUrlForm HTTP/1.1
Host: 192.168.202.1:50052
Content-Type: application/x-www-form-urlencoded
Content-Length: 33
Accept: application/json
Hello=World&Apache=Dubbo&id=10086
两种接收方式:
public ServerResponse greetUrlForm(String hello,String apache,long id){
System.out.println("Hello:"+hello);
System.out.println("Apache:"+apache);
System.out.println("Id:"+id);
return new ServerResponse("Server Received url form");
}
public ServerResponse greetUrlForm(Map<String,Object> params){
System.out.println("Hello:"+params.get("Hello"));
System.out.println("Apache"+params.get("Apache"));
System.out.println("Id"+params.get("Id"));
return new ServerResponse("Server Received url form");
}
POST /org.apache.dubbo.samples.tri.noidl.api.PojoGreeter/greetString HTTP/1.1
Host: 192.168.202.1:50052
Content-Type: text/plain; charset=UTF-8
Content-Length: 6
Accept: application/json
World!
接收:
public ServerResponse greetUrlForm(String world){
System.out.println("Hello:"+ world);
return new ServerResponse("Server Received url form.");
}
POST /org.apache.dubbo.samples.tri.noidl.api.PojoGreeter/greetXml HTTP/1.1
Host: 192.168.202.1:50052
Content-Type: application/xml
Content-Length: 86
Accept: application/xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<User>
<username>JohnDoe</username>
<location>New York</location>
</User>
接收:
@Override
public ServerResponse greetXml(User user) {
System.out.println(user.getUsername());
System.out.println(user.getLocation());
return new ServerResponse("Server Received xml.");
}