bug
- 【java】java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
- 【联调】`{$ref: “$[0]“}`
- 【联调】Required String parameter ‘id‘ is not present
- 【mysql】{dataSource-1} closed
- 【mysql】LocalDateTime写入数据库有时差
- 【mysql】Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded
- 【mybatis】`<if>`中`test`的`==`判断不生效
- 【mybatis】传参到xml后不报错但查询失败
- 【mybatis plus】mybatis plus Invalid bound statement (not found):
- 【gateway】Consider defining a bean of type ‘org.springframework.http.codec.ServerCodecConfigure
- 【RabbitMQ】springcloud config bus 刷新配置报错
- 【feign-hystrix】feign hystrix enabled设置无效
- 【feign】feign.RetryableException: connect timed out executing GET http://nacos-authserver/user/userinfo
- 【oauth2】更新token报错“server error”、“token server UserDetailsService is required”
- 【oauth2】/oauth/check_token报错“401”、“Unauthorized”
- 【oauth2】“Invalid token does not contain resource id (oauth2-resource)”
【java】java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
原因:该数组值不存在,
if
判断或try catch
List<SysOrg> orgList=sysOrgService.getByUserId(su.getUserId());
String orgName="";
if (orgList!=null&&orgList.size()>0) {
orgName=orgList.get(0).getOrgName();
su.setOrgName(orgName);
list2.add(su);
}
【联调】{$ref: “$[0]“}
问题描述
后台接口代码
if(res1 != null){
res.add(res1.get(0));
res.add(res1.get(0));
}
return JSON.toJSONString(res)
前端请求返回
0: {loan_score: 97, absence_score: 77.5, night_score: 73.8, game_score: 86.5, video_score: 86.84}
1: {$ref: "$[0]"}
问题解决
SerializerFeature.DisableCircularReferenceDetect
import com.alibaba.fastjson.serializer.SerializerFeature;
return JSON.toJSONString(res,SerializerFeature.DisableCircularReferenceDetect);
【联调】Required String parameter ‘id‘ is not present
-
若后台用@RequestParam接收参数,
@RequestParam(value="id", required = false) String id
- Content-Type: 为
application/x-www-form-urlencoded
、form-data
- GET,POST均可
- 可以是多个参数,也可以是一个Map集合
- Content-Type: 为
-
若后台用@RequestBody接收参数,
@RequestBody Map map
- Content-Type为
application/json
, application/xml - form-data、x-www-form-urlencoded时候不可用
- GET请求不能用@RequestBody来接收参数
- 只有一个参数
- Content-Type为
【mysql】{dataSource-1} closed
问题描述
好久不打开的程序打开,突然无法运行成功,没有其他报错信息,就是 {dataSource-1} init
完很快就 {dataSource-1} closed
,然后failed
[extShutdownHook] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closed
问题分析
- 验证数据库本身无误可连接
- 验证application.yml无误
- pom的mysql-connector-java更换version也无效
问题解决
去掉pom.xml
中的spring-boot-starter-tomcat
就能运行了
猜测是和springboot的tomcat有冲突,也不知道之前是怎么运行无误的
【mysql】LocalDateTime写入数据库有时差
jdbc连接里边有这样一个参数&serverTimezone=UTC,改为&serverTimezone=CTT即可
CTT 指Asia/Shanghai
spring:
datasource:
url: jdbc:mysql://XXXX:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=CTT
【mysql】Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded
具体原因不清楚,尝试的多种方法都无效,应该是格式问题
删掉旧的application.yml重新创建application.yml
配置就好了
【mybatis】<if>
中test
的==
判断不生效
问题
代码如下,一直以为是的问题,怎么改都没用
<sql id="List_Index">
(select * from warning where event = #{event} group by phone,
<if test="event!='1'">touch_time</if>
<if test="event=='1'">substring(touch_time,1,10)</if>)
</sql>
<select id="getIndexByDate" resultMap="CardIndex">
select count(*) as total from <include refid="List_Index"/> as l
</select>
解决
数字加单引号会被默认为数字,和字符串的event比较会不相等,从而导致==
判断失效,可通过toString()
转换
<if test="event!='1'.toString()">touch_time</if>
<if test="event=='1'.toString()">substring(touch_time,1,10)</if>)
【mybatis】传参到xml后不报错但查询失败
解决:#{}
改为${}
, #{}
会为变量加"",当传送字符串到select,会查询不到结果
${}
会把传入的参数直接显示,而#{}
会把传入的参数加双引号当字符串处理
#{}
是预编译处理,${}
是字符串替换- Mybatis在处理
#{}
时,会将sql中的#{}
替换为占位符‘?’
,调用PreparedStatement的set方法来赋值 - Mybatis在处理
${}
时,就是把${}
替换成变量的值 - 使用
#{}
可以有效的防止SQL注入,提高系统安全
【mybatis plus】mybatis plus Invalid bound statement (not found):
问题描述
配置都没错,打包后target/classes目录下没有生成xml
原因分析
使用idea打包时,会过滤一些文件
开始解决
-
pox.xml文件
<build>
配置<resources>
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.yml</include> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources>
-
maven必须先
clean
再campile
重新编译,查看target下生成xml后,再运行程序。
注意修改完后一定要campile重新编译,之前一直clean后直接运行,会报错找不到url配置,连yml文件也无法生成。
- 如果报数据库url错误,依然无法生成yml文件,可参考官方教程解决 mybatis-plus官网问答,注意配置文件mybatis-plus.mapper-locations根据自己实际位置。
<build>
<resources>
<resource>
<!-- xml放在java目录下-->
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--指定资源的位置(xml放在resources下,可以不用指定)-->
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
【gateway】Consider defining a bean of type ‘org.springframework.http.codec.ServerCodecConfigure
问题描述
nacos+gateway+sentinel,按网上的配置启动后总是报错
原因分析
多方尝试后发现问题主要在gateway的依赖里包含了spring-boot-starter-web,其他依赖里也有造成了冲突
解决方案
删除其他的spring-boot-starter-web依赖
【RabbitMQ】springcloud config bus 刷新配置报错
报错一:/bus-refresh刷新无效
RabbitMQ需要提前安装好
报错二:socket closed
原因
服务器安装完mq后认为别人配置中的port:5672
写错了,应该写成RabbitMQ请求web的端口15672
解决
改回5672
报错三:bus mq connection error; protocol method: #method<connection.close>(reply-code=530, reply-text=NOT_ALLOWED - access to vhost ‘/’ refused for user ‘root’, class-id=10, method-id=40)
给用户的权限下添加虚拟主机
一开时配置management.endpoints.web.exposure.include='*'总是没有自动提示(这个原因还没找到,总觉得是换了idea社区版的原因),只有加了spring-boot-starter-web才能正常配置,就导致了与gateway的冲突
【feign-hystrix】feign hystrix enabled设置无效
使用的是springboot的最新版本2.4.3和springcloud的最新版本2020.0.1,对应的feign版本不包含hystrix依赖
- 更换
springboot
版本为2.1.6.RELEASE - 更换
springclould
版本Greenwich.SR1
【feign】feign.RetryableException: connect timed out executing GET http://nacos-authserver/user/userinfo
Flipping property: nacos-authserver.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
feign.RetryableException: connect timed out executing GET http://nacos-authserver/user/userinfo
at feign.FeignException.errorExecuting(FeignException.java:132)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:113)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78)
...
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
...
原因
如果服务都在远程服务器没问题,如果服务都在本地启动没问题,但nacos服务与被调用的nacos-authserver服务都运行在服务器,而本服务在本地开发,所以是因为注册服务ip问题,导致本地服务无法通过服务名字找到相应的服务
- nacos服务与被调用的nacos-authserver服务都运行在服务器,所以通过服务器ip测试接口正常
- 本服务在本地(127.0.0.1)开发调试,通过nacos服务器ip来注册服务,所以本服务注册正常
- 本服务通过nacos服务找到nacos-authserver服务,nacos-authserver服务会告诉nacos服务本服务的ip是127.0.0.1,所以无法找到
解决
所以在本地开发时,可以指定fegin的url地址
@FeignClient(value = "nacos-authserver",url = "http://XXXX:18890/nacos-authserver",configuration = OAuth2FeignRequestInterceptor.class)
public interface UserRemote {
@GetMapping("/user/userinfo")
Object getUserInfo();
}
【oauth2】更新token报错“server error”、“token server UserDetailsService is required”
AuthorizationServer增加配置UserDetailsService
@Configuration
@EnableAuthorizationServer //注解开启了验证服务器
public class OAuth2AuthServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
public UserDetailsService userDetailsService;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints..userDetailsService(userDetailsService);
}
......
}
spring security oauth2授权服务刷新令牌报错UserDetailsService is required
【oauth2】/oauth/check_token报错“401”、“Unauthorized”
@Configuration
@EnableAuthorizationServer //注解开启了验证服务器
public class OAuth2AuthServerConfig extends AuthorizationServerConfigurerAdapter {
......
/**
* @Description: 配置 token 节点的安全策略
* @Param: [security]
* @Return: void
*/
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
//默认"denyAll()",不允许访问/oauth/check_token;
//"isAuthenticated()"需要携带auth信息认证访问;
//"permitAll()"可直接访问
security.checkTokenAccess("isAuthenticated()");
}
......
}
【oauth2】“Invalid token does not contain resource id (oauth2-resource)”
在每个ResourceServer(资源服务器)实例上设置resourceId,该resourceId作为该服务资源的唯一标识。(假如同一个微服务资源部署多份,resourceId相同)。
ResourceId是在Resource Server资源服务器进行验证。当资源请求发送到Resource Server的时候会携带access_token,Resource Server会根据access_token找到client_id,进而找到该client可以访问的resource_ids。如果resource_ids包含Resource Server自己设置ResourceId,就可以继续进行其他的权限验证。
@Configuration
@EnableResourceServer
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId("project_api");
}
...
}