Skip to content

Commit

Permalink
首次提交
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyenews committed Apr 12, 2021
1 parent 372d4b3 commit 386f49d
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 36 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright 2019, Yuye
Copyright 2021, Yuye

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Magician的官方Web组件

## 项目简介

Magician-Web 是 Magician的官方Web组件,实现了以Controller的方式来进行参数的接口和响应
Magician-Web 是 Magician的官方Web组件,实现了以Controller的方式来进行参数的接收和响应

## 安装步骤

Expand Down Expand Up @@ -71,6 +71,11 @@ Magician.createHttpServer().httpHandler("/", req -> {

}).bind(8080).start();
```
## 除此之外还实现了以下功能

1. 自定义拦截器
2. 注解式参数校验
3. 自带JWT管理类

## 开发资源
- 开发文档: [http://magician-io.com/docs/index.html](http://magician-io.com/docs/web/index.html)
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.yuyenews</groupId>
<artifactId>Magician-Web</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>

<dependencies>
<dependency>
Expand All @@ -18,7 +18,7 @@
<dependency>
<groupId>com.github.yuyenews</groupId>
<artifactId>Magician</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<optional>true</optional>
</dependency>

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/magician/web/MagicianWeb.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public void request(MagicianRequest request){
logger.error("执行MagicianWeb出现异常", e);

request.getResponse()
.setResponseHeader("content-type", "application/json;charset="+ MagicianWebConstant.ENCODING)
.sendText(200, MesUtil.getMes(500, e.getMessage()));
.sendJson(200, MesUtil.getMes(500, e.getMessage()));
}
}
}
25 changes: 15 additions & 10 deletions src/main/java/com/magician/web/core/util/JwtManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,28 @@ public class JwtManager {
*/
private int calendarInterval = 86400;

private static JwtManager jwtManager = new JwtManager();
private JwtManager(){}

public static JwtManager getJwtManager() {
return jwtManager;
}

public void setSecret(String secret) {
public JwtManager setSecret(String secret) {
this.secret = secret;
return this;
}

public void setCalendarField(int calendarField) {
public JwtManager setCalendarField(int calendarField) {
this.calendarField = calendarField;
return this;
}

public void setCalendarInterval(int calendarInterval) {
public JwtManager setCalendarInterval(int calendarInterval) {
this.calendarInterval = calendarInterval;
return this;
}

/**
* 创建一个JWT管理类
* @return
*/
public static JwtManager builder() {
JwtManager jwtManager = new JwtManager();
return jwtManager;
}

/**
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/com/magician/web/core/util/ParamsCheckUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public class ParamsCheckUtil {
/**
* 校验参数
* @param params 参数集合
* @param method 要执行的方法
* @param url 路径
* @return 校验结果
*/
public static String checkParam(Object[] params, Method method){
public static String checkParam(Object[] params, String url){
if(params == null){
return null;
}
Expand All @@ -38,7 +38,7 @@ public static String checkParam(Object[] params, Method method){
if(requestClass.equals(cls)){
continue;
}
String result = checkParam(cls,obj,method);
String result = checkParam(cls, obj, url);
if(result != null){
return result;
}
Expand All @@ -52,7 +52,7 @@ public static String checkParam(Object[] params, Method method){
* @param obj 参数对象
* @return 校验结果
*/
private static String checkParam(Class<?> cls, Object obj, Method method) {
private static String checkParam(Class<?> cls, Object obj, String url) {
try {
Field[] fields = cls.getDeclaredFields();
for(Field field : fields){
Expand All @@ -65,7 +65,7 @@ private static String checkParam(Class<?> cls, Object obj, Method method) {

/* 判断此注解是否生效与当前api,如果不生效那就直接跳入下一次循环 */
String[] apis = verification.apis();
if(!isThisApi(apis,method)){
if(!isThisApi(apis, url)){
continue;
}

Expand Down Expand Up @@ -137,21 +137,17 @@ private static boolean notNull(Verification verification, Object val){

/**
* 校验apis列表里是否包含此api
* @param method 此api
* @param url 此api
* @param apis api列表
* @return
*/
private static boolean isThisApi(String[] apis, Method method){
private static boolean isThisApi(String[] apis, String url){
if(apis == null || apis.length < 1){
return true;
}
Route route = method.getAnnotation(Route.class);
if(route == null){
return false;
}

for(String api : apis){
if(MatchUtil.isMatch(api,route.value())){
if(MatchUtil.isMatch(api.toUpperCase(), url.toUpperCase())){
return true;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/magician/web/execute/ApiExecute.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void execute(MagicianRequest request) throws Exception {
Object[] params = BuildParams.builder(method, request);

/* 校验传参 */
String checkResult = ParamsCheckUtil.checkParam(params,method);
String checkResult = ParamsCheckUtil.checkParam(params, url);
if(checkResult != null){
sendText(request, checkResult);
return;
Expand Down Expand Up @@ -77,9 +77,10 @@ public static void execute(MagicianRequest request) throws Exception {

/* 如果返回的是个流,就直接响应流 */
if(result instanceof InputStream){
request.getResponse()
.setResponseHeader("content-type", "application/octet-stream")
.sendResponseBody((InputStream) result);
request.getResponse().sendStream((InputStream) result);
return;
} else if(result instanceof byte[]){
request.getResponse().sendStream((byte[]) result);
return;
}

Expand All @@ -94,8 +95,7 @@ public static void execute(MagicianRequest request) throws Exception {
*/
private static void sendText(MagicianRequest request, String text){
request.getResponse()
.setResponseHeader("content-type", "application/json;charset="+ MagicianWebConstant.ENCODING)
.sendText(200, text);
.sendJson(200, text);
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/magician/web/load/ApiLoad.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public static void load() throws Exception {
/* 扫描包下的类 */
scanClassList = ScanUtil.scanClassList(MagicianWebConfig.getScanPath());

/* 筛选并创建拦截器和接口 */
/* 筛选并创建拦截器 */
Map<String, InterceptorModel> interceptorModelMap = scanInterceptor(scanClassList);

/* 筛选并创建接口 */
scanRoute(scanClassList);

/* 将拦截器跟route比对后,分类存放 */
Expand Down Expand Up @@ -97,11 +99,16 @@ private static void scanRoute(Set<String> scanClassList) throws Exception {
throw new Exception("["+method+"]方法没有返回类型");
}

String path = getRoute(methodRoute, clsRoute);
if(routeModelMap.containsKey(path)){
throw new Exception("["+path+"]Route有重复");
}

RouteModel routeModel = new RouteModel();
routeModel.setCls(cls);
routeModel.setMethod(method);
routeModel.setReqMethods(methodRoute.requestMethod());
routeModel.setRoute(getRoute(methodRoute, clsRoute));
routeModel.setRoute(path);
routeModel.setObject(cls.getDeclaredConstructor().newInstance());

routeModelMap.put(routeModel.getRoute(), routeModel);
Expand Down

0 comments on commit 386f49d

Please sign in to comment.