Skip to content

Commit 32d5fd4

Browse files
1.2.0-RELEASE PRE
Former-commit-id: 82dbd01dce2e16422b675dd6e38c0018ee1134ee
1 parent 3c7536f commit 32d5fd4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+590
-451
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@
2424
/nbdist/
2525
/.nb-gradle/
2626

27-
.DS_Store
27+
.DS_Store
28+
29+
*.log
30+
logs

pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@
4747
<project-parent.version>${parent.version}</project-parent.version>
4848
<java.version>1.8</java.version>
4949
<spring-boot.version>2.1.6.RELEASE</spring-boot.version>
50-
<mybatis-plus-boot-starter.version>3.1.1</mybatis-plus-boot-starter.version>
51-
<fastjson.version>1.2.58</fastjson.version>
50+
<mybatis-plus-boot-starter.version>3.1.2</mybatis-plus-boot-starter.version>
51+
<fastjson.version>1.2.59</fastjson.version>
5252
<swagger2.version>2.7.0</swagger2.version>
5353
<commons-lang3.version>3.9</commons-lang3.version>
5454
<velocity.version>1.7</velocity.version>
5555
<mysql.version>5.1.47</mysql.version>
56-
<druid.version>1.1.17</druid.version>
56+
<druid.version>1.1.18</druid.version>
5757
<commons-io.version>2.6</commons-io.version>
5858
<reflections.version>0.9.11</reflections.version>
5959
<hibernate-validator.version>6.0.17.Final</hibernate-validator.version>
@@ -311,6 +311,7 @@
311311
<plugin>
312312
<groupId>org.springframework.boot</groupId>
313313
<artifactId>spring-boot-maven-plugin</artifactId>
314+
<version>${spring-boot.version}</version>
314315
<configuration>
315316
<mainClass>io.geekidea.springbootplus.SpringBootPlusApplication</mainClass>
316317
</configuration>

src/main/java/io/geekidea/springbootplus/SpringBootPlusApplication.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import de.codecentric.boot.admin.server.config.EnableAdminServer;
2020
import io.geekidea.springbootplus.util.PrintApplicationInfo;
21-
import lombok.extern.slf4j.Slf4j;
2221
import org.mybatis.spring.annotation.MapperScan;
2322
import org.springframework.boot.SpringApplication;
2423
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -30,18 +29,17 @@
3029

3130

3231
/**
33-
* Spring Boot Plus 项目启动入口
32+
* spring-boot-plus 项目启动入口
3433
* @author geekidea
3534
* @since 2018-11-08
3635
*/
37-
@Slf4j
3836
@EnableAsync
3937
@EnableScheduling
4038
@EnableTransactionManagement
4139
@EnableConfigurationProperties
4240
@EnableAdminServer
43-
@SpringBootApplication
4441
@MapperScan({"io.geekidea.springbootplus.**.mapper"})
42+
@SpringBootApplication
4543
public class SpringBootPlusApplication {
4644

4745
public static void main(String[] args) {

src/main/java/io/geekidea/springbootplus/common/aop/LogAop.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
import io.geekidea.springbootplus.util.AnsiUtil;
2424
import io.geekidea.springbootplus.util.DateUtil;
2525
import io.geekidea.springbootplus.util.IpUtil;
26+
import lombok.Data;
2627
import lombok.extern.slf4j.Slf4j;
2728
import org.aspectj.lang.ProceedingJoinPoint;
2829
import org.aspectj.lang.Signature;
2930
import org.aspectj.lang.annotation.Around;
3031
import org.aspectj.lang.annotation.Aspect;
3132
import org.aspectj.lang.reflect.MethodSignature;
3233
import org.fusesource.jansi.Ansi;
33-
import org.springframework.stereotype.Component;
3434
import org.springframework.web.bind.annotation.RequestBody;
3535
import org.springframework.web.context.request.RequestContextHolder;
3636
import org.springframework.web.context.request.ServletRequestAttributes;
@@ -50,10 +50,10 @@
5050
*
5151
* @author geekidea
5252
* @date 2018-11-08
53-
// */
54-
@Aspect
55-
@Component
53+
*/
54+
@Data
5655
@Slf4j
56+
@Aspect
5757
public class LogAop {
5858

5959
/**
@@ -77,9 +77,19 @@ public class LogAop {
7777
**/
7878
private static final String POST = "POST";
7979

80+
/**
81+
* 请求日志是否格式化输出
82+
*/
83+
private boolean requestLogFormat;
84+
85+
/**
86+
* 响应日期是否格式化输出
87+
*/
88+
private boolean responseLogFormat;
8089

8190
@Around(POINTCUT)
8291
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
92+
8393
// 获取请求相关信息
8494
try {
8595
// 获取当前的HttpServletRequest对象
@@ -125,11 +135,15 @@ public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
125135

126136
String requestInfo = null;
127137
try {
128-
requestInfo = JSON.toJSONString(map);
138+
if (requestLogFormat){
139+
requestInfo = "\n" + JSON.toJSONString(map,true);
140+
}else{
141+
requestInfo = JSON.toJSONString(map);
142+
}
129143
} catch (Exception e) {
130144

131145
}
132-
log.info(AnsiUtil.getAnsi(Ansi.Color.GREEN,"requestInfo:"+requestInfo));
146+
log.info(AnsiUtil.getAnsi(Ansi.Color.GREEN,"requestInfo:" + requestInfo));
133147

134148
} catch (Exception e) {
135149
e.printStackTrace();
@@ -139,16 +153,22 @@ public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
139153

140154
// 执行目标方法,获得返回值
141155
Object result = joinPoint.proceed();
142-
Object responseResult = null;
143156
try{
144-
if (responseResult != null && responseResult instanceof ApiResult){
145-
ApiResult apiResult = (ApiResult) responseResult;
157+
if (result != null && result instanceof ApiResult){
158+
ApiResult apiResult = (ApiResult) result;
146159
int code = apiResult.getCode();
147-
if (code != ApiCode.SUCCESS.getCode()){
148-
log.error(AnsiUtil.getAnsi(Ansi.Color.RED,"responseResult:"+JSON.toJSONString(responseResult)));
160+
String responseResultInfo = "responseResult:";
161+
if (responseLogFormat){
162+
responseResultInfo += "\n" + JSON.toJSONString(apiResult,true);
149163
}else{
150-
log.info(AnsiUtil.getAnsi(Ansi.Color.BLUE,"responseResult:"+JSON.toJSONString(responseResult)));
164+
responseResultInfo += JSON.toJSONString(apiResult);
151165
}
166+
if (code == ApiCode.SUCCESS.getCode()){
167+
log.info(AnsiUtil.getAnsi(Ansi.Color.BLUE,responseResultInfo));
168+
}else{
169+
log.error(AnsiUtil.getAnsi(Ansi.Color.RED,responseResultInfo));
170+
}
171+
152172
}
153173
}catch (Exception e){
154174
log.error("处理响应结果异常",e);

src/main/java/io/geekidea/springbootplus/common/api/ApiCode.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public enum ApiCode {
4242

4343
PARAMETER_PARSE_EXCEPTION(5002,"请求参数解析异常"),
4444

45-
HTTP_MEDIA_TYPE_EXCEPTION(5003,"HTTP Media 类型异常")
45+
HTTP_MEDIA_TYPE_EXCEPTION(5003,"HTTP Media 类型异常"),
46+
47+
SYSTEM_LOGIN_EXCEPTION(5005,"系统登录异常")
4648

4749
;
4850

@@ -72,9 +74,4 @@ public String getMsg() {
7274
return msg;
7375
}
7476

75-
public static void main(String[] args) {
76-
ApiCode apiCode = ApiCode.getApiCode(200);
77-
System.out.println("apiCode = " + apiCode);
78-
79-
}
8077
}

src/main/java/io/geekidea/springbootplus/common/exception/GlobalExceptionHandler.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,14 @@ public ApiResult handleMethodArgumentNotValidExceptionHandler(MethodArgumentNotV
6868
* @param exception
6969
* @return
7070
*/
71-
// @ExceptionHandler(value = SysLoginException.class)
72-
// @ResponseStatus(HttpStatus.OK)
73-
// public ApiResult sysLoginExceptionHandler(SysLoginException exception) {
74-
// log.warn("系统登录异常:" + exception.getMessage());
75-
// return new ApiResult(500,exception.getMessage());
76-
// }
77-
78-
79-
/**
80-
* 默认的异常处理
81-
* @param exception
82-
* @return
83-
*/
84-
@ExceptionHandler(value = Exception.class)
71+
@ExceptionHandler(value = SysLoginException.class)
8572
@ResponseStatus(HttpStatus.OK)
86-
public ApiResult exceptionHandler(Exception exception) {
87-
log.error("exception:",exception);
88-
return ApiResult.fail(ApiCode.PARAMETER_EXCEPTION, ApiCode.SYSTEM_EXCEPTION);
73+
public ApiResult sysLoginExceptionHandler(SysLoginException exception) {
74+
log.warn("系统登录异常:" + exception.getMessage());
75+
return ApiResult.fail(ApiCode.SYSTEM_LOGIN_EXCEPTION);
8976
}
9077

78+
9179
/**
9280
* HTTP解析请求参数异常
9381
* @param exception
@@ -101,7 +89,6 @@ public ApiResult httpMessageNotReadableException(HttpMessageNotReadableException
10189
}
10290

10391
/**
104-
* TODO
10592
* HTTP
10693
* @param exception
10794
* @return
@@ -112,4 +99,17 @@ public ApiResult httpMediaTypeException(HttpMediaTypeException exception) {
11299
log.error("httpMediaTypeException:",exception);
113100
return ApiResult.fail(ApiCode.PARAMETER_EXCEPTION, ApiCode.HTTP_MEDIA_TYPE_EXCEPTION);
114101
}
102+
103+
/**
104+
* 默认的异常处理
105+
* @param exception
106+
* @return
107+
*/
108+
@ExceptionHandler(value = Exception.class)
109+
@ResponseStatus(HttpStatus.OK)
110+
public ApiResult exceptionHandler(Exception exception) {
111+
log.error("exception:",exception);
112+
return ApiResult.fail(ApiCode.SYSTEM_EXCEPTION);
113+
}
114+
115115
}

src/main/java/io/geekidea/springbootplus/common/enums/OrderEnum.java renamed to src/main/java/io/geekidea/springbootplus/common/exception/SysLoginException.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,34 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.geekidea.springbootplus.common.enums;
17+
package io.geekidea.springbootplus.common.exception;
18+
19+
import org.apache.commons.lang3.StringUtils;
1820

1921
/**
22+
* 系统登录异常
2023
* @author geekidea
21-
* @date 2018-11-08
24+
* @date 2019-08-04
2225
*/
23-
public enum OrderEnum {
24-
ASC,DESC;
26+
public class SysLoginException extends RuntimeException{
27+
private Integer code;
28+
public SysLoginException() {
29+
}
30+
31+
public SysLoginException(String message) {
32+
super(message);
33+
}
34+
35+
public SysLoginException(Integer code , String message){
36+
this(StringUtils.isBlank(message)?String.valueOf(code):message);
37+
this.code=code;
38+
}
39+
40+
public Integer getCode() {
41+
return code;
42+
}
43+
44+
public void setCode(Integer code) {
45+
this.code = code;
46+
}
2547
}

src/main/java/io/geekidea/springbootplus/common/service/impl/BaseServiceImpl.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717
package io.geekidea.springbootplus.common.service.impl;
1818

1919
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
20+
import com.baomidou.mybatisplus.core.metadata.OrderItem;
2021
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
2122
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
22-
import io.geekidea.springbootplus.common.enums.OrderEnum;
2323
import io.geekidea.springbootplus.common.service.BaseService;
24+
import io.geekidea.springbootplus.common.web.param.OrderQueryParam;
2425
import io.geekidea.springbootplus.common.web.param.QueryParam;
26+
import org.apache.commons.collections.CollectionUtils;
27+
28+
import java.util.Arrays;
29+
import java.util.List;
2530

2631
/**
2732
* @author geekidea
@@ -30,20 +35,29 @@
3035
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {
3136

3237
protected Page setPageParam(QueryParam queryParam) {
33-
return setPageParam(queryParam,null,null);
38+
return setPageParam(queryParam,null);
3439
}
3540

36-
protected Page setPageParam(QueryParam queryParam, String defaultOrderField, OrderEnum orderEnum) {
41+
protected Page setPageParam(QueryParam queryParam, OrderItem defaultOrder) {
3742
Page page = new Page();
43+
// 设置当前页码
3844
page.setCurrent(queryParam.getCurrent());
45+
// 设置页大小
3946
page.setSize(queryParam.getSize());
40-
page.setAsc(queryParam.getAscs());
41-
page.setDesc(queryParam.getDescs());
42-
43-
if (orderEnum == OrderEnum.DESC){
44-
page.setDesc(defaultOrderField);
45-
}else {
46-
page.setAsc(defaultOrderField);
47+
/**
48+
* 如果是queryParam是OrderQueryParam,并且不为空,则使用前端排序
49+
* 否则使用默认排序
50+
*/
51+
if (queryParam instanceof OrderQueryParam){
52+
OrderQueryParam orderQueryParam = (OrderQueryParam) queryParam;
53+
List<OrderItem> orderItems = orderQueryParam.getOrders();
54+
if (CollectionUtils.isEmpty(orderItems)){
55+
page.setOrders(Arrays.asList(defaultOrder));
56+
}else{
57+
page.setOrders(orderItems);
58+
}
59+
}else{
60+
page.setOrders(Arrays.asList(defaultOrder));
4761
}
4862

4963
return page;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright 2019-2029 geekidea(https://github.com/geekidea)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.geekidea.springbootplus.common.web.param;
18+
19+
import com.baomidou.mybatisplus.core.metadata.OrderItem;
20+
import io.swagger.annotations.ApiModel;
21+
import io.swagger.annotations.ApiModelProperty;
22+
import lombok.Data;
23+
import lombok.EqualsAndHashCode;
24+
import org.apache.commons.collections.CollectionUtils;
25+
26+
import java.util.Arrays;
27+
import java.util.List;
28+
29+
/**
30+
* 可排序查询参数对象
31+
* @author geekidea
32+
* @since 2019-08-04
33+
*/
34+
@Data
35+
@EqualsAndHashCode(callSuper = true)
36+
@ApiModel("可排序查询参数对象")
37+
public abstract class OrderQueryParam extends QueryParam{
38+
private static final long serialVersionUID = 57714391204790143L;
39+
40+
@ApiModelProperty(value = "排序")
41+
private List<OrderItem> orders;
42+
43+
public void defaultOrder(OrderItem orderItem){
44+
this.defaultOrders(Arrays.asList(orderItem));
45+
}
46+
47+
public void defaultOrders(List<OrderItem> orderItems){
48+
if (CollectionUtils.isEmpty(orderItems)){
49+
return;
50+
}
51+
this.orders = orderItems;
52+
}
53+
54+
}

0 commit comments

Comments
 (0)