1 重复注入问题
在多模块的maven项目中,如果当前项目引用的模块中注入了相同的bean,会提示重复注入错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘requestMappingHandlerMapping’ defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘com.*.*.*.handler.HttpErrorController’ method
com.*.*.*.handler.HttpErrorController#error(HttpServletResponse)
to { /error}: There is already ‘basicErrorController’ bean method org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest) mapped.
- 错误原因:在该例中,可以看到是
org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController
类注入了该Bean,查看源码可知,该自动配置在org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
类中完成
- 解决方法:只需要在引用的模块中加入
@AutoConfigureBefore(ErrorMvcAutoConfiguration.class)
注解即可先于该自动配置完成注入。
2 无法打包的问题
在执行 maven 打包时,出现以下错误提示
[ERROR] Failed to execute goal on project api-receiver:
Could not resolve dependencies for project com.*.*.api:api-receiver:jar:1.0-SNAPSHOT:
Failed to collect dependencies at com.*.*.api:api-server-util:jar:1.0-SNAPSHOT:
Failed to read artifact descriptor for com.*.*.api:api-server-util:jar:1.0-SNAPSHOT:
Could not find artifact com.*.*.api:api-server:pom:1.0-SNAPSHOT
-
错误原因:在本例中,
api-server-util
是当前项目的一个模块,在执行打包过程前,由于这个模块没有安装到本地maven仓库中,因此提示找不到这个依赖。 -
解决方法:将
api-server-util
模块执行install
操作,将模块安装到本地maven仓库中。 -
PS:
api-server-util
模块的pom文件中<parent></parent>
标签不能填写当前项目的id,即将该模块独立出来,不依赖当前项目。否则执行install过程中有可能会提示找不到父文件pom。
修改后的pom配置如下:
3 解决 Tomcat 部分特殊字符的问题
在使用 tomcat 7 及以上版本时,如果提交的url中包括一些特殊字符(如"{“、”}"等),会提示 400 错误。
-
错误原因:Tomcat 7 及以上版本严格遵循 RFC 7230 和 RFC 3986 的规定,不接受特殊字符的请求。
-
解决方法:将 web 容器替换成 jetty 即可。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>