Skip to content

Commit 95c8366

Browse files
committed
处理异常
1 parent b9d84fb commit 95c8366

File tree

4 files changed

+35
-37
lines changed

4 files changed

+35
-37
lines changed

SpringBootDemo/src/main/java/com/xiaour/spring/boot/config/DataBaseConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public void setEnvironment(Environment env) {
4040
*/
4141
@Bean(name = "dataSource",destroyMethod = "close")
4242
public DataSource dataSource() {
43-
log.debug(env.getActiveProfiles().toString());
4443

4544
DruidDataSource dataSource = new DruidDataSource();
4645
dataSource.setUrl(propertyResolver.getProperty("url"));
@@ -64,7 +63,7 @@ public DataSource dataSource() {
6463
dataSource.setFilters("stat,wall");
6564
dataSource.init();
6665
} catch (SQLException e) {
67-
66+
log.error(e.getMessage());
6867
}
6968
return dataSource;
7069
}

SpringBootDemo/src/main/java/com/xiaour/spring/boot/config/MybatisConfiguration.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public SqlSessionFactory sqlSessionFactory() {
6464

6565
return sessionFactory.getObject();
6666
} catch (Exception e) {
67-
e.printStackTrace();
6867
logger.warn("Could not confiure mybatis session factory");
6968
return null;
7069
}

SpringBootDemo/src/main/java/com/xiaour/spring/boot/config/RedisConfig.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.xiaour.spring.boot.config;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
35
import org.springframework.beans.factory.annotation.Autowired;
46
import org.springframework.beans.factory.annotation.Qualifier;
57
import org.springframework.beans.factory.annotation.Value;
@@ -19,7 +21,8 @@
1921
*/
2022
@Configuration
2123
public class RedisConfig {
22-
24+
private static Logger log = LoggerFactory.getLogger(RedisConfig.class);
25+
2326

2427
@Bean(name= "jedis.pool")
2528
@Autowired
@@ -33,11 +36,16 @@ public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig confi
3336
public JedisPoolConfig jedisPoolConfig (@Value("${jedis.pool.config.maxTotal}")int maxTotal,
3437
@Value("${jedis.pool.config.maxIdle}")int maxIdle,
3538
@Value("${jedis.pool.config.maxWaitMillis}")int maxWaitMillis) {
36-
JedisPoolConfig config = new JedisPoolConfig();
37-
config.setMaxTotal(maxTotal);
38-
config.setMaxIdle(maxIdle);
39-
config.setMaxWaitMillis(maxWaitMillis);
40-
return config;
39+
try {
40+
JedisPoolConfig config = new JedisPoolConfig();
41+
config.setMaxTotal(maxTotal);
42+
config.setMaxIdle(maxIdle);
43+
config.setMaxWaitMillis(maxWaitMillis);
44+
return config;
45+
} catch (Exception e) {
46+
log.error(e.getMessage());
47+
}
48+
return null;
4149
}
4250

4351
}

SpringBootDemo/src/main/java/com/xiaour/spring/boot/utils/JsonUtil.java

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,36 @@
1010
import com.fasterxml.jackson.databind.ObjectMapper;
1111

1212
public class JsonUtil {
13+
14+
1315

14-
public static String getJsonString(Object javaObj) {
16+
public static String getJsonString(Object javaObj) throws JsonProcessingException {
1517
String ret = "{}";
18+
1619
ObjectMapper mapper = new ObjectMapper();
17-
try {
18-
ret = mapper.writeValueAsString(javaObj);
19-
} catch (JsonProcessingException e) {
20-
e.printStackTrace();
21-
}
20+
21+
ret = mapper.writeValueAsString(javaObj);
2222

2323
return ret.replaceAll(":null",":\"\"");
2424

2525
}
2626

27-
public static <T> T readJson2Obj(String json, Class<T> classObj) {
27+
public static <T> T readJson2Obj(String json, Class<T> classObj) throws JsonParseException,JsonMappingException,IOException {
2828
T ret = null;
29+
2930
ObjectMapper mapper = new ObjectMapper();
30-
try {
31-
ret = mapper.readValue(json, classObj);
32-
} catch (JsonParseException e) {
33-
e.printStackTrace();
34-
} catch (JsonMappingException e) {
35-
e.printStackTrace();
36-
} catch (IOException e) {
37-
e.printStackTrace();
38-
}
39-
31+
32+
ret = mapper.readValue(json, classObj);
33+
4034
return ret;
4135
}
4236

43-
public static <T> T[] readJson2Array(String json, Class<T[]> classObj) {
37+
public static <T> T[] readJson2Array(String json, Class<T[]> classObj) throws JsonParseException,JsonMappingException,IOException {
4438
T[] ret = null;
39+
4540
ObjectMapper mapper = new ObjectMapper();
46-
try {
47-
ret = mapper.readValue(json, classObj);
48-
} catch (JsonParseException e) {
49-
e.printStackTrace();
50-
} catch (JsonMappingException e) {
51-
e.printStackTrace();
52-
} catch (IOException e) {
53-
e.printStackTrace();
54-
}
41+
42+
ret = mapper.readValue(json, classObj);
5543

5644
return ret;
5745
}
@@ -60,7 +48,11 @@ public static void main(String [] args){
6048
Map<String,Object> map= new HashMap<>();
6149
map.put("name","xiaochouyu");
6250
map.put("stak","小丑鱼");
63-
System.out.println(getJsonString(map));
51+
try {
52+
System.out.println(getJsonString(map));
53+
} catch (JsonProcessingException e) {
54+
e.printStackTrace();
55+
}
6456
}
6557

6658
}

0 commit comments

Comments
 (0)