Skip to content

Commit 0474f39

Browse files
author
vison.cao
committed
Merge branch 'main' into origin/main
2 parents 682b553 + f18eb4c commit 0474f39

29 files changed

+1058
-57
lines changed

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
</properties>
1818

1919
<dependencies>
20+
<dependency>
21+
<groupId>com.google.code.gson</groupId>
22+
<artifactId>gson</artifactId>
23+
<version>2.8.6</version>
24+
<type>jar</type>
25+
</dependency>
2026
<dependency>
2127
<groupId>javax.websocket</groupId>
2228
<artifactId>javax.websocket-api</artifactId>
@@ -50,6 +56,16 @@
5056
<artifactId>tomcat-websocket</artifactId>
5157
<version>${tomcat.version}</version>
5258
</dependency>
59+
<dependency>
60+
<groupId>org.reflections</groupId>
61+
<artifactId>reflections</artifactId>
62+
<version>0.9.12</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>io.pebbletemplates</groupId>
66+
<artifactId>pebble</artifactId>
67+
<version>3.1.4</version>
68+
</dependency>
5369
</dependencies>
5470

5571
<build>

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.vison.webmvc;
2+
3+
/**
4+
*
5+
* @author vison.cao <visonforcoding@gmail.com>
6+
*/
7+
import com.vison.webmvc.config.App;
8+
import java.util.Optional;
9+
import java.util.HashMap;
10+
11+
/**
12+
*
13+
* @author vison.cao <visonforcoding@gmail.com>
14+
*/
15+
public class Response {
16+
17+
private int code;
18+
private String msg;
19+
private Object data;
20+
private String _uniq_req_no = App._uniq_req_no;
21+
22+
public Response(int code, String msg, Object data) {
23+
this.code = code;
24+
this.msg = msg;
25+
this.data = data;
26+
if (data == null) {
27+
this.data = new HashMap<>();
28+
}
29+
if (data instanceof Optional) {
30+
Optional d = (Optional) data;
31+
if (!d.isPresent()) {
32+
this.data = new HashMap();
33+
}
34+
}
35+
}
36+
37+
public Response(int code, String msg) {
38+
this.code = code;
39+
this.msg = msg;
40+
Object d = new HashMap<>();
41+
this.data = d;
42+
}
43+
44+
public void setCode(int code) {
45+
this.code = code;
46+
}
47+
48+
public void setMsg(String msg) {
49+
this.msg = msg;
50+
}
51+
52+
public void setData(Object data) {
53+
this.data = data;
54+
}
55+
56+
public int getCode() {
57+
return code;
58+
}
59+
60+
public String getMsg() {
61+
return msg;
62+
}
63+
64+
public Object getData() {
65+
return data;
66+
}
67+
68+
public String getUniq_req_no() {
69+
return App._uniq_req_no;
70+
}
71+
72+
public void setUniq_req_no(String _uniq_req_no) {
73+
this._uniq_req_no = _uniq_req_no;
74+
}
75+
76+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.vison.webmvc;
7+
8+
/**
9+
*
10+
* @author vison.cao <visonforcoding@gmail.com>
11+
*/
12+
public class ResponseCode {
13+
14+
public static Integer success = 0;
15+
public static Integer dbInsertFail = 101;
16+
public static Integer parametrErrror = 510;
17+
18+
/**
19+
* 未登录
20+
*/
21+
public static Integer unLogin = 401;
22+
23+
public static Integer loginFail = 402;
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.vison.webmvc;
2+
3+
import java.lang.reflect.Method;
4+
5+
/**
6+
*
7+
* @author vison.cao <visonforcoding@gmail.com>
8+
*/
9+
public class Test {
10+
11+
public static void main(String[] args) throws Exception {
12+
Class stdClass = Student.class;
13+
Method method = stdClass.getMethod("getScore", String.class);
14+
Object obj = stdClass.getDeclaredConstructor().newInstance();
15+
System.out.print(method.invoke(obj, "888"));
16+
}
17+
18+
}
19+
20+
class Student extends Person {
21+
22+
public int getScore(String type) {
23+
return 99;
24+
}
25+
26+
private int getGrade(int year) {
27+
return 1;
28+
}
29+
}
30+
31+
class Person {
32+
33+
public String getName() {
34+
return "Person";
35+
}
36+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.vison.webmvc.config;
2+
3+
/**
4+
*
5+
* @author vison.cao <visonforcoding@gmail.com>
6+
*/
7+
public class App {
8+
9+
public static final String SESSION_USER = "user"; // 用户对象
10+
public static String _uniq_req_no = null;
11+
public static String OrderNoIncKey = ":order:no";
12+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.vison.webmvc.config;
2+
3+
import org.apache.logging.log4j.Logger;
4+
import org.apache.logging.log4j.LogManager;
5+
6+
/**
7+
*
8+
* @author vison.cao <visonforcoding@gmail.com>
9+
*/
10+
public class Log {
11+
12+
private static String msgTraceNo;
13+
14+
public static void setMsgTraceNo(String msgTraceNo) {
15+
Log.msgTraceNo = msgTraceNo;
16+
}
17+
18+
public static StackTraceElement findCaller() {
19+
// 获取堆栈信息
20+
StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
21+
if (null == callStack) {
22+
return null;
23+
}
24+
25+
// 最原始被调用的堆栈信息
26+
StackTraceElement caller = null;
27+
// 日志类名称
28+
String logClassName = Log.class.getName();
29+
// 循环遍历到日志类标识
30+
boolean isEachLogClass = false;
31+
32+
// 遍历堆栈信息,获取出最原始被调用的方法信息
33+
for (StackTraceElement s : callStack) {
34+
// 遍历到日志类
35+
if (logClassName.equals(s.getClassName())) {
36+
isEachLogClass = true;
37+
}
38+
// 下一个非日志类的堆栈,就是最原始被调用的方法
39+
if (isEachLogClass) {
40+
if (!logClassName.equals(s.getClassName())) {
41+
isEachLogClass = false;
42+
caller = s;
43+
break;
44+
}
45+
}
46+
}
47+
48+
return caller;
49+
}
50+
51+
private static Logger logger() {
52+
// 最原始被调用的堆栈对象
53+
StackTraceElement caller = findCaller();
54+
if (null == caller) {
55+
return LogManager.getLogger(Log.class);
56+
}
57+
Logger log = LogManager.getLogger(caller.getClassName() + "." + caller.getMethodName() + "() Line: " + caller.getLineNumber());
58+
59+
return log;
60+
}
61+
62+
public static void debug(String msg, Object o) {
63+
logger().debug(String.format("%s %s %s", msgTraceNo, msg, o.toString()));
64+
}
65+
66+
public static void debug(String msg) {
67+
logger().debug(String.format("%s %s", msgTraceNo, msg));
68+
}
69+
70+
public static void error(String msg, Object o) {
71+
logger().error(String.format("%s %s %s", msgTraceNo, msg, o.toString()));
72+
}
73+
74+
public static void error(String msg) {
75+
logger().error(String.format("%s %s", msgTraceNo, msg));
76+
}
77+
78+
public static void info(String msg, Object o) {
79+
logger().info(String.format("%s %s %s", msgTraceNo, msg, o.toString()));
80+
}
81+
82+
public static void info(String msg) {
83+
logger().info(String.format("%s %s", msgTraceNo, msg));
84+
}
85+
86+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.vison.webmvc.config;
2+
3+
import java.util.UUID;
4+
import javax.servlet.ServletRequestEvent;
5+
import javax.servlet.ServletRequestListener;
6+
import javax.servlet.annotation.WebListener;
7+
8+
/**
9+
* Web application lifecycle listener.
10+
*
11+
* @author vison.cao <visonforcoding@gmail.com>
12+
*/
13+
@WebListener
14+
public class ServletListener implements ServletRequestListener {
15+
16+
@Override
17+
public void requestDestroyed(ServletRequestEvent sre) {
18+
}
19+
20+
@Override
21+
public void requestInitialized(ServletRequestEvent sre) {
22+
App._uniq_req_no = UUID.randomUUID().toString();
23+
Log.setMsgTraceNo(App._uniq_req_no);
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.vison.webmvc.controller;
2+
3+
import com.vison.webmvc.framework.GetMapping;
4+
import com.vison.webmvc.framework.ViewEngine;
5+
import java.io.IOException;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
/**
10+
*
11+
* @author vison.cao <visonforcoding@gmail.com>
12+
*/
13+
public class HelloController {
14+
15+
@GetMapping(path = "/hello")
16+
public String hello() throws IOException {
17+
Map<String, Object> context = new HashMap<>();
18+
context.put("vison", "visonforcoding");
19+
return ViewEngine.render("/hello.pebble", context);
20+
}
21+
22+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.vison.webmvc.controller;
2+
3+
import com.vison.webmvc.entity.User;
4+
import com.vison.webmvc.framework.GetMapping;
5+
import com.vison.webmvc.Response;
6+
import com.vison.webmvc.config.Log;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
/**
11+
*
12+
* @author vison.cao <visonforcoding@gmail.com>
13+
*/
14+
public class UserController {
15+
16+
public UserController() {
17+
}
18+
19+
@GetMapping(path = "/user/profile")
20+
public String profile(HttpServletRequest request, HttpServletResponse response) {
21+
System.out.print(request.getCookies());
22+
return "i am user profile";
23+
}
24+
25+
@GetMapping(path = "/user")
26+
public Response user() {
27+
User user = new User();
28+
user.setEmail("visonforcoding@gmail.com");
29+
user.setName("曹麦穗");
30+
Log.debug("request user", user);
31+
return new Response(0, "获取成功", user);
32+
}
33+
34+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.vison.webmvc.entity;
2+
3+
/**
4+
*
5+
* @author vison.cao <visonforcoding@gmail.com>
6+
*/
7+
public class User {
8+
9+
private int id;
10+
11+
private String name;
12+
13+
private String email;
14+
15+
public int getId() {
16+
return id;
17+
}
18+
19+
public void setId(int id) {
20+
this.id = id;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
31+
public String getEmail() {
32+
return email;
33+
}
34+
35+
public void setEmail(String email) {
36+
this.email = email;
37+
}
38+
39+
}

0 commit comments

Comments
 (0)