java开发问题统计

1.maven添加依赖时,没有添加版本

在进行maven导包时,报错如下:

mysql:mysql-connector-java:jar:unknown was not found in http://maven.aliyun.com/nexus/content/groups/public during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of nexus-aliyun has elapsed or updates are forced

解决办法:在mysql的依赖中,写入具体版本号,依赖如下所示,在第四行代码中,添加<version>8.0.28</version>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
    <scope>runtime</scope>
</dependency>

2.修改application.yml文件名称,项目报错

问题原因:在springcloud开发时,我修改了application.yml文件的名称,改为了application-dev.yml,结果服务启动时报错数据源不对,服务起不来,问题代码如下:

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class
Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

解决办法:

把application-dev.yml,改回application.yml,服务正常启动。

3.mysql插入中文数据变成问号的问题

问题原因:mysql插入中文数据变成问号的问题。

分析:

1.application.yml关于数据库字符编码是直接用以前项目的,没有动过,此问题第一次出现,排除配置文件导致中文乱码。

2.数据库的字符编码导致乱码,排除

解决办法:暂无

4. java: 非法字符: '\ufeff'

问题原因:java: 非法字符: '\ufeff'

分析:

1.出现这样的问题来源于这个BOM,一般在编写时候会给你默认添加这样的一个BOM头,是隐藏起来的,编译时候会给出现编码混乱问题。

 解决办法:

在设置-搜索编码-选择文件编码-选择不含BOM-应用保存

5.端口占用,杀死占用端口

问题原因:在启动服务时发现8080端口被占用。

解决办法:

1.使用 命令:lsof -i tcp:8080  这个命令可以查看被占用的端口的PID是多少,我的是18029。

2.输入 kill 18029  ,杀掉进程,再次查看端口,没有占用了,重启服务。

6.数据出现两条一样数据,查询时报错

问题原因:在以前测试的时候,网数据库插入了两条一样的数据,导致idea查出两条数据,但是我只想要一条数据,所以可以把数据库重复的数据删除,报错如下:

Expected one result (or null) to be returned by selectOne(), but found: 2

解决方法:

1.修改数据库数据,保证数据的唯一性

2.修改代码,把接口返回的数据设为List,这样就可以返回多条数据。

7.mybatis-plus分页插件版本低报错

问题原因:想使用mybatis-plus的分页插件,但是MybatisPlusIntercepto报错,查了其他资料,mybatis-plus-boot-starter 3.4之后的分页才会使用这个类,所以是由版本低导致报错。

解决办法:

1.升级mybatis-plus-boot-starter的版本到3.4.1。

2.由于我使用了mybatis- plus的代码生成器,所以mybatis-plus-boot-starter升级上去还是报错,还需要把mybatis-plus-generator的版本升到3.4.1.这样才不会报错。

8.提交代码到gitee,url报错

问题原因:新创建springboot项目提交到代码到gitee,在填写url时,报错。

remote: [31m[session-ac403baf] Oauth: Access token isexpired [0munable to access

解决办法:

1.直接在控制台输入:

git config --system --unset credential.helper

2.输入后重启IDEA

9.创建多模块项目的子项目,模块指向错误

问题原因:发生该问题的根本原因是因为在项目文件夹的外层包含着另一个项目,此时项目文件无法确定该文件的pom依赖是引用哪一个parent依赖导致的。

'parent.relativePath' of POM com.ckb:eureka-server:0.0.1-SNAPSHOT (/Users/chenkunbo/Downloads/springcloud/EureKa_Demo/eureka-server/pom.xml) points at com.ckb:EureKa_Demo instead of com.ckb:SpringCloudDemo, please verify your project structure

解决办法:

在pom文件中spring-boot-starter-parent依赖中添加 <relativePath/>属性.

10.gateway项目中,Web依赖 和 webflux依赖冲突报错

问题原因:这是因为 gateWay 工程中同时引入了 Web依赖 和 webflux依赖,或者是父工程中有Web依赖,二当前微服务中有webflux依赖,这样会出现冲突

Description:

Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' in your configuration.
 

解决办法:删除Web依赖

11.SpringBoot项目,前端正常输入参数,后端接收却为null

问题原因:在项目开发中,我在正常写增删改查,写到增加用户时,使用postmen传参数,参数一切正常,但是后端接收却为null,很是疑惑,在查阅资料后得知,是参数上少了一个注解@RequestBody。

Creating a new SqlSession
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1283f4fe]
JDBC Connection [HikariProxyConnection@1382129385 wrapping com.mysql.cj.jdbc.ConnectionImpl@9e33473] will be managed by Spring
==>  Preparing: insert into sys_user(login_name, user_name, phonenumber, sex, email) value(?, ?, ?, ?, ?)
==> Parameters: null, null, null, null, null
<==    Updates: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1283f4fe]
Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1283f4fe]
Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1283f4fe]
Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1283f4fe]

解决办法: 在post的请求时,给参数加上@RequestBody即可。

大佬链接:springboot开发中 以POST方式向后台传递json数据的时候参数值为null_post请求成功但参数值为null_毕邺的博客-CSDN博客

12.postmen发送json请求,后端接收后报类型错误

错误原因:jackson无法将传递的 JSON 反序列化为Integer。

个人理解,前端传的东西,后端接受不了,系统帮你说:这不是我的活,我不干

2023-05-18 17:47:39.226  WARN 77845 --- [nio-8000-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.Integer` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.Integer` out of START_OBJECT token
 at [Source: (PushbackInputStream); line: 1, column: 1]]

解决办法:把userid放在路径上,使用@PathVariable注解。

13.Mybatis-plus 自动填充数据为null

错误原因:[填充的数据类型] 和 [实体类定义的数据类型] 不一致。

解决办法:

1.下图为实体类注解,@JsonFormat:设置输出的日期格式,@TableField:设置自动填充策略为fill = FieldFill.INSERT。

2.新建一个MyMetaObjectHandler设置自动填充策略

package com.ckb.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
/**
 * 自动填充createTime、updateTime
 * @author chenkunbo
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
    }
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
    }
}

3.在mapper.xml文件编写sql语句 

14.Mybatis-Plus 在操作数据时需要通过 @TableId 注解来识别实体类的主键字段

问题原因:在创建实体类时,没有在主键上添加@TableId注解来识别实体类的主键字段。

报错如下:

class com.ckb.pojo.SysRole ,Not found @TableId annotation, Cannot use Mybatis-Plus 'xxById' Method.

解决方法:

在主键上添加@TableId(value = "user_id", type = IdType.AUTO),说明:value = "user_id",这个时自己数据库的主键字段叫user_id。

15.mapper.xml文件逻辑删除语句写错,导致报错

问题原因:实现功能为逻辑删除角色id,实际为更改del_flag字段值,所以是update,而我用了select语句,报错如下:

Mapper method 'com.ckb.mapper.SysRoleMapper.deleteRoleById attempted to return null from a method with a primitive return type (int).

解决办法:将select修改为update ;

修改后:

16.JDBC数据链接报错,服务无法启动

问题原因:就加了一个maven模块,突然服务就启动不了,百度后了解,是连接数据库

的问题,虽然解决了,但是我以前没加allowPublicKeyRetrieval=true,也没有报错啊,报错如下

 Error creating bean with name 'jdbcConverter' defined in class path resource [org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfiguration$SpringBootJdbcConfiguration.class]: Unsatisfied dependency expressed through method 'jdbcConverter' parameter 4; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcDialect' defined in class path resource [org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfiguration$SpringBootJdbcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.relational.core.dialect.Dialect]: Factory method 'jdbcDialect' threw exception; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed

解决办法:

在appliction.yml中的mysql驱动上加,allowPublicKeyRetrieval=true,虽然问题解决了,但是我依然觉得和这个没啥关系,我以前也能正常运行啊。

17.使用redis做缓存,LocalDateTime属性字段导致的获取信息反序列化问题,结果报错

问题原因:这个错误通常是由于在进行请求参数的JSON反序列化时,发现 JSON 中包含了 LocalDateTime 类型的数据,但是 Jackson 库默认会尝试使用无参构造函数来创建对象实例,而 LocalDateTime 类并没有提供默认的无参构造函数。报错如下:

 Could not read JSON: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

解决办法:为了解决这个问题,我们可以通过添加自定义的日期反序列化器来支持 LocalDateTime 的 JSON 反序列化。


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Configuration
public class LocalDateTimeConfig {

    @Autowired
    private ObjectMapper objectMapper;

    // 配置日期序列化器和反序列化器
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> {
            builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
            builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        };
    }

    // 配置 RestTemplate 日期反序列化器
    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
        for (HttpMessageConverter<?> converter : converters) {
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                MappingJackson2HttpMessageConverter jsonConverter = (MappingJackson2HttpMessageConverter) converter;
                ObjectMapper objectMapper = jsonConverter.getObjectMapper();
                objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
                objectMapper.registerModule(new JavaTimeModule());
                // 添加自定义的日期反序列化器
                objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
                jsonConverter.setObjectMapper(objectMapper);
            }
        }
        return restTemplate;
    }
}

18.多模块开发时,子模块project报错

报错原因:因为有多个模块无法确定引用

'parent.relativePath' of POM com.ckb:leadnews:1.0-SNAPSHOT points at com.ckb:SpringCloudDemo instead of org.springframework.boot:spring-boot-starter-parent, please verify your project structure

 解决办法:

在当前模块加入<relativePath/>然后刷新maven 




在学习中遇到bug是不可避免的,所以想收集一下,方便以后自己回顾。

bug是收集不完的,解决一个又来一个!!!

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Web开发实例大全(提高卷)筛选、汇集了Java Web开发从基础知识到高级应用各个层面的大量实例及源代码,共有600个左右,每个实例及源代码按实例说明、关键技术、设计过程、详尽注释、秘笈心法的顺序进行了分析解读。 全书分为7篇23章,包括流行组件应用、数据库应用、图表统计、Ajax框架应用、流行框架、网站安全与架构模式、综合应用等。重点内容有操作XML文件、发送与接收邮件、数据库操作技术、SQL语句应用技术、复杂查询技术、数据库高级应用、JFreeChart绘图基础、基础图表技术、扩展图表技术、基于Cewolf组件的图表编程、Prototype框架、jQuery框架、Dojo框架、Struts2框架应用、Struts2框架标签应用、Hibernate框架基础、Hibernate高级话题、Spring框架基础、Spring的 Web MVC 框架、网站性能优化与安全策略、设计模式与架构、网站设计与网页配色、Java Web典型项目开发案例等。配书光盘附带了实例的源程序。 《Java Web开发实例大全(提高卷)》既适合Java Web程序员参考和查阅,也适合Java Web初学者,如高校学生、软件开发培训学员及相关求职人员学习、练习、速查使用。 目录 第1篇 流行组件应用篇 第1章 操作XML文件 第2章 发送与接收邮件 第2篇 数据库应用篇 第3章 数据库操作技术 第4章 SQL语句应用技术 第5章 复杂查询技术 第6章 数据库高级应用 第3篇 图表统计篇 第7章 JFreeChart绘图基础 第8章 基础图表技术 第9章 扩展图表技术 第10章 基于Cewolf组件的图表编程 第4篇 Ajax框架应用篇 第11章 Prototype框架 第12章 jQuery框架 第13章 Dojo框架 第5篇 流行框架篇 第14章 Struts2框架应用 第15章 Struts2框架标签应用 第16章 Hibernate框架基础 第17章 Hibernate高级话题 第18章 Spring框架基础 第19章 Spring的Web MVC框架 第6篇 网站安全与架构模式篇 第20章 网站性能优化与安全策略 第21章 设计模式与架构 第7篇 综合应用篇 第22章 网站设计与网页配色 第23章 Java Web典型项目开发案例

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值