美文网首页
IntelliJ IDEA搭建SpringBoot项目

IntelliJ IDEA搭建SpringBoot项目

作者: 修符道人 | 来源:发表于2020-03-06 14:41 被阅读0次

Maven配置阿里镜像

若不使用镜像,会发现maven项目会同步半天都不成功。
https://www.cnblogs.com/dengbo/p/12003667.html

maven配置.jpg
这里的Maven Home Directory,我一配置成自己下载的Maven就报错,只能用自带的,不知道为啥?

pom.xml报错日志:
getInputStream() must not be called against a directory: apache-maven-3.6.3/conf

改变接口访问的项目名

通过修改application.yml文件即可,注意SpringBoot2.0前后的写法不一样。
https://blog.csdn.net/Javaer_dog/article/details/83153613
2.0及之前:
server.context-path=/myprojectname
2.0之后:
server.servlet.context-path=/myprojectname
而且还有个重点我要强调:key与value之间一定要空格

Run项目到本地Tomcat服务器

远程部署(IDEA+SpringBoot+CentOs+Tomcat)

建议远程部署war包之前,先在本地Tomcat部署测试,再去远程部署测试。

下面的都是打war包然后部署
https://blog.csdn.net/shaoyedeboke/article/details/90454494
https://blog.csdn.net/zhanglf02/article/details/86313540
https://www.cnblogs.com/sanjay/p/11818436.html
https://www.licoy.cn/2887.html/amp
https://www.jianshu.com/p/baf624064540
上面的这些博客说的可能千奇百怪,我实践总结了一下,就两点:
1)pom.xml配置打包类型为war
<packaging>war</packaging>
2)定义ServletInitializer类
定义ServletInitializer类,和Application同一目录:

    public class ServletInitializer extends SpringBootServletInitializer {
   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
       //Application的类名
       return application.sources(MainApplication.class);
   }
}

当然也可以把这个configure方法直接写到Application类里。

@SpringBootApplication
public class MainApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        //Application的类名
        return application.sources(MainApplication.class);
    }
}

3)生成war包放到tomcat里

  1. 访问测试
    ip:8080/War包名字/...

如何在SpringBoot里写一个html让浏览器可以访问

第一步: pom.xml里添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!-- 对应application.properties中LEGACYHTML5 -->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>

第二步:写html

image.png
注意上面img的src的写法,使用了thymeleaf模板。

怎么让html适配手机的大小:https://blog.csdn.net/jacob_ios/article/details/79739536
在<head>里添加<meta charset="UTF-8" name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">

第三步:配置application.yml文件

spring:
  thymeleaf:
    prefix: classpath:templates/ # Prefix that gets prepended to view names when building a URL.
    suffix: .html # Suffix that gets appended to view names when building a URL.
    mode: LEGACYHTML5 # Template mode to be applied to templates. See also Thymeleaf's TemplateMode enum.
    encoding: UTF-8
    content-type: text/html
    cache: false

这里的mode: LEGACYHTML5需要依赖第一步pom.xml中依赖的nekohtml,代表不是严格规范的HTML5,否则html5很容易报错。
第四步:新建Controller
1)注意@Controller不是@RestController,后者这个接口会返回普通的字符串,而不是一个网页。
2)注意pom.xml中的spring-boot-starter-thymeleaf,没有这个的话,接口会报404。

@Controller
@RequestMapping("guide")
public class GuideDocController {
    @RequestMapping("copyUrl")
    public String copyUrl(){
        return "/guide_doc/video/parseUrl";
    }
}

大坑

电脑经常性的重启,导致IDEA意外关闭,然后项目就不被识别为Maven项目了,View-ToolWindow-MavenProject就没有这个选项。application.yml修改也没有生效。
解决办法:关闭项目重新import导入即可

相关文章

网友评论

      本文标题:IntelliJ IDEA搭建SpringBoot项目

      本文链接:https://www.haomeiwen.com/subject/pknthhtx.html