工业测量
This commit is contained in:
		| @@ -0,0 +1,59 @@ | ||||
| package com.xkrs.encapsulation; | ||||
|  | ||||
| import java.io.Serializable; | ||||
|  | ||||
| /** | ||||
|  * 输出信息对象 | ||||
|  * @author tajochen | ||||
|  * @param <T> | ||||
|  */ | ||||
| public class EncapsulationObejct<T> implements Serializable { | ||||
|  | ||||
|     /** | ||||
|      * 状态码 | ||||
|      */ | ||||
|     int status; | ||||
|  | ||||
|     /** | ||||
|      * 提示信息 | ||||
|      */ | ||||
|     String msg; | ||||
|  | ||||
|     /** | ||||
|      * 数据 | ||||
|      */ | ||||
|     T data; | ||||
|  | ||||
|     public int getStatus() { | ||||
|         return status; | ||||
|     } | ||||
|  | ||||
|     public void setStatus(int status) { | ||||
|         this.status = status; | ||||
|     } | ||||
|  | ||||
|     public String getMsg() { | ||||
|         return msg; | ||||
|     } | ||||
|  | ||||
|     public void setMsg(String msg) { | ||||
|         this.msg = msg; | ||||
|     } | ||||
|  | ||||
|     public T getData() { | ||||
|         return data; | ||||
|     } | ||||
|  | ||||
|     public void setData(T data) { | ||||
|         this.data = data; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public String toString() { | ||||
|         return "EncapsulationObejct{" + | ||||
|                 "status=" + status + | ||||
|                 ", msg='" + msg + '\'' + | ||||
|                 ", data=" + data + | ||||
|                 '}'; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,96 @@ | ||||
| package com.xkrs.encapsulation; | ||||
|  | ||||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||||
| import com.fasterxml.jackson.databind.DeserializationFeature; | ||||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||||
| import com.fasterxml.jackson.databind.SerializationFeature; | ||||
| import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
| import org.springframework.context.MessageSource; | ||||
| import org.springframework.context.support.ResourceBundleMessageSource; | ||||
| import org.springframework.stereotype.Component; | ||||
| import org.springframework.validation.FieldError; | ||||
|  | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| import java.util.Locale; | ||||
| import java.util.Properties; | ||||
|  | ||||
| /** | ||||
|  * 输出信息封装 | ||||
|  * @author tajochen | ||||
|  */ | ||||
| @Component | ||||
| public class OutputEncapsulation { | ||||
|  | ||||
|     private static final Logger logger = LoggerFactory.getLogger(OutputEncapsulation.class); | ||||
|  | ||||
|     /** | ||||
|      * 读取多国语言文件 | ||||
|      * @return | ||||
|      */ | ||||
|     public static MessageSource messageSource() { | ||||
|         Properties properties = new Properties(); | ||||
|         // 使用ClassLoader加载properties配置文件生成对应的输入流 | ||||
|         InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties"); | ||||
|         // 使用properties对象加载输入流 | ||||
|         try { | ||||
|             properties.load(in); | ||||
|         } catch (IOException e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|         ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); | ||||
|         messageSource.setBasename("i18n/messages"); | ||||
|         messageSource.setDefaultEncoding("UTF-8"); | ||||
|         return messageSource; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 封装输出数据 | ||||
|      * @param promptMessageEnum | ||||
|      * @param obj | ||||
|      * @return | ||||
|      */ | ||||
|     public static String outputEncapsulationObject(PromptMessageEnum promptMessageEnum, Object obj, Locale locale) { | ||||
|  | ||||
|         EncapsulationObejct encapsulationObejct = new EncapsulationObejct(); | ||||
|         encapsulationObejct.setStatus(promptMessageEnum.getCode()); | ||||
|         encapsulationObejct.setMsg(messageSource().getMessage(promptMessageEnum.getText(),null,locale)); | ||||
|         encapsulationObejct.setData(obj); | ||||
|  | ||||
|         ObjectMapper objectMapper = new ObjectMapper(); | ||||
|         // 忽略无法转换的对象 | ||||
|         objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false); | ||||
|         // 忽略json字符串中不识别的属性 | ||||
|         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||||
|         // 解决jackson无法反序列化LocalDateTime的问题,引入jsr310标准 | ||||
|         JavaTimeModule javaTimeModule = new JavaTimeModule(); | ||||
|         objectMapper.registerModule(javaTimeModule); | ||||
|         objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); | ||||
|         String strByEo = ""; | ||||
|         try { | ||||
|             strByEo = objectMapper.writeValueAsString(encapsulationObejct); | ||||
|         } catch (JsonProcessingException e) { | ||||
|             e.printStackTrace(); | ||||
|             logger.warn(e.toString()); | ||||
|         } | ||||
|         return strByEo; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 输出请求值检验错误信息 | ||||
|      * @param fieldErrors | ||||
|      * @return | ||||
|      */ | ||||
|     public static String outputEncapsulationErrorList(List<FieldError> fieldErrors, Locale locale){ | ||||
|         List<String> errorMsg = new ArrayList<>(); | ||||
|         for (FieldError fieldError : fieldErrors) { | ||||
|             String errMessage = fieldError.getDefaultMessage().subSequence(1,fieldError.getDefaultMessage().length()-1).toString(); | ||||
|             errorMsg.add(messageSource().getMessage(errMessage,null,locale)); | ||||
|         } | ||||
|         return outputEncapsulationObject(PromptMessageEnum.PARAM_ILLEGAL,errorMsg,locale); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										65
									
								
								src/main/java/com/xkrs/encapsulation/PromptMessageEnum.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								src/main/java/com/xkrs/encapsulation/PromptMessageEnum.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,65 @@ | ||||
| package com.xkrs.encapsulation; | ||||
|  | ||||
| /** | ||||
|  * 提示信息枚举 | ||||
|  * @author tajochen | ||||
|  */ | ||||
| public enum PromptMessageEnum{ | ||||
|  | ||||
|     // 执行成功 | ||||
|     SUCCESS(0, "sys.message.success"), | ||||
|  | ||||
|  | ||||
|     // 用户权限错误或非法操作:  1001-1999 | ||||
|     USER_NOT_LOGGED(1001, "sys.message.user.not_logged_in"), | ||||
|     USER_LOGIN_ERROR(1002, "sys.message.user.login_error"), | ||||
|     USER_ACCOUNT_FORBIDDEN(1003, "sys.message.user.account_forbidden"), | ||||
|     USER_ACCOUNT_NOT_ACTIVATED(1004, "sys.message.user.account_not_activated"), | ||||
|     USER_HAS_OVERTIME(1005, "sys.message.user.overtime"), | ||||
|     USER_NO_PERMISSION(1006,"sys.message.user.no_permission"), | ||||
|     USER_ALREADY_LOGGED(1007, "sys.message.user.already_logged"), | ||||
|  | ||||
|     // 请求参数错误或非法:2001-2999 | ||||
|     PARAM_NULL(2001, "sys.message.param.null"), | ||||
|     PARAM_ILLEGAL(2002, "sys.message.param.illegal"), | ||||
|  | ||||
|     // 数据返回错误:3001-3999 | ||||
|     DATA_NONE(3001, "sys.message.data.none"), | ||||
|  | ||||
|     DATA_WRONG(3002, "sys.message.data.wrong"), | ||||
|     DATA_EXIT(3003,"sys.message.exit"), | ||||
|  | ||||
|     // 操作失败:4001-4999 | ||||
|     PROCESS_FAIL(4001,"sys.message.process.fail"), | ||||
|     PROCESS_OVERTIME(4002,"sys.message.process.overtime"), | ||||
|     FILE_EXISTS(4003,"sys.message.file.exists"), | ||||
|     FILE_WRITE_ERROR(4004,"sys.message.file.write.error"), | ||||
|     FILE_READ_ERROR(4005,"sys.message.file.read.error"), | ||||
|  | ||||
|     // 系统内部错误或异常:5001-5999 | ||||
|     SYSTEM_INNER_ERROR(5001,"sys.message.system.inner_error"), | ||||
|     SYSTEM_ABNORMAL(5002,"sys.message.system.abnormal"), | ||||
|     SYSTEM_BUSY(5003,"sys.message.system.busy"), | ||||
|     SYSTEM_MAINTAIN(5004,"sys.message.system.maintain"), | ||||
|  | ||||
|     // 数据库错误:6001-6999 | ||||
|     DATABASE_ERROR(6001,"sys.message.database.error"); | ||||
|  | ||||
|     private int code; | ||||
|  | ||||
|     private String text; | ||||
|  | ||||
|     private PromptMessageEnum(int code,String text) { | ||||
|         this.code = code; | ||||
|         this.text = text; | ||||
|     } | ||||
|  | ||||
|     public String getText() { | ||||
|         return this.text; | ||||
|     } | ||||
|  | ||||
|     public int getCode() { | ||||
|         return this.code; | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user