我是靠谱客的博主 飘逸白昼,这篇文章主要介绍使用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多个以英文半角逗号隔开

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

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

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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); } }

 

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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中,写上这句话:

复制代码
1
2
 <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这个工具类,这是我公司自己封装的工具类。

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

最后

以上就是飘逸白昼最近收集整理的关于使用HttpServlet写接口的全部内容,更多相关使用HttpServlet写接口内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(82)

评论列表共有 0 条评论

立即
投稿
返回
顶部