API需要同时维护多个版本。如何优雅的设计?

在项目中经常遇到,相同的数据,对不同的客户以及不同的终端,需要输出不同的数据。

更有特殊的情况,需要对一个数据,在不同的终端表示形式不一样。综合多种考虑,需要一种支持扩展,并且灵活的的接口, 开发效率还需高效。

在网上找了一圈没有发现,决定自己实现一个,决定使用模板,在freeMark和Velocity之间择了Velocity 。

  1. 性能

  2. 够用

原理,

利用Velocity的功能,生成各种复杂的数据,再向通过接口统一输出

直接上代码截图

接口模板

172100_2CZI_1538376.png

172100_7nJD_1538376.png

package com.echo.api.velocity.inter;

import org.apache.velocity.VelocityContext;

/**
 * 适配器接口. <br>
 * 类详细说明.
 * <p>
 * Copyright: Copyright (c) 2015年9月9日 下午4:02:23
 * <p>
 * <p>
 * 
 * @version 1.0.0
 */
public interface IDataAdapter {

	String TYPE_JSON = "json";

	String TYPE_XML = "xml";

	/**
	 * @param type
	 *            报文类型,json,xml
	 * @param version
	 *            版本,1.0.0
	 * @param key
	 *            标示key可以是任意指定
	 * @param template
	 *            报文模板名称
	 * @return
	 */
	public String getResult(String type, String version, String key, String template, VelocityContext context);
}

 

package com.echo.api.velocity.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.StringWriter;
import java.util.Properties;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import com.echo.api.velocity.inter.IDataAdapter;

/**
 * 常见的数据
 * <p>
 * Copyright: Copyright (c) 2015年9月9日 下午4:13:42
 * <p>
 * 
 * @version 1.0.0
 */
public class GeneralDataAdapter implements IDataAdapter {

	/**
	 * 模板基础路径
	 */
	private String baseTemplatePath;

	private VelocityEngine velocityEngine;

	private String encoding = "utf-8";

	public GeneralDataAdapter() throws Exception {
		init();
	}

	private void init() throws Exception {
		try {

			Properties properties = new Properties();
			// 设置velocityp 配置
			properties.load(new FileInputStream(new File("./config/velocity.properties")));
			Velocity.init(properties);
			velocityEngine = new VelocityEngine();
		}
		catch (Exception ex) {
			throw ex;
		}
	}

	public String getResult(String type, String version, String key, String template, VelocityContext context) {
		// 添加通用的处理函数
		context.put("DateFormatUtils", DateFormatUtils.class);
		// 返回报文
		StringWriter sw = new StringWriter();
		String path = baseTemplatePath + "/" + type + "/" + version + "/" + key + "/" + template;
		velocityEngine.mergeTemplate(path, encoding, context, sw);
		return sw.toString();
	}

	public String getBaseTemplatePath() {
		return baseTemplatePath;
	}

	public void setBaseTemplatePath(String baseTemplatePath) {
		this.baseTemplatePath = baseTemplatePath;
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public GeneralDataAdapter(String baseTemplatePath, String encoding) throws Exception {
		super();
		this.baseTemplatePath = baseTemplatePath;
		this.encoding = encoding;
		init();
	}

	public GeneralDataAdapter(String baseTemplatePath, VelocityEngine velocityEngine, String encoding) throws Exception {
		super();
		this.baseTemplatePath = baseTemplatePath;
		this.velocityEngine = velocityEngine;
		this.encoding = encoding;
		init();
	}

}
package com.echo.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.velocity.VelocityContext;

import com.echo.api.velocity.impl.GeneralDataAdapter;
import com.echo.api.velocity.inter.IDataAdapter;

/**
 * 使用velocity设计复杂多变的报文接口,在bean相同情况下实现不同的输出
 * <p>
 * Copyright: Copyright (c) 2015年9月9日 下午3:27:47
 * <p>
 * <p>
 * 
 * @author lihuan
 * @version 1.0.0
 */
public class TestAipVersion {

	/**
	 * main函数.
	 * 
	 * @param args
	 *            启动参数
	 * @throws Exception
	 *             Exception
	 */
	public static void main(String... args) throws Exception {

		VelocityContext context = new VelocityContext();
		// 同一数据源,输出不同报文以及不同的格式
		List<User> users = new ArrayList<User>();
		users.add(new User("张三", 23, "张家庄良民", new Date(), "shaoyishao"));
		// users.add(new User("李四", 25, "李家庄好人", new Date(), "shaoyishao"));
		// users.add(new User("王五", 39, "王庄刁民啊", new Date(), "shaoyishao"));

		context.put("userList", users);
		// json数据
		IDataAdapter generalDataAdapter = new GeneralDataAdapter("./templates", "utf-8");
		// json报文
		long stime = 0;
		stime = System.currentTimeMillis();
		System.out.println(generalDataAdapter.getResult(IDataAdapter.TYPE_JSON, "1.0.0", "android", "userlist.vm", context));
		System.out.println(generalDataAdapter.getResult(IDataAdapter.TYPE_JSON, "1.0.0", "android640", "userlist.vm", context));
		// xml报文
		stime = System.currentTimeMillis();
		System.out.println(generalDataAdapter.getResult(IDataAdapter.TYPE_XML, "1.0.0", "android", "userlist.vm", context));

	}
}

输出json,xml,以及不同屏幕

文案不同=====不同屏幕设备
小屏幕显示文字较少
[{
"count:":  1,
"name":"张三",
"age":23,
"remark":"张家庄良民",
"regTime":"20150909170631",
"btn1":"扫一扫"
}
]	
  大屏幕显示文字较多,
[{
"count:":  1,
"name":"张三",
"age":23,
"remark":"张家庄良民",
"regTime":"20150909170631",
"btn1":"扫一扫就知道"
}
]	
  
<?xml version="1.0" encoding="utf-8" ?>
<data><users><user>
<count>1</count>
<name>张三</name>
<age>23</age>
<remark>张家庄良民</remark> 
<regTime>2015年09月09日 17:06:31</regTime> 
</user>
</users>
</data>


转载于:https://my.oschina.net/u/1538376/blog/503933

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值