1、父项目或管理依赖的项目应打包为pom
2、各个子模块之间不能构成循环依赖
如A依赖B、B又依赖A,这样是不行的,构建模块时要理清楚逻辑思路或业务流程。
3、多模块开发时,要注意包名统一,Springboot启动类应该在父包内
如A模块:com.example.a,B模块:com.example.b,则启动类应在com.example或者添加扫描路径@ComponentScan(basePackages = {"com.example.a","com.example.b"})
.
4、@RestController
和@Controller
注解的坑
@RestController
支持直接输出JSON,但是不支持跳转静态页面@Controller
支持跳转静态页面,但是不能直接返回JSON,若要返回则需要加@ResponseBody
注解。
5、使用Thymeleaf模板引擎后默认页面会从static换到templates文件夹,导致静态页面找不到。
如果用了静态模板你还想返回static中的页面,那么就要用重定向:
如果在使用动态页面时还想跳转到/static/index.html,可以使用重定向
return "redirect:/index.html"。
return "redirect:hello.html";
6、 在打包运行中跳过单元测试
<properties>
<skipTests>true</skipTests>
</properties>
7、freemarker多模块页面404
freemarker的默认配置静态资源路径是绝对路径、我们需要设置为相对路径classpath才能寻找到其他模块的页面文件;
FreeMarkerConfigurer中preferFileSystemAccess变量决定了是以那种方式加载静态资源文件;
让我们来看一下官方解释:
如果启用就以templateLoaderPath设置的路径来加载,默认为true,设置为false的话就以SpringTemplateLoader方式访问,那么就是流的方式访问,这可能是必要的,如果你的一些模板驻留在一个扩展的类而其他人驻留在jar文件的目录。
意思就是 如果你的文件在jar包文件目录下,就有必要设置为false,我们是多模块的,最终只运行一个模块,其他模块都是以jar包来加载的,我们要加载jar包里面的文件目录,必须要把这个属性设置为false才行,debug源码看看:
修改后的配置文件如下:
@Configuration
public class FreemarkerConfig {
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer(ShiroTag shiroTag){
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("classpath:/templates/");
configurer.setPreferFileSystemAccess(true);
Map<String, Object> variables = new HashMap<>(1);
variables.put("shiro", shiroTag);
configurer.setFreemarkerVariables(variables);
Properties settings = new Properties();
settings.setProperty("default_encoding", "utf-8");
settings.setProperty("number_format", "0.##");
settings.setProperty("auto_import", "spring.ftl as spring");
configurer.setFreemarkerSettings(settings);
return configurer;
}
}