Skip to content

Commit

Permalink
重新封装Result模块
Browse files Browse the repository at this point in the history
  • Loading branch information
fengwenyi committed Sep 2, 2018
1 parent 4836ed8 commit 1450db8
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 214 deletions.
38 changes: 12 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ JavaLib,是一个Java开发基础工具类库,对项目开发中常用的工

* 消息引擎

* aop模块


### 目录结构

Expand All @@ -60,6 +62,8 @@ JavaLib
│ │ └──com
│ │ └── fengwenyi
│ │ └── javalib
│ │ ├── aop
│ │ │ └── LBaseWebLogAspect
│ │ ├── handler
│ │ │ ├── Handler // handler
│ │ │ └── HandlerRegister // handler注册
Expand All @@ -70,6 +74,9 @@ JavaLib
│ │ ├── messageengine
│ │ │ └── CommonMessage // 通用消息组件
│ │ ├── result
│ │ │ ├── DefaultResult
│ │ │ ├── DefaultReturnCode
│ │ │ ├── ResultResponseUtil
│ │ │ ├── IReturnCode // 返回码接口
│ │ │ └── Result // Result
│ │ └── util
Expand Down Expand Up @@ -112,32 +119,11 @@ JavaLib
| --- | ---|
| ArtifactId| JavaLib|

**`中央仓库`**

```xml
<dependency>
<groupId>com.fengwenyi</groupId>
<artifactId>JavaLib</artifactId>
<version>0.0.9</version>
</dependency>
```
**`仓库`**

**`jitpack`**

```xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependency>
<groupId>com.github.fengwenyi</groupId>
<artifactId>JavaLib</artifactId>
<version>1.0.1-RELEASE</version>
</dependency>
```
[中央仓库](https://search.maven.org/search?q=g:com.fengwenyi%20AND%20a:JavaLib&core=gav)
|
[JitPack](https://jitpack.io/#fengwenyi/JavaLib)

### API

Expand All @@ -158,7 +144,7 @@ JavaLib
### Licensed

```
Copyright 2017-2018 Wenyi Feng(xfsy_2015@163.com)
Copyright 2017-2018 Wenyi Feng(xfsy2014@gmail.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.fengwenyi</groupId>
<artifactId>JavaLib</artifactId>
<version>1.0.2-RELEASE</version>
<version>1.0.2.RELEASE</version>

<parent>
<groupId>org.sonatype.oss</groupId>
Expand Down Expand Up @@ -44,7 +44,6 @@
<slf4j.version>1.7.25</slf4j.version>
</properties>


<build>
<plugins>
<plugin>
Expand Down
43 changes: 21 additions & 22 deletions src/main/java/com/fengwenyi/javalib/aop/DefaultWebLogAspect.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.fengwenyi.javalib.aop;

import org.aspectj.lang.JoinPoint;

/**
* 实现示例
*
* @author Wenyi Feng
* @since 2018-08-30
*/
public class DefaultWebLogAspect extends LBaseWebLogAspect {

@Override
public void doBefor(JoinPoint joinPoint) throws Exception {
//super.doBefor(joinPoint, request);
}

@Override
public void doAfterReturn(Object ret) throws Exception {
//super.doAfterReturn(ret, request);
}
}
//package com.fengwenyi.javalib.aop;
//
//import org.aspectj.lang.JoinPoint;
//
///**
// * 实现示例
// * @author Wenyi Feng
// * @since 2018-08-30
// */
//public class DefaultWebLogAspect extends LBaseWebLogAspect {
//
// @Override
// public void doBefor(JoinPoint joinPoint) throws Exception {
// //super.doBefor(joinPoint, request);
// }
//
// @Override
// public void doAfterReturn(Object ret) throws Exception {
// //super.doAfterReturn(ret, request);
// }
//}
52 changes: 33 additions & 19 deletions src/main/java/com/fengwenyi/javalib/aop/LBaseWebLogAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
import java.util.Set;

/**
* web请求aop切面
* <p>
* web请求aop切面
* </p>
*
* 通过日志记录web请求以及方法请求处理的一些细节
* <p>
* 通过日志记录web请求以及方法请求处理的一些细节
* </p>
*
* @author Wenyi Feng
* @since 2018-08-30
Expand All @@ -24,25 +28,33 @@ public abstract class LBaseWebLogAspect {

/**
* 继承类可以直接使用
*
* 关于log变量的定义问题及更多细节,可以参考doc中的数码
*/
//protected final Logger log = LoggerFactory.getLogger(this.getClass());
protected static final Logger log = LoggerFactory.getLogger(LBaseWebLogAspect.class);

// 记录方法处理消耗的时间(纳秒)
private long startTime = 0L, endTime = 0L, time = 0L;
/** 记录方法处理消耗的时间(纳秒) */
private long startTime = 0L;

// 需要去实现(进入方法前)
/**
* 需要去实现(进入方法前)
* @param joinPoint 切点
* @throws Exception 异常
*/
public abstract void doBefor(JoinPoint joinPoint) throws Exception;

// 需要实现(返回后)
/**
* 需要实现(返回后)
* @param ret 返回的数据
* @throws Exception 异常
*/
public abstract void doAfterReturn(Object ret) throws Exception;

/**
*
* @param joinPoint
* @param request 为了完全不侵入你的代码,所以我能帮你拿到request,但是,你很容易,所以,我选择交给你获取
* @throws Exception
* 进入方法之前
* @param joinPoint 切点
* @param request 为了完全不侵入你的代码,所以我很难帮你拿到request,但是,你很容易,所以,我选择交给你获取
* @throws Exception 异常
*/
public void doBefore(JoinPoint joinPoint, HttpServletRequest request) throws Exception {

Expand Down Expand Up @@ -72,7 +84,7 @@ public void doBefore(JoinPoint joinPoint, HttpServletRequest request) throws Exc

//------------------------------------------------------------

log.info("∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧∧");
log.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");

log.info("request ip:{}", ip);
log.info("request url:{}", url);
Expand All @@ -84,21 +96,23 @@ public void doBefore(JoinPoint joinPoint, HttpServletRequest request) throws Exc
}

/**
*
* @param ret
* @param request
* @throws Exception
* 结束方法访问
* @param ret 返回数据
* @param request 切点
* @throws Exception 异常
*/
public void doAfterReturning(Object ret, HttpServletRequest request) throws Exception {

//

log.info("return data:{}", ret);
endTime = System.nanoTime();
time = endTime - startTime;
long endTime = System.nanoTime();

/** 单位:纳秒 */
long time = endTime - startTime;
log.info("time spead:{} (ns)", time);

log.info("∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨∨");
log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

}
}
17 changes: 16 additions & 1 deletion src/main/java/com/fengwenyi/javalib/result/DefaultResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,23 @@
* @since 2018-08-28
*/
@Data
public class DefaultResult<T> extends Result<T> {
public class DefaultResult extends Result {

/** 返回结果属性(成功[true]/失败[false])标志 */
private Boolean success;

/**
* DefaultResult toString
* @return DefaultResult extends Result
* 如果没有这个方法,你可能打印不出想要的数据
*/
@Override
public String toString() {
return "DefaultResult{" +
"code=" + this.getCode() +
" success=" + this.getSuccess() +
" msg=" + this.getMsg() +
" data=" + this.getData() +
"}";
}
}
27 changes: 17 additions & 10 deletions src/main/java/com/fengwenyi/javalib/result/DefaultReturnCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,42 @@
import lombok.Getter;

/**
* 默认返回码
* <p>
* 默认返回码
* </p>
*
* 这是一个使用IReturnCode写的示例,也是提供默认使用的
* <p>
* 这是一个使用IReturnCode写的示例,也是提供默认使用的
* </p>
*
* @author Wenyi Feng
* @since 2018-08-27
*/
@Getter
public enum DefaultReturnCode implements IReturnCode {

//-1
/** 初始化 */
ERROR_INIT(-1, "init..."),

// 500
//ERROR_500(500, "(Error)程序出错"),
/** 系统内部错误 */
ERROR_500(500, "(Error)程序出错"),

/** 成功 */
SUCCESS(0, "Success")
;

/** 返回码 */
private Integer code;
/** 描述 */
private String msg;

/**
* 构造方法
* @param code 返回码
* @param msg 描述
*/
DefaultReturnCode(Integer code, String msg) {
this.code = code;
this.msg = msg;
}

public void setReturnCode(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/fengwenyi/javalib/result/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Data
// 无参数构造方法
@NoArgsConstructor
public class Result<T> {
public class Result {

/** 返回码 */
private Integer code;
Expand All @@ -19,8 +19,8 @@ public class Result<T> {
private String msg;

/** 数据 */
// private Object data;
private T data;
private Object data;
//private T data;

/**
* 设置返回码
Expand All @@ -35,7 +35,7 @@ public void setResult(IReturnCode iReturnCode) {
* @param iReturnCode 自定义枚举类,需要实现或继承IReturnCode
* @param data 自定义数据对象
*/
public void setResult(IReturnCode iReturnCode, T data) {
public void setResult(IReturnCode iReturnCode, Object data) {
common(iReturnCode);
this.data = data;
}
Expand Down
Loading

0 comments on commit 1450db8

Please sign in to comment.