consumer support application/xml MIME type
返回值序列化扩展
概念阐述
当前REST通道返回值支持application/json和text/plain两种格式,支持开发人员扩展和重写,服务提供者通过produces声明可提供序列化能力。
B030版本中通过增加org.apache.servicecomb.common.rest.codec.produce.ProduceXmlProcessor支持返回值为application/xml类型。
验证
下面将通过一个端到端(xmlclient->edge->xmlServer)的demo进行说明及验证
一、定义服务端实现
@RestSchema(schemaId="xml") @RequestMapping(path = "/XmlTest") public class SpringmvcXmlImpl implements Xml{ @RequestMapping(path = "/appXmls", method = RequestMethod.POST,produces = MediaType.APPLICATION_XML_VALUE) public String appXmls(@RequestBody String s) { return s; } }
二、定义EDGE
1.增加自定义路由机制
public class EdgeDispatcher extends AbstractEdgeDispatcher { private CompatiblePathVersionMapper versionMapper = new CompatiblePathVersionMapper(); @Override public int getOrder() { return 10000; } @Override public void init(Router router) { String regex = "/api/([^\\\\/]+)/(.*)"; router.routeWithRegex(regex).handler(CookieHandler.create()); router.routeWithRegex(regex).handler(createBodyHandler()); router.routeWithRegex(regex).failureHandler(this::onFailure).handler(this::onRequest); } protected void onRequest(RoutingContext context) { Map<String, String> pathParams = context.pathParams(); String microserviceName = pathParams.get("param0"); String prefix = "/api/" + microserviceName; String path = context.request().path().substring(prefix.length()); EdgeInvocation edgeInvocation = new EdgeInvocation(); edgeInvocation.init(microserviceName, context, path, httpServerFilters); edgeInvocation.edgeInvoke(); } }
2.以SPI的方式在resources下META-INF/services/文件夹新建文件,文件名为org.apache.servicecomb.transport.rest.vertx.VertxHttpDispatcher(SD提供,用于扩展路由策略),内容填写org.apache.servicecomb.demo.xml.edge.EdgeDispatcher(对应自定义路由的完整类名)
三、客户端调用
private static void test6() { String body = "<JAXBPerson>" + "<name>name</name><age>18</age><role>role</role><weight>111</weight>" + "<job><name>nnn</name><content>ccc</content></job>" + "</JAXBPerson>"; String s = invoker.postForObject("http://127.0.0.1:18080/api/xmlprovider/XmlTest/appXmls", body, String.class); System.out.println("springmvc s"+s); TestMgr.check(body, s); }
四、通过postman验证,header内容如下
body内容为
<JAXBPerson><name>name</name><age>18</age><role>role</role><weight>111</weight><job><name>nnn</name><content>ccc</content></job></JAXBPerson>
访问http://127.0.0.1:18080/api/xmlprovider/XmlTest/appXmls,结果如下
注意事项:
1.因为需要声明produces=MediaType.APPLICATION_XML_VALUE,所以不支持POJO方式开发
2.三个微服务的appId必须相同
3.http://127.0.0.1:18080/api/xmlprovider/XmlTest/appXmls,其中18080是edge发布的端口,xmlprovider是服务端微服务名
- 点赞
- 收藏
- 关注作者
评论(0)