美文网首页
SpringBoot HelloWorld

SpringBoot HelloWorld

作者: MinicupSimon | 来源:发表于2019-03-17 16:57 被阅读0次
    项目代码:https://github.com/daydreamer1988/springboot-helloworld

    方法一:使用Spring Initializer

    New Project 选择构建类型 选择需要的依赖 解析下载依赖 初始目录结构

    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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.minicup</groupId>
        <artifactId>springboot-helloworld</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>springboot-helloworld</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.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
               
                    // 支持Maven打包
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    

    Application

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

    以上就是所有的配置,运行main函数

    image.png

    打开浏览器http://localhost:8080

    image.png

    服务是启动了, 但是还没有接口,接下来创建HelloController.java

    @RestController
    public class HelloController {
    
        @GetMapping("/hello/{name}")
        public String hollo(@PathVariable String name){
            return "Hello "+ name;
        }
    }
    
    

    重新运行

    image.png

    除了运行main函数之外, 还可以通过命令行执行mvn spring-boot:run来启动

    image.png

    项目打包部署

    image.png image.png

    在命令行中输入java -jar target/springboot-helloworld-0.0.1-SNAPSHOT.jar

    image.png
    注意, pom文件中的依赖没有版本号, 这是因为parent的spring-boot-starter-parent中已经定义了

    方法二:使用普通Maven创建SpringBoot

    image.png image.png 目录结构

    pom.xml

    image.png

    添加以下依赖(https://spring.io/guides/gs/rest-service/)

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.jayway.jsonpath</groupId>
                <artifactId>json-path</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    

    添加Application类

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

    添加TestController类

    @RestController
    public class TestController {
    
        @GetMapping("/hello/{name}")
        public String hello(@PathVariable String name){
            return "Hello " + name;
        }
    }
    

    运行main函数,访问8080,完成

    方法三:官网下载类似脚手架项目,IDEA直接打开即可

    image.png

    相关文章

      网友评论

          本文标题:SpringBoot HelloWorld

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