接口开发经验之谈

xiaoxiao2021-02-28  79

前言:现在开发中常使用前后端分离,后台开发使用springMVC框架进行后台接口的开发。经过最近的开发对接口开发有了一些新的体会,在这里做一下总结。 希望能和大家交流一下,并能吸取更多接口开发方面的经验。

接口开发格式的封装

返回接口的格式要有固定的格式,这样前端人员能够进行规律的解析。

例子 { "code": 0, "msg": "成功", "data": {} }

这样的格式是长使用的。key值可以根据习惯更改

那么根据这种数据类型,我们可以使用springMVC的ModelMap类型。 这里可以封装一个工具类,来进行固定格式的返回。

public class ModelMapHelper extends ModelMap { public void setCode(int code) { this.put("code",code); } public void setMsg(String msg) { this.put("msg",msg); } public void setData(Object data) { this.put("data",data); } public void setCodeAndMsg(int code,String msg){ this.put("code",code); this.put("msg",msg); } public void setErrorMap(String errMsg){ this.put("code",HssContants.CODE_SYSTEM_ERROR); this.put("msg",errMsg); } public void setInternalErrorMap(){ this.put("code",HssContants.CODE_SYSTEM_ERROR); this.put("msg",HssContants.MSG_INTERNAL_ERROR); } public void setSuccessMap(String successInfo){ this.put("code",HssContants.CODE_SUCCESS); this.put("msg",successInfo); } public void setSuccessMap(){ this.put("code",HssContants.CODE_SUCCESS); this.put("msg",HssContants.MSG_SUCCESS); } public static void main(String[] args) { ModelMapHelper modelMapHelper = new ModelMapHelper(); modelMapHelper.setCode(1); modelMapHelper.setMsg("hello"); modelMapHelper.setData("你好"); System.out.println("modelMapHelper = " + modelMapHelper); } }

接口开发中各层分别的作用

Controller层的作用


我认为,controller层是接受数据请求(request)和返回数据结果(response)的。 因此 ,我常在controller层进行必要数据的判断,如果有错误直接返回视图(ModelMap)。


例子 @RequestMapping(value = "/save",method = RequestMethod.POST) public ModelMap saveMsg(@ModelAttribute Message msg){ ModelMapHelper modelMapHelper = new ModelMapHelper(); //做非空判断 if(StringUtils.isEmpty(msg.getCustomerId())){ modelMapHelper.setErrorMap(HssContants.MSG_NOT_CUSTOMERID); return modelMapHelper; } try{ //将结果返回给视图 messageService.saveMessage(msg); modelMapHelper.setSuccessMap(HssContants.MSG_SUCCESS_ADD); modelMapHelper.setData(msg); }catch (GeneralException g){ modelMapHelper.setErrorMap(g.getMessage()); g.printStackTrace(); logger.warn("保存消息信息失败",g); }catch (Exception e){ modelMapHelper.setInternalErrorMap(); e.printStackTrace(); logger.error("保存消息信息异常",e); } return modelMapHelper; }

Service层的作用


service层用来处理一些复杂的逻辑操作等。 具有承上启下的作用。

它常做的事情是:

根据业务逻辑,调用dao层,并处理数据。返回直接的操作结果给controller层。

这样让看似复杂的操作让controller层调用起来看起来很简洁。


例子(分页查询) public List<Message> getAll(Message message){ Integer page = message.getPage(); Integer rows = message.getRows(); if(page !=null && rows!=null){ PageHelper.startPage(page,rows, "id"); } Example example = new Example(Message.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("customerId",message.getCustomerId()); List<Message> list = messageMapper.selectByExample(example); return list; }

Dao层的作用


dao层是直接对数据库的操作,默认为大家都会。


接口开发对异常的处理


我总结了一下几点:

无论什么异常都要返回给视图一个结果(ModelMap)。(成功或者失败)可以将异常分为自定义异常和系统内部异常。 自定义异常,表示因为某些字段没有获取,无法进行操作的异常,这些异常是程序可以控制的异常。也就是开发人员知道的异常。系统内部异常,主要是数据库操作时抛出的不可控的异常。这些异常归结为系统异常。定义好异常的代号,有利于排查错误信息。
转载请注明原文地址: https://www.6miu.com/read-54904.html

最新回复(0)