1.http请求
请求https报错:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) ~[na:1.8.0_111-internal]
说明SSL证书过期
解决:可以请求忽略SSL证书
http请求时,响应数据中如果包含转义字符时的处理:
public static String doGet(String url, int connTimeout, int readTimeout, String charset, Map<String, String> headerMap) {
......
GetMethod method = new GetMethod(url);
if (statusCode == HttpStatus.SC_OK) {
//将响应body进行json反序列化
String res = getRequestBodyJson(method.getResponseBodyAsStream());
}
......
}
private static String getRequestBodyJson(InputStream inputStream) throws IOException {
Reader input = new InputStreamReader(inputStream);
Writer output = new StringWriter();
char[] buffer = new char[1024 * 4];
int n;
while(-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toString();
}
2.事务
事务(对数据库的操作):
@Transactional可以直接放在service上,也可在某个方法上
@Transactional
public void method(){
method1();
method2();
}
private void method1(){ 事务1 }
private void method2(){ 事务2 }
这样放,则事务1和事务2是属于一个事务的,报错会事务回滚,如果method方法不加,在1和2上加,则不是同一个
这个报错指的是数据库增删改失败,而不是人为的报错,需要把方法1和2的人为报错都写在method方法中,在最前面判断。
3.内部feign调用spring security安全框架的登录接口(/oauth/token)
@FeignClient(name = "authentication", fallback = AuthenticationProviderFallback.class)
public interface AuthenticationProvider {
@PostMapping(value = "/oauth/token", headers = {"Authorization=xxxx"})
Result oauthTokenLogin(@RequestParam(value = "mobile") String mobile, @RequestParam(value = "type") String type,
@RequestParam(value = "grant_type") String grantType, @RequestParam(value = "scope") String scope);
}
4.@RestController和@Controller
@RestController
相当于
@Controller
接口方法上 @ResponseBody 返回json格式数据
如果该接口方法需返回跳转到某页面,则该类不能用 @RestController, 只用 @Controller
public String aa(){
return "redirect:/";
}
5.java调用百度api
百度地图api:
AK:
根据地理位置获取经纬度:
http://api.map.baidu.com/geocoding/v3/?address=xxx&output=json&ak=xxx
获取所在镇:
6.测试系统框架对异常的处理
@GetMapping("/numCount")
public WebResponse numCount(){
ArrayIndexOutOfBoundsException exception = new ArrayIndexOutOfBoundsException();
throw exception;
}
//调用该接口,会报500
三种处理异常:
try/catch、throw抛异常、throws抛自定义异常
SpringMVC框架对异常的处理------>SpringBoot异常处理:
7.分页插件xml中的sql需注意
有些复杂sql,用分页插件时order by会消失,需加注释/* keep orderBy */
8.lombok工具
引依赖lombok
@Data
Mybatis-generator插件:整合lombok插件,即生成实体类时给该实体类上加@Data
自写一个LombokPlugin.java
Build -- Recompile编译成LombokPlugin.class
E:\xxx\target\classes\org\mybatis\generator\plugins:
LombokPlugin.class 复制到
E:\repository\org\mybatis\generator\mybatis-generator-core\1.3.5:
mybatis-generator-core-1.3.5.jar中(打开压缩包,直接粘贴进去)
9.generator插件
pom.xml:引入Mybatis-generator插件
编写:generatorConfig.xml
右边Maven:Plugins:mybatis-generator
10.提供的sdk,私服上没有
可以先本地,放在本地仓库的相应目录中,pom中会标红,但是启动是可以的
或者
IDEA将本地(外来的)jar包导入到maven项目中_idea引入本地jar包到maven项目-CSDN博客
把别人的jar包导入我们maven项目中,先使用
11.自定义注解
@interface
可以把类变成一个注解,一般都是:自定义注解 + (aop或interceptor) + 用于类或方法上
比如:接口权限认证(接口指定角色访问),其中一个实现方案:自定义注解 + aop
1.自定义注解Authentication
@Target({ElementType.TYPE, ElementType.METHOD}) //可以作用在类、方法上
public @interface Authentication {
int[] role() default {};
}
2.写个aop:
@Aspect
public class AuthenticationAspect{
类或方法加了这个注解后,想执行的操作
}
3.用于类或方法上
@Authentication(role = {41}) //如果是直接(),就取默认值{}
public Result testMethod(){
}
12.insertSelective
tk.mybatis Mapper:
如果代码中需要用到插入数据后的id,则需要用insertSelective方法,则实体类中的id上需加@GeneratedValue(generator = "JDBC"),即用代码自动生成id
mybatisplus BaseMapper:
insert方法就会返回插入数据后的id
13.后台防止重复提交
多线程:
乐观锁(数据库表中增加一个版本号字段:version)、悲观锁、闭锁
redis
分布式锁
SpringBoot如何防止重复提交2--use_springboot防止重复提交-CSDN博客
14.上传文件大小限制
java配置中:
spring:
servlet:
multipart:
max-file-size: 100MB
max-request-size: 1000MB
nginx配置中:
http {
client_max_body_size 100M;
}
15.接口返回数据量超大,可以分组发给前端,然后前端分组渲染
后端返回所有数据但是是分组的,前端每隔几s去取返回的第几组数据,从2到groupSize+1
16.输入框输入”[]”报网络错误问题
解决方案:启动类xxApplication.java中:
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector ->connector.setProperty("relaxedQueryChars", "|{}[]\\"));
return factory;
}
17.根据word模板动态生成word文档
java 根据模板导出word文档(poi-tl)-CSDN博客
poi-tl
是一个基于Apache POI的Word模板引擎,也是一个免费开源的Java类库
18.poi导出excel
如果报错:java.lang.IllegalArgumentException: The maximum length of cell contents (text) is 32,767 characters
是因为插入的文本内容超过限制最大值
解决:
// 导出数据前调用,通过反射去修改 SpreadsheetVersion 的 _maxTextLength 变量;修改的原因:因为xx太长,导致报错:The maximum length of cell contents (text) is 32,767 characters
ExcelStyleUtil.initCellMaxTextLength();
/**
* 初始化 cell 内容长度
* cell 原本内容长度限制 32767 现修改为Integer.MAX_VALUE
*/
public static void initCellMaxTextLength() {
SpreadsheetVersion excel2007 = SpreadsheetVersion.EXCEL2007;
if (excel2007.getMaxTextLength() < Integer.MAX_VALUE) {
Field field;
try {
field = excel2007.getClass().getDeclaredField("_maxTextLength");
field.setAccessible(true);
field.set(excel2007,Integer.MAX_VALUE);
} catch (Exception e) {
log.error("initCellMaxTextLength 初始化cell内容长度异常:", e);
}
}
}