抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

1234567891011121314151617181920212223242526272829package com.thok.trading.server.util;import org.springframework.http.HttpEntity;import org.springframework.http.HttpHeaders;import org.springframewo...

通过自定义异常拦截器,讲程序异常,转化为 org.springframework.http.ResponseEntity.ResponseEntity,这个类允许自定义任意的状态码和错误数据。

代码

1
2
3
4
5
6
7
8
9
public void save(BusUser busUser) {
BusUser.checkAccountAndPasswordNotNull(busUser);
if (busUserRepository.findByAccount(busUser.getAccount()).size() != 0) {
throw new ApiException(ErrorType.SAVE_EXIST, "账号已经存在!");
}
// 存储的时候,服务器应该再次进行一次 hash,双重哈希,避免数据库泄漏,密码被人拿去登陆 todo
busUserRepository.save(busUser);
log.info("增加了一个用户:" + busUser.getAccount());
}

GlobalExceptionHandler.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package pri.anarckk.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import pri.anarckk.common.entity.ErrorEntity;
import pri.anarckk.common.exception.ApiException;

/**
* Created by zzanar on 2019/6/1
* 全局异常事件处理类
*/
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
private final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

@ResponseBody
@ExceptionHandler
public ResponseEntity<ErrorEntity> handleException(Exception e) {
if (e instanceof ApiException) {
ApiException apiException = (ApiException) e;
ErrorType code = apiException.getCode();
apiException.error(logger);
return new ResponseEntity<>(new ErrorEntity(code.code(), apiException.getMessage()), code.httpStatus());
} else {
logger.error(e.getMessage(), e);
return new ResponseEntity<>(new ErrorEntity(ErrorType.UN_KNOW.code(), ErrorType.UN_KNOW.message()), ErrorType.UN_KNOW.httpStatus());
}
}
}

环境:Windows10-64位,SpringBoot是2.1.1,内嵌Tomcat 9.0.13, 把日志级别设为Debug后,从控制台看到启动时tomcat报无法找到JNI动态链接库的错误,完整的异常栈信息如下: 12345678910111213141516171819202122232425262728293031323334org.apache.tomcat.jni.Library...

script
1
2
3
4
5
6
7
8
9
10
11
echo "执行容器启动";
docker stop java-web;
if [ $? -ne 0 ]; then
echo "未发现需要删除的容器";
else
docker rm java-web;
fi
docker run -it -d -m 700m --memory-reservation 350m --restart=always --name=java-web -v /home/ppp:/home/ppp --privileged=true --net=host -p xxxx:xxxx centosjdk:v1 java -jar -Duser.timezone=GMT+08 /home/ppp/project-0.0.1.jar;
if [ $? -ne 0 ]; then
echo "启动server服务器失败";
fi

同事使用一个Util进行bean的拷贝,他用的时候是左边拷到右边,我用的时候是右边拷到左边,我TM才发现,原来是同名的BeanUtils,一个是springframework的,一个是apache的。真坑人。

1
2
3
4
5
6
import org.springframework.beans.BeanUtils;
public static void copyProperties(Object source, Object target) throws BeansException

import org.apache.commons.beanutils.BeanUtils;
public static void copyProperties(Object dest, Object orig)
throws IllegalAccessException, InvocationTargetException

后端的坑还是挺多的,慢慢趟吧。

在 swagger 项目里,spring 容器里 requestMapping 会比工程多出来几个 api , 有点好奇,是不是所有的 swagger 工程都会多出来这么几个 api, 还有 /error 是不是 spring 工程自带的一个报错接口 1String[] except = new String[]{"/error", "/co...

123456789101112131415161718package com.thok.myoa.aop.annotation;import com.thok.myoa.constant.AccessType;import com.thok.myoa.constant.TransactionalConstant;import java.lang.annotation.*;/** * Crea...

各类“服务器推”技术原理与实例(Polling/COMET/SSE/WebSocket)

简单来讲

主要是4中方法: 轮询、COMET、SSE (Server-Sent Events)、WebSocket

轮询简单易实现,问题是连接数量多会挤爆服务器

COMET 包含两种: 基于HTTP的长轮询(long-polling)、基于iframe的长连接流(stream)模式

SSE (Server-Sent Events) 是HTML5标准中的一部分。其实现原理类似于我们在上一节中提到的基于iframe的长连接模式。
HTTP响应内容有一种特殊的content-type —— text/event-stream,该响应头标识了响应内容为事件流,客户端不会关闭连接,而是等待服务端不断得发送响应结果。
SSE规范比较简单,主要分为两个部分:浏览器中的EventSource对象,以及服务器端与浏览器端之间的通讯协议。

WebSocket与http协议一样都是基于TCP的。WebSocket其实不仅仅限于“服务器推”了,它是一个全双工的协议,适用于需要进行复杂双向数据通讯的场景。因此也有着更复杂的规范。

来源Spring中获取request的几种方法,及其线程安全性分析 概述在哪里使用1)在Spring的Bean中使用request对象:既包括Controller、Service、Repository等MVC的Bean,也包括了Component等普通的Spring Bean。为了方便说明,后文中Spring中的Bean一律简称为Bean。 2)在非Bean中使用request对象:如普通的...

12345678910111213// 创建一个MapMap<String, Object> infoMap = new HashMap<>();infoMap.put("name", "Zebe");infoMap.put("site", "www.zebe.me");infoMap.p...