当前位置: 首页 > news >正文

开发一个app软件多少钱seo引擎优化是什么意思

开发一个app软件多少钱,seo引擎优化是什么意思,营销型网站建设价格贵吗,华为品牌策划方案前言 最近有个想法想整理一个内容比较完整springboot项目初始化Demo。 SpringBoot接口统一返回和全局异常处理#xff0c;使用ControllerAdvice ExceptionHandler 的组合来实现。 一、pom文件新增依赖 dependencygroupIdcom.alibaba/groupIdar… 前言 最近有个想法想整理一个内容比较完整springboot项目初始化Demo。 SpringBoot接口统一返回和全局异常处理使用ControllerAdvice ExceptionHandler 的组合来实现。 一、pom文件新增依赖 dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.3/version/dependencydependencygroupIdorg.apache.commons/groupIdartifactIdcommons-lang3/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency二、创建Json工具类、统一返回相关类 2.1JsonUtil  package com.murg.bootdemo.util;import com.alibaba.fastjson.JSONArray; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.SneakyThrows; import org.springframework.stereotype.Component;import javax.servlet.ServletOutputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; import java.util.List; import java.util.Map;/*** jackson的序列化和反序列化工具类逐步替换gson只留一个序列化框架*/ Component public class JsonUtil {public static ObjectMapper objectMapper;public JsonUtil(ObjectMapper objectMapper) {JsonUtil.objectMapper objectMapper;}public static ObjectMapper getObjectMapper() {return objectMapper;}/*** 根据json字符串解析对象(不带解密)*/SneakyThrowspublic static T T fromJson(String json, ClassT c) {ObjectMapper gson getObjectMapper();return gson.readValue(json, c);}/*** 根据json字符串解析对象(不带解密,第二个参数是Type适用于泛型类型的返回类型...)*/SneakyThrowspublic static T T fromJson(String json, TypeReferenceT type) {ObjectMapper gson getObjectMapper();return gson.readValue(json, type);}public static MapString, Object fromJsonToMap(String json) {return fromJson(json, new TypeReferenceMapString, Object() {});}public static MapString, String fromJsonToStrMap(String json) {return fromJson(json, new TypeReferenceMapString, String() {});}/*** 根据对象解析成json字符串**/SneakyThrowspublic static String toJson(Object obj) {return getObjectMapper().writeValueAsString(obj);}public static MapString, Object fromJsonToMap(InputStream inputStream) throws IOException {return fromJson(inputStream, new TypeReferenceMapString, Object() {});}private static T T fromJson(InputStream inputStream, TypeReferenceT tTypeReference) throws IOException {return objectMapper.readValue(inputStream, tTypeReference);}/*** map中取list* param map* param clazz* param key* return*/public static T ListT mapToList(MapString, Object map, Class clazz, String key) {Object omap.get(key);String jsonJSONArray.toJSONString(o);return JSONArray.parseArray(json,clazz);}public static void write2Stream(ServletOutputStream outputStream, Object webResult) throws IOException {objectMapper.writeValue(outputStream, webResult);}/**** description: 实体类转Map(可追加字段)* author: Jeff* date: 2019年10月29日* param object* return*/public static MapString, Object entityToMap(Object object,MapString,Object paramMap) {for (Field field : object.getClass().getDeclaredFields()) {try {boolean flag field.isAccessible();field.setAccessible(true);Object o field.get(object);paramMap.put(field.getName(), o);field.setAccessible(flag);} catch (Exception e) {e.printStackTrace();}}return paramMap;}}2.2ErrorCode  package com.murg.bootdemo.exception;import lombok.Getter; import lombok.RequiredArgsConstructor;/***/ Getter RequiredArgsConstructor public enum ErrorCode {ERROR_CODE_400(400, 参数异常),;private final int errorCode;private final String errorMsg; }2.3WebResult package com.murg.bootdemo.common;import com.murg.bootdemo.exception.ErrorCode; import com.murg.bootdemo.util.JsonUtil; import lombok.Data; import org.slf4j.MDC; import java.util.*;Data public class WebResultT {private T data;private int code SysType.WEB_RESULT_OK;private int msgType SysType.WEB_RESULT_MSG_ALERT;private String msg ;private final String trace Objects.toString(MDC.get(traceId), );public WebResultT setMsgType(int msgType) {this.msgType msgType;return this;}public WebResult() {}public WebResult(int code, String msg) {this.code code;this.msg msg;}public T getData() {return data;}public WebResultT setData(T data) {this.data data;return this;}public int getCode() {return code;}public WebResultT setCode(int code) {this.code code;return this;}public String getMsg() {return msg;}public WebResultT setMsg(String msg) {this.msg msg;return this;}public WebResultT isOK() {this.code SysType.WEB_RESULT_OK;return this;}public WebResultT isWrong() {this.code SysType.WEB_RESULT_WRONG;this.msgType SysType.WEB_RESULT_MSG_ERROR;return this;}public WebResultT isWrong(String msg) {this.code SysType.WEB_RESULT_WRONG;this.msgType SysType.WEB_RESULT_MSG_ERROR;this.msg msg;return this;}public WebResultT setErrorCode(int errorCode) {this.code errorCode;return this;}public static T WebResultT ok() {return new WebResult();}public static T WebResultT ok(T data) {return WebResult.Tok().setData(data);}public static T WebResultT wrong() {return new WebResult(SysType.WEB_RESULT_WRONG, 操作失败);}public static T WebResultT init(int code, String msg) {return new WebResult(code, msg);}public static T WebResultT wrong(String msg) {return new WebResult(SysType.WEB_RESULT_WRONG, msg);}public WebResultT alert(String msg) {this.setMsgType(SysType.WEB_RESULT_MSG_ALERT);this.setMsg(msg);return this;}public WebResultT alert(String msg, String yes) {this.setMsgType(SysType.WEB_RESULT_MSG_ALERT);this.setMsg(msg);return this;}public static T WebResultT confirm(String msg) {return new WebResultT().setMsgType(SysType.WEB_RESULT_MSG_CONFIRM).setMsg(msg);}public static T WebResultT setErrorCode(ErrorCode errorCode) {return WebResult.Twrong().setErrorCode(errorCode.getErrorCode()).setMsg(errorCode.getErrorMsg());}public WebResultT question(String msg, String yes) {this.setMsgType(SysType.WEB_RESULT_MSG_QUESTION);this.setMsg(msg);return this;}public WebResultT put(String key, Object val) {if (this.data null) {this.data (T) new HashMapString, Object();}((MapString, Object)this.data).put(key, val);return this;}public WebResultT putAll(Object object) {if (this.data null) {this.data (T) new HashMapString, Object();}if (object instanceof Map) {((MapString, Object)this.data).putAll((Map)object);} else {MapString, Object paramMap JsonUtil.fromJsonToMap(JsonUtil.toJson(object));((MapString, Object)this.data).putAll(paramMap);}return this;}public WebResultT setMainMessage(String msg) {return setMsg(msg);}public WebResultT setMessageType(int msgType) {return setMsgType(msgType);}public WebResultT relaod(String mes) {return this.alert(mes,$reloadPage);}}三、创建全局异常处理配置 3.1BusinessException 创建自定义异常类BusinessException继承RuntimeException package com.murg.bootdemo.exception;/*** 业务级异常***/ SuppressWarnings(serial) public class BusinessException extends RuntimeException {private String errMsg;private int errCode -1;public BusinessException(Throwable cause) {}public BusinessException() {}public BusinessException(final String message, final Throwable cause) {super(message, cause);this.errMsg message;}public BusinessException(ErrorCode errorCode) {this.errCode errorCode.getErrorCode();this.errMsg errorCode.getErrorMsg();}public BusinessException(String errMsg) {this.errMsg errMsg;}public BusinessException(int errCode) {this.errCode errCode;}public BusinessException(String errMsg, int errCode) {this.errMsg errMsg;this.errCode errCode;}Overridepublic Throwable fillInStackTrace() {return this;}public String getErrMsg() {return errMsg;}public BusinessException setErrMsg(String errMsg) {this.errMsg errMsg;return this;}public int getErrCode() {return errCode;}public BusinessException setErrCode(int errCode) {this.errCode errCode;return this;}Overridepublic String getMessage() {return errMsg;}}3.2自定义ErrorCode package com.murg.bootdemo.exception;import lombok.Getter; import lombok.RequiredArgsConstructor;/***/ Getter RequiredArgsConstructor public enum ErrorCode {ERROR_CODE_400(400, 参数异常),;private final int errorCode;private final String errorMsg; } 3.3自定义ExceptionUtil,获取堆栈信息 package com.murg.bootdemo.util;import java.io.PrintWriter; import java.io.StringWriter;/*** Description: 系统异常处理*/ public class ExceptionUtil {/*** 获取完整的堆栈信息* param throwable* return*/public static String getStackTrace(Throwable throwable) {StringWriter sw new StringWriter();PrintWriter pw new PrintWriter(sw, true);throwable.printStackTrace(pw);return sw.getBuffer().toString();}/*** 获取异常堆栈信息 - 指定长度* param throwable* param size* return*/public static String getStackTrace(Throwable throwable,int size) {String stac getStackTrace(throwable);if (stac.length() size) {stac stac.substring(0, size);}return stac;} }3.4数据库创建表MySysException用于以后将错误信息记录 package com.murg.bootdemo.exception.po;import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.KeySequence; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import io.swagger.annotations.ApiModel; import lombok.Getter; import lombok.Setter;import java.io.Serializable; import java.math.BigDecimal; import java.util.Date;/***/ Getter Setter TableName(MY_SYS_EXCEPTION) KeySequence(MY_SYS_EXCEPTION_SEQ) public class MySysException implements Serializable {private static final long serialVersionUID 1L;TableId(value EXCEPTIONID, type IdType.INPUT)private BigDecimal exceptionid;private String userid;private String functionid;private String requesturl;private String requestparams;private Date exceptiondate;private Long exceptiontype;private Long exceptioncode;private String exceptionmsg;private String exceptionstac;}3.5最后通过ControllerAdvice创建一个全局异常的配置类ExceptionConfig  package com.murg.bootdemo.config;import com.murg.bootdemo.common.WebResult; import com.murg.bootdemo.exception.BusinessException; import com.murg.bootdemo.exception.ErrorCode; import com.murg.bootdemo.exception.po.MySysException; import com.murg.bootdemo.util.ExceptionUtil; import com.murg.bootdemo.util.JsonUtil; import io.jsonwebtoken.ExpiredJwtException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.http.HttpStatus; import org.springframework.validation.BindingResult; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.servlet.NoHandlerFoundException; import javax.servlet.http.HttpServletRequest; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors;/*** 公共全局异常处理*/ ControllerAdvice ResponseBody Slf4j RequiredArgsConstructor public class ExceptionConfig {private final String defaultMessage 系统出错请与系统管理员联系!;private final Long EXCEPTION_CONTROLLER 1L;private final Long EXCEPTION_SERVICE 2L;ExceptionHandler(BusinessException.class)public WebResultObject businessExceptionHandler(BusinessException businessException) {return new WebResult().alert(StringUtils.defaultString(businessException.getMessage(), defaultMessage)).setErrorCode(businessException.getErrCode());}ExceptionHandler(MethodArgumentNotValidException.class)public WebResultObject handler(MethodArgumentNotValidException e) {log.error(e.getMessage(), e);BindingResult bindingResult e.getBindingResult();ListMapString, String collect bindingResult.getAllErrors().stream().map(objectError - {ConstraintViolation? unwrap objectError.unwrap(ConstraintViolation.class);MapString, String map new HashMap(3);map.put(property, unwrap.getPropertyPath().toString());map.put(message, objectError.getDefaultMessage());return map;}).collect(Collectors.toList());return WebResult.setErrorCode(ErrorCode.ERROR_CODE_400).setData(collect).alert(参数错误);}//处理请求参数格式错误 RequestParam上validate失败后抛出的异常是javax.validation.ConstraintViolationExceptionExceptionHandler(ConstraintViolationException.class)public WebResultObject handler(ConstraintViolationException e) {log.error(e.getMessage(), e);ListMapString, String collect e.getConstraintViolations().stream().map(constraintViolation - {MapString, String map new HashMap(3);map.put(property, constraintViolation.getPropertyPath().toString());map.put(message, constraintViolation.getMessage());return map;}).collect(Collectors.toList());return WebResult.setErrorCode(ErrorCode.ERROR_CODE_400).setData(collect).alert(参数错误);}ExceptionHandler(Exception.class)public WebResultObject exceptionHandler(Exception exception, HttpServletRequest request) {log.error(系统错误, exception);// 简单记录下错误日志复制的his表结构以及逻辑后续应该会做调整errorLogRecord(request, exception);return WebResult.wrong().alert(StringUtils.defaultString(exception.getMessage(), defaultMessage));}ExceptionHandler(ExpiredJwtException.class)public WebResultObject exceptionHandler(ExpiredJwtException exception, HttpServletRequest request) {log.error(token解析错误, exception);WebResultObject webResult WebResult.ok();webResult.isWrong(您可能未登录或登录超时请重新登陆);// 简单记录下错误日志复制的his表结构以及逻辑后续应该会做调整errorLogRecord(request, exception);return webResult;}ExceptionHandler(NoHandlerFoundException.class)ResponseStatus(value HttpStatus.NOT_FOUND)public WebResultObject handleNotFoundError(NoHandlerFoundException ex) {return WebResult.wrong(接口不存在);}private void errorLogRecord(HttpServletRequest request, Exception e) {try {MySysException sysException new MySysException();//异常类型sysException.setExceptiontype(EXCEPTION_CONTROLLER);//请求地址String basePath request.getScheme() :// request.getServerName() : request.getServerPort() request.getContextPath();sysException.setRequesturl(basePath);//请求参数MapString, String[] params new HashMap(request.getParameterMap());String param JsonUtil.toJson(params);if (param.length() 1800) {param param.substring(0, 1800);}sysException.setRequestparams(param);//时间sysException.setExceptiondate(new Date());//异常信息String msg e.getMessage();if (StringUtils.length(msg) 200) {msg msg.substring(0, 200);}sysException.setExceptionmsg(msg);//堆栈信息sysException.setExceptionstac(ExceptionUtil.getStackTrace(e, 1000));//后续正式的话保存信息//MySysExceptionMapper.insert(sysException);} catch (Exception exception) {//不处理log.error(拦截异常保存失败: exception.getMessage());}}}package com.murg.bootdemo.util;import java.io.PrintWriter; import java.io.StringWriter;/*** Description: 系统异常处理*/ public class ExceptionUtil {/*** 获取完整的堆栈信息* param throwable* return*/public static String getStackTrace(Throwable throwable) {StringWriter sw new StringWriter();PrintWriter pw new PrintWriter(sw, true);throwable.printStackTrace(pw);return sw.getBuffer().toString();}/*** 获取异常堆栈信息 - 指定长度* param throwable* param size* return*/public static String getStackTrace(Throwable throwable,int size) {String stac getStackTrace(throwable);if (stac.length() size) {stac stac.substring(0, size);}return stac;} }四、改造getTt26接口返回统一返回值 package com.murg.bootdemo.business.controller; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.murg.bootdemo.business.entity.Tt26; import com.murg.bootdemo.business.service.Tt26Service; import com.murg.bootdemo.common.WebResult; import lombok.RequiredArgsConstructor; import org.springframework.http.*; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.Map;RequiredArgsConstructor RestController public class TestRestController {private final RestTemplate restTemplate;private final Tt26Service tt26Service;RequestMapping(value /getTt26, method RequestMethod.GET)public WebResult getTt26(RequestParam String code){//通过不同的id获取不同的namereturn WebResult.ok(tt26Service.getOne(Wrappers.Tt26lambdaQuery().eq(Tt26::getCode,code)));}}出参结果 五、创建测试Controller测试全局异常  package com.murg.bootdemo.business.controller;import com.murg.bootdemo.exception.BusinessException; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;RestController public class TestExceptionController {//Get接口主要是用来获取信息的接口RequestMapping(value /testbusiexception, method RequestMethod.GET)public String testbussexception(){throw new BusinessException(测试全局异常拦截);}//Get接口主要是用来获取信息的接口RequestMapping(value /testexception, method RequestMethod.GET)public String testexception() throws Exception {throw new Exception(测试全局异常拦截2);}}接口访问自主抛出异常的接口看返回结果
http://www.w-s-a.com/news/489353/

相关文章:

  • 职业生涯规划大赛心得贵阳哪家网站做优化排名最好
  • wordpress 图片懒加载北京网站优化和推广
  • 深圳网站建设工作一个dede管理两个网站
  • 被禁止访问网站怎么办中国建筑网官网查询系统
  • 网站管理运营建设网贷网站
  • 深圳市龙岗区住房和建设局网站怎么给网站做404界面
  • 设计类网站网站系统 建设和软件岗位职责
  • 网站后台打开慢站长之家网址ip查询
  • 图书馆网站设计方案家具设计作品
  • 马鞍山做网站公司排名徐州网站外包
  • 十堰微网站建设电话宣传型网站建设
  • 电脑制作网站教程网络公司除了建网站
  • 360制作网站搜网站网
  • 门户网站标题居中加大网站底部的制作
  • 网站建设项目费用报价ai软件下载
  • 面料 做网站重庆网站seo费用
  • 中国沈阳网站在哪里下载中国移动营销策略分析
  • 建设银行 钓鱼网站360免费建站教程
  • wordpress全站cdn网站运营年度推广方案
  • 成都网站开发培训机构网站开发 实习报告
  • 廊坊网站建设佛山厂商wordpress神主题
  • 成县建设局网站中国建筑有几个工程局
  • 网站打不开被拦截怎么办单页面网站制作
  • 关于协会网站建设的建议设计公司名字参考
  • 怎样申请做p2p融资网站页面设计时最好使用一种颜色
  • 一般做网站上传的图片大小网站软件设计
  • 用来网站备案注册什么公司好wordpress怎么搜索中文主题
  • 网站开发 打标签深圳软件公司排名
  • 邯郸的网站建设电子网站怎么做的
  • 中国企业信用网四川游戏seo整站优化