Java项目笔记(一)
一、springboot控制台打印sql日志 ---------.mapper为你启动类扫描的mapper路径
logging.level.---------.mapper = debug
二、前端传到后台的json转换成java对象
json:代表的是前端定义的json字符串的变量名
ProductRecord.class:对应的是你想转换成的对象.class
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
ProductRecord productRecord = JSON.parseObject(json, ProductRecord.class);
如果前端传的集合对象后台接收为string类型,解决办法:
public ResponseState createOrder(Orders orders,
@RequestParam(value = "listOrderProduct",required = true) String listOrderProduct){
JSONArray arrayList= JSONArray.parseArray(listOrderProduct); //把 string转成JSONArray对象。
List<OrderProduct> list = JSONObject.parseArray(arrayList.toJSONString(),OrderProduct.class); //转化为你想要的对象list,OrderProduct为目标对象。
return orderService.createOrder(orders,list);
}
三、解决前端传一个对象和一个json,后台接收为null
@RequestMapping里加一个produces="application/json; utf-8"
四、后台接收一个集合抛出异常java.lang.NoSuchMethodException: java.util.List.()
解决办法添加 @RequestParam(value=“list”,required=true) List list
五、for、foreach、Iterator不同数据结构遍历时间测试
基于链表结构的数据,数据增加到万级,for遍历的时间开始成倍数增长
LinkedList linkedList = new LinkedList();
for (int i =0;i<10000;i++){
linkedList.add(i);
}
基于数组格式的数据,数据添加到百万级,三者遍历时间并无明显区别
ArrayList linkedList = new ArrayList();
for (int i =0;i<1000000;i++){
linkedList.add(i);
}
foreach和Iterator相比,两种数据格式遍历时间无明显差别
六、springboot全局日期格式化配置
#jackson相关配置
spring.jackson.date-format = yyyy-MM-dd HH:mm:ss
#时区必须要设置
spring.jackson.time-zone= GMT+8
#ALWAYS的意思是即时属性为null,仍然也会输出这个key,对应yml里面的注释里面的类型
spring.jackson.default-property-inclusion=ALWAYS
七、接口测试时发现一个问题,两个实体类有继承关系,其中一个实体类有list集合。此时接口还需要传一个list集合,如果这两个list集合变量名重名,当你在遍历该参数list集合时无论集合中有多少条数据,发现list.size总是等于2,此时list中的数据被当成一条数据保存到数据库里并且循环两次!解决办法,就是更改变量名,防止重名
八、集合Swagger的时候:报错栈溢出,原因可能是代码和swagger冲突导致了无限递归
(1)、不要在接口中的参数中引用实体类,尽量用json。
(2)、代码逻辑中尽量不要 多层 集合嵌套集合。
解决办法 :注释掉启动类的@EnableSwagger2,让我改代码逻辑还是算了吧
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is com.google.common.util.concurrent.ExecutionError: java.lang.StackOverflowError
java.lang.StackOverflowError
抛出该异常的原因:Jvm线程请求的栈深度大于虚拟机所允许的深度
九、捕获异常
package rongheng.member.controller;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import rongheng.member.entity.ResponseState;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
public class GlobalDefaultExceptionHandler{
/**
* @RequiresPermissions("user:updateSysUser") 捕获无权限异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(UnauthorizedException.class)
@ResponseBody
public Object defaultExceptionHandler(HttpServletRequest req,Exception e){
ResponseState responseState = new ResponseState();
responseState.setState("403");
responseState.setMsg("对不起,暂无权限!");
return responseState;
}
}
十、springboot热部署
#设置开启热部署
spring.devtools.restart.enabled=true
#页面不加载缓存,修改即使生效
spring.freemarker.cache=false
<!--添加热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<build>
<finalName>vip</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<!--热部署配置-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork:如果没有该项配置,整个devtools不会起作用-->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
本文来自博客园,作者:好事的猫,转载请注明原文链接:https://www.cnblogs.com/BeiJiuGuRen/p/15828599.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)