SpringBoot常见错误集



1.解springboot项目请求出现非法字符问题 java.lang.IllegalArgumentException:

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
/**
* @author Blue
* @date 2020/3/12
**/

import org.apache.catalina.connector.Connector;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @Author: Blue
* Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
**/
@Configuration
public class TomcatConfig {

@Bean
public TomcatServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addConnectorCustomizers((Connector connector) -> {
connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}");
connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}");
});
return factory;
}
}

2.org.springframework.web.HttpMediaTypeNotSupportedException: Content type ‘application/json;charset=UTF-8’ not supported或其他Content type不支持处理

注意:

  • 使用post协议提交时,请检查Content type类型
  • 如果想用@RequestBody注解做提交json字符串自动绑定到pojo入参时,contentType类型需要是”application/json;charset=UTF-8”,否则也会抛”not supported”异常
1
2
3
4
5
6
7
8
9
10
11
// 示例
$.ajax({
type: "POST",
contentType: "application/json;charset=UTF-8",
url: "/reg",
data: JSON.stringify(data.field),
dataType: 'json',
success: function(result) {

}
});
1
2
3
4
5
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public Result<?> reg(@RequestBody UserInfo user) {
//...
}

心得:

  • @ResponseBody:将java对象转为json格式的数据,可作用在方法上
  • @RequestBody:将json数据转换成对象,可作用在方法入参上

3、循环依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
UserInfoServiceImpl类下:

自己注入自己没问题,只是不会是注入此类的最后引用

@Autowired
UserInfoServiceImpl

包括UserInfoServiceImpl中注入的RoleServiceImpl的也有异步方法,也可以正常启动

但是 UserInfoServiceImpl 如果有 @Async异步注解,就会启动Springboot失败

因为在 UserInfoServiceImpl类中 再次注入了 UserInfoServiceImpl


错误信息:
Bean with name 'userInfoServiceImpl' has been injected into other beans [userInfoServiceImpl] in its raw version as part of a circular reference, but has eventually been wrapped.
This means that said other beans do not use the final version of the bean.
This is often the result of over-eager type matching - consider using 'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.


名称为“userInfoServiceImpl”的Bean在其原始版本中作为循环引用的一部分被注入到其他Bean [userInfoServiceImpl]中,但最终被包装了。这意味着其他bean不使用该bean的最终版本。这通常是过于急切的类型匹配的结果——例如,考虑使用'getBeanNamesForType',同时关闭'allowEagerInit'标志。

参考链接:
https://blog.csdn.net/weixin_49200545/article/details/109352978
https://blog.csdn.net/qq_33886821/article/details/102585214

打赏
  • 版权声明: 本网站所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  1. © 2020-2021 Lauy    湘ICP备20003709号-1

请我喝杯咖啡吧~

支付宝
微信