使用HttpServlet写接口

背景:

  今天来了个大活儿:对外提供三个接口,接口的功能分别是按月来取数据、按天来取数据、按id来取数据。

下面是我定义的接口的详细文档:

月数据用于返回某些部门单月的原创稿件数据;

天数据用于返回某些部门单天的原创稿件数据;

文章详情则返回某些文章的详细数据,使用根据zbGuid去对应库获取信息。

请求方式:POST。

校验:

如果安全校验未通过,则会返回错误信息,示意如下:

{

 

   "Records": "error!",

 

   "message": "请传递合适的参数!"

 

}

原创稿月数据

接口地址:

正式环境根地址http://********/casData/depOriginal?type=month

测试环境根地址http://********/testCAS/casData/depOriginal?type=month

接口参数:

departmentIds=部门Id,多个以英文半角逗号分隔  例如136,135

pubMonth=发表月份YYYYMM格式,例如201703

原创稿天数据

接口地址:

正式环境根地址http://********/casData/depOriginal?type=day

测试环境根地址http://********/casData/depOriginal?type=day

接口参数:

departmentIds=部门Id,多个以英文半角逗号分隔  例如136,135

pubDay=发表日期YYYYMMDD格式,例如20170301

原创稿文章详情数据

接口地址:

正式环境根地址http://********/casData/depOriginal?type=article

测试环境根地址http://********/casData/depOriginal?type= article

接口参数:

zbGuids=文章的zbGuid多个以英文半角逗号隔开

--------------------------------------------------------------------------

  终于费了九牛二虎之力将接口文档写完了。但是,真正的工作才刚刚开始:下面讲的是如何在项目里添加功能:

 

public class CasDataApiServlet extends HttpServlet{


     @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String returnJson = ""; String pathInfo = request.getPathInfo(); if ("/depOriginal".equals(pathInfo)){ returnJson = queryDepOriginal(request, response); } ResponseUtil.json(response, returnJson); } }

 

 

	private String queryDepOriginal(HttpServletRequest request, HttpServletResponse response) {
		String returnJson = "";
		String type = RequestUtil.getParameter(request, "type", "");
		LOG.info("type = {}", type);
		if (type.equals("month")) {
			// 原创稿月数据
			String departmentIds = RequestUtil.getParameter(request, "departmentIds", "");
			String pubMonth = RequestUtil.getParameter(request, "pubMonth", "");
			if (StringHelper.isEmpty(departmentIds) || StringHelper.isEmpty(pubMonth)) {
				returnJson = "{\"Records\":\"error!\",\"message\":\"请传递合适的值!\"}";
			} else {
				returnJson = statDepartmentOrignalDayService.getApiJsonByDepIdAndMonth(departmentIds, pubMonth);
			}
		} else if (type.equals("day")) {
			// 原创稿天数据
			String departmentIds = RequestUtil.getParameter(request, "departmentIds", "");
			String pubDay = RequestUtil.getParameter(request, "pubDay", "");
			if (StringHelper.isEmpty(departmentIds) || StringHelper.isEmpty(pubDay)) {
				returnJson = "{\"Records\":\"error!\",\"message\":\"请传递合适的值!\"}";
			} else {
				returnJson = articleCEIResultPerformanceService.getOriginalArticleDayDataJson(departmentIds, pubDay);
			}
		} else if (type.equals("article")) {
			// 原创稿文章详情数据
			String zbGuids = RequestUtil.getParameter(request, "zbGuids", "");
			if (StringHelper.isEmpty(zbGuids)) {
				returnJson = "{\"Records\":\"error!\",\"message\":\"请传递合适的值!\"}";
			} else {
				returnJson = articleCEIResultPerformanceService.getOriginalArticleDetailDataJsonByZbguid(zbGuids);
			}
		} else {
			returnJson = "{\"Records\":\"error!\",\"message\":\"请传递合适的参数!\"}";
		}
		return returnJson;
	}

在web.xml中,写上这句话:

 <servlet>
  <servlet-name>casData</servlet-name>
  <servlet-class>com.trs.bas.controller.servlet.CasDataApiServlet</servlet-class>
  <load-on-startup>11</load-on-startup>
  <async-supported>true</async-supported>
 </servlet>
 <servlet-mapping>
  <servlet-name>casData</servlet-name>
  <url-pattern>/casData/*</url-pattern>
 </servlet-mapping>

 上述代码中运用了  ResponseUtil这个工具类,这是我公司自己封装的工具类。

它的作用就是:将给定的文本内容(字符串)返回给客户端.

	public static void json(HttpServletResponse response, String json)
			throws IOException {
		response(response, json, "utf-8", "text/html");
	}

        public static void response(HttpServletResponse response, String textPlain,
			String encoding, String contentType) throws IOException {
		//response.setBufferSize设置缓冲
	final int bufSize = NumberUtil.getMin(textPlain.length(), 1048576) * 3;
	try {
		response.setBufferSize(bufSize);
	} catch (Exception e) {
		LOG.error("setBufferSize:" + bufSize + " error", e);
	}
	if (contentType != null) {
	response.setContentType(contentType);
	} else {
		response.setContentType("text/plain");
	}
	if (encoding != null) {
		response.setCharacterEncoding(encoding);
	}

	// 不能设置setContentLength头, 否则在包含中文时浏览器可能收不全!
	esponse.getWriter().write(textPlain);
	response.flushBuffer();
	}

好了,这样架子就搭好了。接下来就是在函数中构造json串。此处省略一万字。。。

 

 

后记:等我做完了之后,发现月和日可以使用一个接口。 这样就可以少提供一个接口,减少前期的接口开发和后期的接口维护工作。还是应了那句老话: 多想少做。

  

 

转载于:https://www.cnblogs.com/donefive/p/6508564.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值