Triple协议Http标准能力增强-多Content-Type支持

本文主要介绍Triple对更多Http标准Content-Type的支持方式,以及服务该如何接收这些请求。

Triple协议Http标准能力增强-多Content-Type支持

本文主要介绍Triple对更多HTTP标准Content-Type的支持方式,以及服务该如何接收这些请求。

概述

Triple目前支持两种序列化方式:Json和protobuf,对应的ContentType:

  • application/json
  • application/grpc+proto

这在消费者和提供者都是后端服务时没有问题。但对于浏览器客户端,其可能发送更多类型的ContentType,需要服务端支持解码,如:

  • multipart/formdata
  • text/plain
  • application/x-www-form-urlencoded
  • application/xml

Rest已基本实现上述解码能力,使Triple实现这些能力是让Triple服务端与浏览器客户端完全互通的重要一步。

用法

multipart/formdata
  1. POST /org.apache.dubbo.samples.tri.noidl.api.PojoGreeter/greetPojo HTTP/1.1
  2. Host: 192.168.202.1:50052
  3. Content-Type: multipart/form-data; boundary=example-part-boundary
  4. Accept: application/json
  5. --example-part-boundary
  6. Content-Disposition: form-data; name="username"
  7. Content-Type: text/plain
  8. LuYue
  9. --example-part-boundary
  10. Content-Disposition: form-data; name="userdetail"
  11. Content-Type: application/json
  12. {
  13. "location":"beijing",
  14. "username":"LuYue"
  15. }
  16. --example-part-boundary
  17. Content-Disposition: form-data; name="userimg";filename="user.jpeg"
  18. Content-Type: image/jpeg
  19. <binary-image data>
  20. --example-part-boundary--

接收:

  1. @Override
  2. public ServerResponse greetPojo(String username, User user, byte[] attachment) {
  3. //LuYue
  4. System.out.println(username);
  5. //user.name=Luyue;user.location=beijing
  6. System.out.println(user);
  7. //<binary-image data>
  8. System.out.println(new String(attachment, StandardCharsets.UTF_8));
  9. return new ServerResponse("Server Received:"+username);
  10. }
  • 每一个 part 根据其 Content-Type 解码
  • 若方法参数是 byte[] 或 Byte[],对应字段不会解码
  • 响应使用 application/json 编码
application/x-www-form-urlencoded
  1. POST /org.apache.dubbo.samples.tri.noidl.api.PojoGreeter/greetUrlForm HTTP/1.1
  2. Host: 192.168.202.1:50052
  3. Content-Type: application/x-www-form-urlencoded
  4. Content-Length: 33
  5. Accept: application/json
  6. Hello=World&Apache=Dubbo&id=10086

两种接收方式:

  1. public ServerResponse greetUrlForm(String hello,String apache,long id){
  2. System.out.println("Hello:"+hello);
  3. System.out.println("Apache:"+apache);
  4. System.out.println("Id:"+id);
  5. return new ServerResponse("Server Received url form");
  6. }
  1. public ServerResponse greetUrlForm(Map<String,Object> params){
  2. System.out.println("Hello:"+params.get("Hello"));
  3. System.out.println("Apache"+params.get("Apache"));
  4. System.out.println("Id"+params.get("Id"));
  5. return new ServerResponse("Server Received url form");
  6. }
  • 若参数为Map,则解码为Map<String,String>传入
  • 若参数均为String或数值类型,按照参数列表逐个解码传入
  • 响应使用 application/json 编码
text/plain
  1. POST /org.apache.dubbo.samples.tri.noidl.api.PojoGreeter/greetString HTTP/1.1
  2. Host: 192.168.202.1:50052
  3. Content-Type: text/plain; charset=UTF-8
  4. Content-Length: 6
  5. Accept: application/json
  6. World!

接收:

  1. public ServerResponse greetUrlForm(String world){
  2. System.out.println("Hello:"+ world);
  3. return new ServerResponse("Server Received url form.");
  4. }
  • charset支持ASCII、UTF-8、UTF-16等,默认UTF-8
  • 响应使用 application/json 编码
application/xml
  1. POST /org.apache.dubbo.samples.tri.noidl.api.PojoGreeter/greetXml HTTP/1.1
  2. Host: 192.168.202.1:50052
  3. Content-Type: application/xml
  4. Content-Length: 86
  5. Accept: application/xml
  6. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  7. <User>
  8. <username>JohnDoe</username>
  9. <location>New York</location>
  10. </User>

接收:

  1. @Override
  2. public ServerResponse greetXml(User user) {
  3. System.out.println(user.getUsername());
  4. System.out.println(user.getLocation());
  5. return new ServerResponse("Server Received xml.");
  6. }
  • 该实现与Rest的XMLCodec相同
  • 响应使用 application/xml 编码

最后修改 November 22, 2023: Add a proposal for Triple support for Multiple Content-Type (#2868) (02a78fede7b)