├── sp01-commons ├── .settings │ ├── org.eclipse.m2e.core.prefs │ └── org.eclipse.jdt.core.prefs ├── src │ └── main │ │ └── java │ │ └── com │ │ └── tedu │ │ ├── sp01 │ │ ├── pojo │ │ │ ├── Item.java │ │ │ ├── User.java │ │ │ └── Order.java │ │ └── service │ │ │ ├── OrderService.java │ │ │ ├── UserService.java │ │ │ └── ItemService.java │ │ └── web │ │ └── util │ │ ├── JsonResult.java │ │ ├── CookieUtil.java │ │ └── JsonUtil.java ├── target │ └── classes │ │ ├── com │ │ └── tedu │ │ │ ├── sp01 │ │ │ ├── pojo │ │ │ │ ├── Item.class │ │ │ │ ├── Order.class │ │ │ │ └── User.class │ │ │ └── service │ │ │ │ ├── ItemService.class │ │ │ │ ├── OrderService.class │ │ │ │ └── UserService.class │ │ │ └── web │ │ │ └── util │ │ │ ├── JsonUtil.class │ │ │ ├── CookieUtil.class │ │ │ ├── JsonResult.class │ │ │ └── JsonUtil$1.class │ │ └── META-INF │ │ ├── MANIFEST.MF │ │ └── maven │ │ └── com.tedu │ │ └── sp01-commons │ │ ├── pom.properties │ │ └── pom.xml ├── a ├── .project ├── .classpath └── pom.xml └── sp-config ├── .project ├── item-service-dev.yml ├── user-service-dev.yml ├── zuul-dev.yml └── order-service-dev.yml /sp01-commons/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /sp01-commons/src/main/java/com/tedu/sp01/pojo/Item.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benwang6/sp_repo/HEAD/sp01-commons/src/main/java/com/tedu/sp01/pojo/Item.java -------------------------------------------------------------------------------- /sp01-commons/target/classes/com/tedu/sp01/pojo/Item.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benwang6/sp_repo/HEAD/sp01-commons/target/classes/com/tedu/sp01/pojo/Item.class -------------------------------------------------------------------------------- /sp01-commons/target/classes/com/tedu/sp01/pojo/Order.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benwang6/sp_repo/HEAD/sp01-commons/target/classes/com/tedu/sp01/pojo/Order.class -------------------------------------------------------------------------------- /sp01-commons/target/classes/com/tedu/sp01/pojo/User.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benwang6/sp_repo/HEAD/sp01-commons/target/classes/com/tedu/sp01/pojo/User.class -------------------------------------------------------------------------------- /sp01-commons/src/main/java/com/tedu/web/util/JsonResult.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benwang6/sp_repo/HEAD/sp01-commons/src/main/java/com/tedu/web/util/JsonResult.java -------------------------------------------------------------------------------- /sp01-commons/target/classes/com/tedu/web/util/JsonUtil.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benwang6/sp_repo/HEAD/sp01-commons/target/classes/com/tedu/web/util/JsonUtil.class -------------------------------------------------------------------------------- /sp01-commons/target/classes/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Built-By: wanght 3 | Build-Jdk: 1.8.0_191 4 | Created-By: Maven Integration for Eclipse 5 | 6 | -------------------------------------------------------------------------------- /sp01-commons/target/classes/com/tedu/web/util/CookieUtil.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benwang6/sp_repo/HEAD/sp01-commons/target/classes/com/tedu/web/util/CookieUtil.class -------------------------------------------------------------------------------- /sp01-commons/target/classes/com/tedu/web/util/JsonResult.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benwang6/sp_repo/HEAD/sp01-commons/target/classes/com/tedu/web/util/JsonResult.class -------------------------------------------------------------------------------- /sp01-commons/target/classes/com/tedu/web/util/JsonUtil$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benwang6/sp_repo/HEAD/sp01-commons/target/classes/com/tedu/web/util/JsonUtil$1.class -------------------------------------------------------------------------------- /sp01-commons/a: -------------------------------------------------------------------------------- 1 | sdfsdf 2 | sadfas 3 | dfasdfas 4 | dfasdf 5 | 6 | 1111111111111111111 7 | 22222222222222222 8 | 888888888888888888 9 | 55555555555555555555555555 10 | -------------------------------------------------------------------------------- /sp01-commons/target/classes/com/tedu/sp01/service/ItemService.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benwang6/sp_repo/HEAD/sp01-commons/target/classes/com/tedu/sp01/service/ItemService.class -------------------------------------------------------------------------------- /sp01-commons/target/classes/com/tedu/sp01/service/OrderService.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benwang6/sp_repo/HEAD/sp01-commons/target/classes/com/tedu/sp01/service/OrderService.class -------------------------------------------------------------------------------- /sp01-commons/target/classes/com/tedu/sp01/service/UserService.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benwang6/sp_repo/HEAD/sp01-commons/target/classes/com/tedu/sp01/service/UserService.class -------------------------------------------------------------------------------- /sp01-commons/src/main/java/com/tedu/sp01/service/OrderService.java: -------------------------------------------------------------------------------- 1 | package com.tedu.sp01.service; 2 | 3 | import com.tedu.sp01.pojo.Order; 4 | 5 | public interface OrderService { 6 | Order getOrder(String orderId); 7 | void addOrder(Order order); 8 | } 9 | -------------------------------------------------------------------------------- /sp01-commons/src/main/java/com/tedu/sp01/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.tedu.sp01.service; 2 | 3 | import com.tedu.sp01.pojo.User; 4 | 5 | public interface UserService { 6 | User getUser(Integer id); 7 | void addScore(Integer id, Integer score); 8 | } 9 | -------------------------------------------------------------------------------- /sp-config/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | sp-config 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /sp01-commons/src/main/java/com/tedu/sp01/service/ItemService.java: -------------------------------------------------------------------------------- 1 | package com.tedu.sp01.service; 2 | 3 | import java.util.List; 4 | 5 | import com.tedu.sp01.pojo.Item; 6 | 7 | public interface ItemService { 8 | List getItems(String orderId); 9 | void decreaseNumbers(List list); 10 | } -------------------------------------------------------------------------------- /sp01-commons/target/classes/META-INF/maven/com.tedu/sp01-commons/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven Integration for Eclipse 2 | #Thu Jun 27 16:59:15 CST 2019 3 | version=0.0.1-SNAPSHOT 4 | groupId=com.tedu 5 | m2e.projectName=sp01-commons 6 | m2e.projectLocation=D\:\\home\\lesson\\1902\\ws2\\sp_repo\\sp01-commons 7 | artifactId=sp01-commons 8 | -------------------------------------------------------------------------------- /sp01-commons/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 3 | org.eclipse.jdt.core.compiler.compliance=1.8 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.release=disabled 6 | org.eclipse.jdt.core.compiler.source=1.8 7 | -------------------------------------------------------------------------------- /sp01-commons/src/main/java/com/tedu/sp01/pojo/User.java: -------------------------------------------------------------------------------- 1 | package com.tedu.sp01.pojo; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | @Data 8 | @NoArgsConstructor 9 | @AllArgsConstructor 10 | public class User { 11 | private Integer id; 12 | private String username; 13 | private String password; 14 | } -------------------------------------------------------------------------------- /sp01-commons/src/main/java/com/tedu/sp01/pojo/Order.java: -------------------------------------------------------------------------------- 1 | package com.tedu.sp01.pojo; 2 | 3 | import java.util.List; 4 | 5 | import lombok.AllArgsConstructor; 6 | import lombok.Data; 7 | import lombok.NoArgsConstructor; 8 | 9 | @Data 10 | @NoArgsConstructor 11 | @AllArgsConstructor 12 | public class Order { 13 | private String id; 14 | private User user; 15 | private List items; 16 | } -------------------------------------------------------------------------------- /sp-config/item-service-dev.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: item-service 4 | rabbitmq: 5 | host: 192.168.64.3 6 | port: 5672 7 | username: admin 8 | password: admin 9 | #server: 10 | # port: 8001 11 | 12 | eureka: 13 | client: 14 | service-url: 15 | defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka 16 | 17 | --- 18 | spring: 19 | profiles: item1 20 | 21 | server: 22 | port: 8001 23 | --- 24 | spring: 25 | profiles: item2 26 | 27 | server: 28 | port: 8002 -------------------------------------------------------------------------------- /sp01-commons/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | sp01-commons 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /sp-config/user-service-dev.yml: -------------------------------------------------------------------------------- 1 | sp: 2 | user-service: 3 | users: "[{\"id\":7, \"username\":\"abc\",\"password\":\"123\"},{\"id\":8, \"username\":\"def\",\"password\":\"456\"},{\"id\":9, \"username\":\"ghi\",\"password\":\"789\"}]" 4 | 5 | spring: 6 | application: 7 | name: user-service 8 | rabbitmq: 9 | host: 192.168.64.3 10 | port: 5672 11 | username: admin 12 | password: admin 13 | 14 | server: 15 | port: 8101 16 | 17 | eureka: 18 | client: 19 | service-url: 20 | defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka 21 | 22 | management: 23 | endpoints: 24 | web: 25 | exposure: 26 | include: refresh -------------------------------------------------------------------------------- /sp-config/zuul-dev.yml: -------------------------------------------------------------------------------- 1 | 2 | spring: 3 | application: 4 | name: zuul 5 | rabbitmq: 6 | host: 192.168.64.3 7 | port: 5672 8 | username: admin 9 | password: admin 10 | 11 | server: 12 | port: 3001 13 | 14 | eureka: 15 | client: 16 | service-url: 17 | defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka 18 | 19 | #zuul: 20 | # routes: 21 | # item-service: /item-service/** 22 | # user-service: /user-service/** 23 | # order-service: /order-service/** 24 | 25 | zuul: 26 | retryable: true 27 | 28 | management: 29 | endpoints: 30 | web: 31 | exposure: 32 | include: hystrix.stream 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /sp-config/order-service-dev.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: order-service 4 | rabbitmq: 5 | host: 192.168.64.3 6 | port: 5672 7 | username: admin 8 | password: admin 9 | 10 | # server: 11 | # port: 8201 12 | 13 | 14 | eureka: 15 | client: 16 | service-url: 17 | defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka 18 | 19 | feign: 20 | hystrix: 21 | enabled: true 22 | 23 | management: 24 | endpoints: 25 | web: 26 | exposure: 27 | include: hystrix.stream 28 | 29 | --- 30 | spring: 31 | profiles: order1 32 | 33 | server: 34 | port: 8201 35 | 36 | --- 37 | spring: 38 | profiles: order2 39 | 40 | server: 41 | port: 8202 -------------------------------------------------------------------------------- /sp01-commons/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /sp01-commons/src/main/java/com/tedu/web/util/CookieUtil.java: -------------------------------------------------------------------------------- 1 | package com.tedu.web.util; 2 | 3 | import javax.servlet.http.Cookie; 4 | import javax.servlet.http.HttpServletRequest; 5 | import javax.servlet.http.HttpServletResponse; 6 | 7 | public class CookieUtil { 8 | 9 | /** 10 | * @param response 11 | * @param name 12 | * @param value 13 | * @param maxAge 14 | */ 15 | public static void setCookie(HttpServletResponse response, 16 | String name, String value, String domain, String path, int maxAge) { 17 | Cookie cookie = new Cookie(name, value); 18 | if(domain != null) { 19 | cookie.setDomain(domain); 20 | } 21 | cookie.setPath(path); 22 | cookie.setMaxAge(maxAge); 23 | response.addCookie(cookie); 24 | } 25 | public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) { 26 | setCookie(response, name, value, null, "/", maxAge); 27 | } 28 | public static void setCookie(HttpServletResponse response, String name, String value) { 29 | setCookie(response, name, value, null, "/", 3600); 30 | } 31 | public static void setCookie(HttpServletResponse response, String name) { 32 | setCookie(response, name, "", null, "/", 3600); 33 | } 34 | 35 | /** 36 | * @param request 37 | * @param name 38 | * @return 39 | */ 40 | public static String getCookie(HttpServletRequest request, String name) { 41 | String value = null; 42 | Cookie[] cookies = request.getCookies(); 43 | if (null != cookies) { 44 | for (Cookie cookie : cookies) { 45 | if (cookie.getName().equals(name)) { 46 | value = cookie.getValue(); 47 | } 48 | } 49 | } 50 | return value; 51 | } 52 | 53 | /** 54 | * @param response 55 | * @param name 56 | * @return 57 | */ 58 | public static void removeCookie(HttpServletResponse response, String name, String domain, String path) { 59 | setCookie(response, name, "", domain, path, 0); 60 | } 61 | 62 | } -------------------------------------------------------------------------------- /sp01-commons/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | com.tedu 6 | sp01-commons 7 | 0.0.1-SNAPSHOT 8 | sp01-commons 9 | 10 | 11 | com.fasterxml.jackson.module 12 | jackson-module-parameter-names 13 | 2.9.8 14 | 15 | 16 | com.fasterxml.jackson.datatype 17 | jackson-datatype-jdk8 18 | 2.9.8 19 | 20 | 21 | com.fasterxml.jackson.datatype 22 | jackson-datatype-jsr310 23 | 2.9.8 24 | 25 | 26 | com.fasterxml.jackson.datatype 27 | jackson-datatype-guava 28 | 2.9.8 29 | 30 | 31 | 32 | org.projectlombok 33 | lombok 34 | 1.18.6 35 | 36 | 37 | javax.servlet 38 | javax.servlet-api 39 | 3.1.0 40 | 41 | 42 | org.slf4j 43 | slf4j-api 44 | 1.7.26 45 | 46 | 47 | org.apache.commons 48 | commons-lang3 49 | 3.9 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.apache.maven.plugins 58 | maven-compiler-plugin 59 | 3.8.0 60 | 61 | 1.8 62 | 1.8 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /sp01-commons/target/classes/META-INF/maven/com.tedu/sp01-commons/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | com.tedu 6 | sp01-commons 7 | 0.0.1-SNAPSHOT 8 | sp01-commons 9 | 10 | 11 | com.fasterxml.jackson.module 12 | jackson-module-parameter-names 13 | 2.9.8 14 | 15 | 16 | com.fasterxml.jackson.datatype 17 | jackson-datatype-jdk8 18 | 2.9.8 19 | 20 | 21 | com.fasterxml.jackson.datatype 22 | jackson-datatype-jsr310 23 | 2.9.8 24 | 25 | 26 | com.fasterxml.jackson.datatype 27 | jackson-datatype-guava 28 | 2.9.8 29 | 30 | 31 | 32 | org.projectlombok 33 | lombok 34 | 1.18.6 35 | 36 | 37 | javax.servlet 38 | javax.servlet-api 39 | 3.1.0 40 | 41 | 42 | org.slf4j 43 | slf4j-api 44 | 1.7.26 45 | 46 | 47 | org.apache.commons 48 | commons-lang3 49 | 3.9 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.apache.maven.plugins 58 | maven-compiler-plugin 59 | 3.8.0 60 | 61 | 1.8 62 | 1.8 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /sp01-commons/src/main/java/com/tedu/web/util/JsonUtil.java: -------------------------------------------------------------------------------- 1 | package com.tedu.web.util; 2 | 3 | import java.io.File; 4 | import java.io.FileWriter; 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.io.InputStreamReader; 8 | import java.io.Writer; 9 | import java.math.BigDecimal; 10 | import java.math.BigInteger; 11 | import java.net.URL; 12 | import java.nio.charset.StandardCharsets; 13 | import java.text.SimpleDateFormat; 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | import org.apache.commons.lang3.StringUtils; 18 | 19 | import com.fasterxml.jackson.annotation.JsonInclude; 20 | import com.fasterxml.jackson.core.JsonGenerator; 21 | import com.fasterxml.jackson.core.JsonParser; 22 | import com.fasterxml.jackson.core.JsonProcessingException; 23 | import com.fasterxml.jackson.core.type.TypeReference; 24 | import com.fasterxml.jackson.databind.DeserializationFeature; 25 | import com.fasterxml.jackson.databind.JsonNode; 26 | import com.fasterxml.jackson.databind.ObjectMapper; 27 | import com.fasterxml.jackson.databind.SerializationFeature; 28 | import com.fasterxml.jackson.databind.node.ObjectNode; 29 | import com.fasterxml.jackson.datatype.guava.GuavaModule; 30 | import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; 31 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; 32 | import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; 33 | 34 | import lombok.extern.slf4j.Slf4j; 35 | 36 | @Slf4j 37 | public class JsonUtil { 38 | private static ObjectMapper mapper; 39 | private static JsonInclude.Include DEFAULT_PROPERTY_INCLUSION = JsonInclude.Include.NON_DEFAULT; 40 | private static boolean IS_ENABLE_INDENT_OUTPUT = false; 41 | private static String CSV_DEFAULT_COLUMN_SEPARATOR = ","; 42 | static { 43 | try { 44 | initMapper(); 45 | configPropertyInclusion(); 46 | configIndentOutput(); 47 | configCommon(); 48 | } catch (Exception e) { 49 | log.error("jackson config error", e); 50 | } 51 | } 52 | 53 | private static void initMapper() { 54 | mapper = new ObjectMapper(); 55 | } 56 | 57 | private static void configCommon() { 58 | config(mapper); 59 | } 60 | 61 | private static void configPropertyInclusion() { 62 | mapper.setSerializationInclusion(DEFAULT_PROPERTY_INCLUSION); 63 | } 64 | 65 | private static void configIndentOutput() { 66 | mapper.configure(SerializationFeature.INDENT_OUTPUT, IS_ENABLE_INDENT_OUTPUT); 67 | } 68 | 69 | private static void config(ObjectMapper objectMapper) { 70 | objectMapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN); 71 | objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); 72 | objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); 73 | objectMapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY); 74 | objectMapper.enable(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS); 75 | objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 76 | objectMapper.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES); 77 | objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); 78 | objectMapper.enable(JsonParser.Feature.ALLOW_COMMENTS); 79 | objectMapper.disable(JsonGenerator.Feature.ESCAPE_NON_ASCII); 80 | objectMapper.enable(JsonGenerator.Feature.IGNORE_UNKNOWN); 81 | objectMapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES); 82 | objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); 83 | objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); 84 | objectMapper.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES); 85 | objectMapper.registerModule(new ParameterNamesModule()); 86 | objectMapper.registerModule(new Jdk8Module()); 87 | objectMapper.registerModule(new JavaTimeModule()); 88 | objectMapper.registerModule(new GuavaModule()); 89 | } 90 | public static void setSerializationInclusion(JsonInclude.Include inclusion) { 91 | DEFAULT_PROPERTY_INCLUSION = inclusion; 92 | configPropertyInclusion(); 93 | } 94 | 95 | public static void setIndentOutput(boolean isEnable) { 96 | IS_ENABLE_INDENT_OUTPUT = isEnable; 97 | configIndentOutput(); 98 | } 99 | 100 | public static V from(URL url, Class c) { 101 | try { 102 | return mapper.readValue(url, c); 103 | } catch (IOException e) { 104 | log.error("jackson from error, url: {}, type: {}", url.getPath(), c, e); 105 | return null; 106 | } 107 | } 108 | 109 | public static V from(InputStream inputStream, Class c) { 110 | try { 111 | return mapper.readValue(inputStream, c); 112 | } catch (IOException e) { 113 | log.error("jackson from error, type: {}", c, e); 114 | return null; 115 | } 116 | } 117 | 118 | public static V from(File file, Class c) { 119 | try { 120 | return mapper.readValue(file, c); 121 | } catch (IOException e) { 122 | log.error("jackson from error, file path: {}, type: {}", file.getPath(), c, e); 123 | return null; 124 | } 125 | } 126 | 127 | public static V from(Object jsonObj, Class c) { 128 | try { 129 | return mapper.readValue(jsonObj.toString(), c); 130 | } catch (IOException e) { 131 | log.error("jackson from error, json: {}, type: {}", jsonObj.toString(), c, e); 132 | return null; 133 | } 134 | } 135 | 136 | public static V from(String json, Class c) { 137 | try { 138 | return mapper.readValue(json, c); 139 | } catch (IOException e) { 140 | log.error("jackson from error, json: {}, type: {}", json, c, e); 141 | return null; 142 | } 143 | } 144 | 145 | public static V from(URL url, TypeReference type) { 146 | try { 147 | return mapper.readValue(url, type); 148 | } catch (IOException e) { 149 | log.error("jackson from error, url: {}, type: {}", url.getPath(), type, e); 150 | return null; 151 | } 152 | } 153 | 154 | public static V from(InputStream inputStream, TypeReference type) { 155 | try { 156 | return mapper.readValue(inputStream, type); 157 | } catch (IOException e) { 158 | log.error("jackson from error, type: {}", type, e); 159 | return null; 160 | } 161 | } 162 | 163 | public static V from(File file, TypeReference type) { 164 | try { 165 | return mapper.readValue(file, type); 166 | } catch (IOException e) { 167 | log.error("jackson from error, file path: {}, type: {}", file.getPath(), type, e); 168 | return null; 169 | } 170 | } 171 | 172 | public static V from(Object jsonObj, TypeReference type) { 173 | try { 174 | return mapper.readValue(jsonObj.toString(), type); 175 | } catch (IOException e) { 176 | log.error("jackson from error, json: {}, type: {}", jsonObj.toString(), type, e); 177 | return null; 178 | } 179 | } 180 | 181 | public static V from(String json, TypeReference type) { 182 | try { 183 | return mapper.readValue(json, type); 184 | } catch (IOException e) { 185 | log.error("jackson from error, json: {}, type: {}", json, type, e); 186 | return null; 187 | } 188 | } 189 | 190 | public static String to(List list) { 191 | try { 192 | return mapper.writeValueAsString(list); 193 | } catch (JsonProcessingException e) { 194 | log.error("jackson to error, obj: {}", list, e); 195 | return null; 196 | } 197 | } 198 | 199 | public static String to(V v) { 200 | try { 201 | return mapper.writeValueAsString(v); 202 | } catch (JsonProcessingException e) { 203 | log.error("jackson to error, obj: {}", v, e); 204 | return null; 205 | } 206 | } 207 | 208 | public static void toFile(String path, List list) { 209 | try (Writer writer = new FileWriter(new File(path), true)) { 210 | mapper.writer().writeValues(writer).writeAll(list); 211 | writer.flush(); 212 | } catch (Exception e) { 213 | log.error("jackson to file error, path: {}, list: {}", path, list, e); 214 | } 215 | } 216 | 217 | public static void toFile(String path, V v) { 218 | try (Writer writer = new FileWriter(new File(path), true)) { 219 | mapper.writer().writeValues(writer).write(v); 220 | writer.flush(); 221 | } catch (Exception e) { 222 | log.error("jackson to file error, path: {}, obj: {}", path, v, e); 223 | } 224 | } 225 | 226 | public static String getString(String json, String key) { 227 | if (StringUtils.isEmpty(json)) { 228 | return null; 229 | } 230 | try { 231 | JsonNode node = mapper.readTree(json); 232 | if (null != node) { 233 | return node.get(key).toString(); 234 | } else { 235 | return null; 236 | } 237 | } catch (IOException e) { 238 | log.error("jackson get string error, json: {}, key: {}", json, key, e); 239 | return null; 240 | } 241 | } 242 | 243 | public static Integer getInt(String json, String key) { 244 | if (StringUtils.isEmpty(json)) { 245 | return null; 246 | } 247 | try { 248 | JsonNode node = mapper.readTree(json); 249 | if (null != node) { 250 | return node.get(key).intValue(); 251 | } else { 252 | return null; 253 | } 254 | } catch (IOException e) { 255 | log.error("jackson get int error, json: {}, key: {}", json, key, e); 256 | return null; 257 | } 258 | } 259 | 260 | public static Long getLong(String json, String key) { 261 | if (StringUtils.isEmpty(json)) { 262 | return null; 263 | } 264 | try { 265 | JsonNode node = mapper.readTree(json); 266 | if (null != node) { 267 | return node.get(key).longValue(); 268 | } else { 269 | return null; 270 | } 271 | } catch (IOException e) { 272 | log.error("jackson get long error, json: {}, key: {}", json, key, e); 273 | return null; 274 | } 275 | } 276 | 277 | public static Double getDouble(String json, String key) { 278 | if (StringUtils.isEmpty(json)) { 279 | return null; 280 | } 281 | try { 282 | JsonNode node = mapper.readTree(json); 283 | if (null != node) { 284 | return node.get(key).doubleValue(); 285 | } else { 286 | return null; 287 | } 288 | } catch (IOException e) { 289 | log.error("jackson get double error, json: {}, key: {}", json, key, e); 290 | return null; 291 | } 292 | } 293 | 294 | public static BigInteger getBigInteger(String json, String key) { 295 | if (StringUtils.isEmpty(json)) { 296 | return new BigInteger(String.valueOf(0.00)); 297 | } 298 | try { 299 | JsonNode node = mapper.readTree(json); 300 | if (null != node) { 301 | return node.get(key).bigIntegerValue(); 302 | } else { 303 | return null; 304 | } 305 | } catch (IOException e) { 306 | log.error("jackson get biginteger error, json: {}, key: {}", json, key, e); 307 | return null; 308 | } 309 | } 310 | 311 | public static BigDecimal getBigDecimal(String json, String key) { 312 | if (StringUtils.isEmpty(json)) { 313 | return null; 314 | } 315 | try { 316 | JsonNode node = mapper.readTree(json); 317 | if (null != node) { 318 | return node.get(key).decimalValue(); 319 | } else { 320 | return null; 321 | } 322 | } catch (IOException e) { 323 | log.error("jackson get bigdecimal error, json: {}, key: {}", json, key, e); 324 | return null; 325 | } 326 | } 327 | 328 | public static boolean getBoolean(String json, String key) { 329 | if (StringUtils.isEmpty(json)) { 330 | return false; 331 | } 332 | try { 333 | JsonNode node = mapper.readTree(json); 334 | if (null != node) { 335 | return node.get(key).booleanValue(); 336 | } else { 337 | return false; 338 | } 339 | } catch (IOException e) { 340 | log.error("jackson get boolean error, json: {}, key: {}", json, key, e); 341 | return false; 342 | } 343 | } 344 | 345 | public static byte[] getByte(String json, String key) { 346 | if (StringUtils.isEmpty(json)) { 347 | return null; 348 | } 349 | try { 350 | JsonNode node = mapper.readTree(json); 351 | if (null != node) { 352 | return node.get(key).binaryValue(); 353 | } else { 354 | return null; 355 | } 356 | } catch (IOException e) { 357 | log.error("jackson get byte error, json: {}, key: {}", json, key, e); 358 | return null; 359 | } 360 | } 361 | 362 | public static ArrayList getList(String json, String key) { 363 | if (StringUtils.isEmpty(json)) { 364 | return null; 365 | } 366 | String string = getString(json, key); 367 | return from(string, new TypeReference>() {}); 368 | } 369 | 370 | public static String add(String json, String key, T value) { 371 | try { 372 | JsonNode node = mapper.readTree(json); 373 | add(node, key, value); 374 | return node.toString(); 375 | } catch (IOException e) { 376 | log.error("jackson add error, json: {}, key: {}, value: {}", json, key, value, e); 377 | return json; 378 | } 379 | } 380 | 381 | private static void add(JsonNode jsonNode, String key, T value) { 382 | if (value instanceof String) { 383 | ((ObjectNode) jsonNode).put(key, (String) value); 384 | } else if (value instanceof Short) { 385 | ((ObjectNode) jsonNode).put(key, (Short) value); 386 | } else if (value instanceof Integer) { 387 | ((ObjectNode) jsonNode).put(key, (Integer) value); 388 | } else if (value instanceof Long) { 389 | ((ObjectNode) jsonNode).put(key, (Long) value); 390 | } else if (value instanceof Float) { 391 | ((ObjectNode) jsonNode).put(key, (Float) value); 392 | } else if (value instanceof Double) { 393 | ((ObjectNode) jsonNode).put(key, (Double) value); 394 | } else if (value instanceof BigDecimal) { 395 | ((ObjectNode) jsonNode).put(key, (BigDecimal) value); 396 | } else if (value instanceof BigInteger) { 397 | ((ObjectNode) jsonNode).put(key, (BigInteger) value); 398 | } else if (value instanceof Boolean) { 399 | ((ObjectNode) jsonNode).put(key, (Boolean) value); 400 | } else if (value instanceof byte[]) { 401 | ((ObjectNode) jsonNode).put(key, (byte[]) value); 402 | } else { 403 | ((ObjectNode) jsonNode).put(key, to(value)); 404 | } 405 | } 406 | 407 | public static String remove(String json, String key) { 408 | try { 409 | JsonNode node = mapper.readTree(json); 410 | ((ObjectNode) node).remove(key); 411 | return node.toString(); 412 | } catch (IOException e) { 413 | log.error("jackson remove error, json: {}, key: {}", json, key, e); 414 | return json; 415 | } 416 | } 417 | 418 | public static String update(String json, String key, T value) { 419 | try { 420 | JsonNode node = mapper.readTree(json); 421 | ((ObjectNode) node).remove(key); 422 | add(node, key, value); 423 | return node.toString(); 424 | } catch (IOException e) { 425 | log.error("jackson update error, json: {}, key: {}, value: {}", json, key, value, e); 426 | return json; 427 | } 428 | } 429 | 430 | public static String format(String json) { 431 | try { 432 | JsonNode node = mapper.readTree(json); 433 | return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node); 434 | } catch (IOException e) { 435 | log.error("jackson format json error, json: {}", json, e); 436 | return json; 437 | } 438 | } 439 | 440 | public static boolean isJson(String json) { 441 | try { 442 | mapper.readTree(json); 443 | return true; 444 | } catch (Exception e) { 445 | log.error("jackson check json error, json: {}", json, e); 446 | return false; 447 | } 448 | } 449 | 450 | private static InputStream getResourceStream(String name) { 451 | return JsonUtil.class.getClassLoader().getResourceAsStream(name); 452 | } 453 | 454 | private static InputStreamReader getResourceReader(InputStream inputStream) { 455 | if (null == inputStream) { 456 | return null; 457 | } 458 | return new InputStreamReader(inputStream, StandardCharsets.UTF_8); 459 | } 460 | } --------------------------------------------------------------------------------