开发者社区> 问答> 正文

关于公司让写客户端首页接口,不明白什么意思?

只知道普通的接口的定义,服务接口还真不知道怎么下手。下面是原先一个小模块的服务接口

package cn.damai.mt.app.advert.rest.impl;

import java.util.HashMap;
 import java.util.Map;

import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.springframework.stereotype.Service;

import cn.damai.mt.app.advert.rest.AdvertRestService;
 import cn.damai.mt.app.advert.service.MtcAppAdvertService;
 import cn.damai.mt.app.model.MtcAppAdvert;
 import cn.damai.mt.app.resp.ModelResp;
 import cn.damai.mt.app.util.ParamUtil;
 @Service("terminalAppAdvertRestService")
 public class AdvertRestServiceImpl implements AdvertRestService {
 private Log log = LogFactory.getLog(getClass());
 @Resource(name = "mtcAppAdvertService")
 MtcAppAdvertService mtcAppAdvertService;
 @Override
 public ModelResp> getHomeAdvert(HttpServletRequest request) {
 ModelResp> resp=new ModelResp>();
 Map map=ParamUtil.getParaMap(request);
 Map resultMap=new HashMap();
 int type=0;
 if(map.get("type")!=null){
 try {
 type=(Integer)map.get("type");
 } catch (Exception e) {
 log.info("参数不符合条件!");
 resp.setCode(400);
 return resp;
 }
 }
 if(type==1||type==2||type==3){
 try {
 MtcAppAdvert mtcAppAdvert = mtcAppAdvertService.getHomeAdvert();
 MtcAppAdvert mtcAppAd = mtcAppAdvertService.getHomeAd();
 if(mtcAppAdvert!=null){
 String imgUrl = mtcAppAdvert.getImgUrl();
 String[] array = imgUrl.split(",");
 String md5 = mtcAppAdvert.getMd5();
 String[] arr = md5.split(",");
 if(type==1){
 imgUrl = array[0];
 md5 = arr[0];
 }else if(type==2){
 imgUrl = array[1];
 md5 = arr[1];
 }else if(type==3){
 imgUrl = array[2];
 md5 = arr[2];
 }
 resultMap.put("advertTitle", mtcAppAdvert.getAdvertTitle());
 resultMap.put("jumpContent", mtcAppAdvert.getJumpContent());
 resultMap.put("imgUrl", imgUrl);
 resultMap.put("md5", md5);
 resp.setCode(200);
 resp.setData(resultMap);
 return resp;
 }else{//符合条件的为空,
 if(mtcAppAd!=null){//有数据,但不符合条件
 log.info("有数据,但不符合条件!");
 resp.setCode(203);
 //203:无数据返回
 return resp;
 }
 log.info("没有广告!");
 resp.setCode(201);
 //201:无数据返回
 return resp;
 }
 } catch (Exception e) {
 log.error(e);
 e.printStackTrace();
 resp.setCode(500);
 return resp;
 }
 }else{
 log.info("获取首页广告图:安卓和ios的分辨类型不正确!");
 resp.setCode(400);
 return resp;
 }
}


}

展开
收起
爵霸 2016-03-20 08:04:02 3699 0
1 条回答
写回答
取消 提交回答
  • 你这样理解:
    手机请求服务器某URL,并传入参数,服务器处理后返回一堆数据,这就是接口

    1、你上面的代码就代表URL是http://你公司的域名/项目名/terminalAppAdvertRestService
    2、手机传的参数是一个JSON字符串,通过ParamUtil转成了MAP对象
    3、服务器经过一系列处理得到resultMap,并通过这个方法将其返回给WebService
    4、WebService服务将resultMap转换成JSON字符串,返回给了手机

    2019-07-17 19:08:28
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
网易 NEI 接口管理解决 方案 立即下载
QQ移送页面框架优化实践 立即下载
QQ移动页面框架优化实践 立即下载