详细SpringBoot教程之入门springboot 简单入门案例 hello World

系统要求

  • Java8以上
  • Maven3.3以上

Maven的配置

<!--阿里云配置-->
<mirrors>
      <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      </mirror>
  </mirrors>
 <!--使用JDK1.8-->
<profiles>
     <profile>
          <id>jdk-1.8</id>
          <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>1.8</jdk>
          </activation>
          <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
          </properties>
     </profile>
</profiles>

一、HelloWrod

1.1 创建Maven工程

写一个外部调用接口

1.2 引入Spring boot启动器,web启动器

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.4.RELEASE</version>
</parent>

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

1.2 编写主类

@SpringBootApplication
public class MainApplication {
    
    
    public static void main(String[] args){
    
    
        SpringApplication.run(MainApplication.class,args);
    }
}

1.3 编写业务

  • 控制器
  • @RestController
    • @RequestBody
    • @Controller

@RestController
public class DemoController {
    
    
    @RequestMapping("/hello")
    public String app1(){
    
    
        return "Hello SpringBoot2";
    }
}

1.4 简化测试

  • 直接运行主类中Main即可

1.5 简化配置

通过application.properties来配置端口号等配置

1.6 简化部署

  • 引入Spring boot maven插件,打成Jar包,在目标服务器即可
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

记得取消CMD快速编辑模式

猜你喜欢

转载自blog.csdn.net/Janyi_/article/details/114453198