用Spring Boot写自己的第一个HelloWorld程序,超详细

    技术2023-10-03  104

    1、所用工具有关版本如下

    jdk 1.8maven 3.3.6idea 2018

    2、创建spring boot项目

    1、创建maven 项目 然后点击Nest继续填写 然后finish

    2、在pom.xml中添加依赖

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.atzy</groupId> <artifactId>spring_boot_01_helloWorld</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>

    3、创建Spring Boot启动类

    package com.atzy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /* * @SpringBootApplication 来标注一个主程序类,说明这是一个spring boot应用 * */ @SpringBootApplication public class HelloWorldMainApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldMainApplication.class,args); } }

    3、创建Controller

    package com.atzy.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @RequestMapping("/hello") @ResponseBody public String hello(){ return "Hello World!"; } }

    4、启动第二步创建的Spring Boot启动类 5、即可进行访问

    3、简单部署

    1、在pom.xml中加入插件依赖

    <!--这个依赖可以将应用打包成一个可执行的jar包--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

    2、进行导包,双击proback完成打包

    Processed: 0.011, SQL: 9