IntelliJ IDEA搭建SpringBoot项目

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IntelliJ IDEA搭建SpringBoot项目相关的知识,希望对你有一定的参考价值。

参考技术A

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

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之间一定要空格

建议远程部署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同一目录:

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

3)生成war包放到tomcat里

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

第二步:写html

怎么让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文件

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

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

使用intellij idea 搭建springboot项目实践

1.使用intellij idea 搭建springboot项目

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

pom.xml文件  开始的demo 先把mybatis 注释掉

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ssm</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.mybatis.spring.boot</groupId>-->
<!--            <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!--            <version>2.1.1</version>-->
<!--        </dependency>-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

 

 

 

通过配置文件 application.properties 

修改端口server.port=8081

访问路径增加项目名

使用context-path (上下文属性) 设置 /demo 前缀  server.servlet.context-path=/demo

 

 

 

 

 

 

以上是关于IntelliJ IDEA搭建SpringBoot项目的主要内容,如果未能解决你的问题,请参考以下文章

IntelliJ IDEA搭建SpringBoot应用

IntelliJ IDEA搭建SpringBoot项目

intellij idea搭建springboot

IntelliJ IDEA搭建SpringBoot的小Demo

intellij idea搭建SpringBoot

用IntelliJ IDEA搭建第一个SpringBoot例子