1 Overview
In the past, we deal with every method in the front sent me a request, needs its own configuration data request, then by spring provided @ResponseBody forced into JSON data do, in fact, there has been a lot of duplicate code, what I also can construct a
Tools to achieve only concerned with data that needs to be changed. This gives the tools below.
public class JsonMsg { private int code; private String msg; private Map<String, Object> extendInfo = new HashMap<>(); public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Map<String, Object> getExtendInfo() { return extendInfo; } public void setExtendInfo(Map<String, Object> extendInfo) { this.extendInfo = extendInfo; } public static JsonMsg success(){ JsonMsg res = new JsonMsg(); res.setCode ( 100 ); res.setMsg ( "Operation Success!" ); return RES; } public static JsonMsg fail(){ JsonMsg res = new JsonMsg(); res.setCode ( 200 ); res.setMsg ( "Operation failed!" ); return RES; } public JsonMsg addInfo(String key, Object obj){ this.extendInfo.put(key, obj); return this; } }
Let me give a specific example of use:
It is a common CRUD Code
package com.hrms.controller; import com.hrms.bean.Department; import com.hrms.service.DepartmentService; import com.hrms.util.JsonMsg; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import java.util.List; / ** * @author GenshenWang.nomico * @date 2018/3/8. */ @Controller @RequestMapping(value = "/hrms/dept") public class DepartmentController { @Autowired DepartmentService departmentService; /** * Delete * @Param deptıd * @return */ @RequestMapping(value = "/delDept/{deptId}", method = RequestMethod.DELETE) @ResponseBody public JsonMsg deleteDept(@PathVariable("deptId") Integer deptId){ int res = 0; if (deptId > 0){ res = departmentService.deleteDeptById(deptId); } IF (! = RES. 1 ) { return . JsonMsg.fail () addInfo ( "del_dept_error", "delete abnormal" ); } return JsonMsg.success(); } /** * Change department * @Param deptıd * @param department * @return */ @RequestMapping(value = "/updateDept/{deptId}", method = RequestMethod.PUT) @ResponseBody public JsonMsg updateDeptById(@PathVariable("deptId") Integer deptId, Department department){ int res = 0; if (deptId > 0){ res = departmentService.updateDeptById(deptId, department); } IF (! RES = 1 ) { return . JsonMsg.fail () addInfo ( "update_dept_error", "department update failed" ); } return JsonMsg.success(); } /** * Added departments * @param department * @return */ @RequestMapping(value = "/addDept", method = RequestMethod.PUT) @ResponseBody public JsonMsg addDept(Department department){ int res = departmentService.addDept(department); if (res != 1){ return JsonMsg.fail().addInfo("add_dept_error", "添加异常!"); } return JsonMsg.success(); } /** * Query department total number of pages of information * @return */ @RequestMapping(value = "/getTotalPages", method = RequestMethod.GET) @ResponseBody Public JsonMsg given numerous version () { // Number of rows per page int limit =. 5 ; // total number of records int totalItems = departmentService.getDeptCount (); int TEMP = totalItems / limit; int totalpages = (% totalItems limit == 0) TEMP: TEMP? +1 ; return JsonMsg.success().addInfo("totalPages", totalPages); } /** * */ @RequestMapping(value = "/getDeptById/{deptId}", method = RequestMethod.GET) @ResponseBody public JsonMsg getDeptById(@PathVariable("deptId") Integer deptId){ Department department = null; if (deptId > 0){ department = departmentService.getDeptById(deptId); } if (department != null){ return JsonMsg.success().addInfo("department", department); } return . JsonMsg.fail () addInfo ( "get_dept_error", "No department information" ); } /** * Paging query: Returns the number of pages corresponding data * @param pageNo * @return */ @RequestMapping(value = "/getDeptList", method = RequestMethod.GET) public ModelAndView getDeptList(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo){ Music Videos ModelAndView = new new ModelAndView ( "departmentPage" ); // Number of rows per page int limit =. 5 ; // total number of records int totalItems = departmentService.getDeptCount (); int TEMP = totalItems / limit; int totalpages = ( ? totalItems% limit == 0) TEMP: TEMP +. 1 ; // page starting row (offset + 1) data as in the first page (offset = 0, from 1 (offset + 1) row data start) int offset = (pageNo is not -. 1) * limit; List<Department> departments = departmentService.getDeptList(offset, limit); mv.addObject("departments", departments) .addObject("totalItems", totalItems) .addObject("totalPages", totalPages) .addObject("curPageNo", pageNo); return mv; } /** * Query all sectors Name * @return */ @RequestMapping(value = "/getDeptName", method = RequestMethod.GET) @ResponseBody public JsonMsg getDeptName(){ List<Department> departmentList = departmentService.getDeptName(); if (departmentList != null){ return JsonMsg.success().addInfo("departmentList", departmentList); } return JsonMsg.fail(); } }