快速构建可运行的Spring-boot项目(Hello World)

spring-boot快速构建hello world

  • 创建Maven项目
    快速构建可运行的Spring-boot项目(Hello World)_第1张图片

  • 配置pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>com.pdzgroupId>
    <artifactId>springbootartifactId>
    <packaging>warpackaging>
    <version>0.0.1-SNAPSHOTversion>
    <name>springboot Maven Webappname>
    <url>http://maven.apache.orgurl>

    <properties>
        <jdk.version>1.8jdk.version>
    properties>


    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.9.RELEASEversion>
    parent>

    
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>

    <build>
        <finalName>springbootfinalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>${jdk.version}source>
                    <target>${jdk.version}target>
                configuration>
            plugin>


            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>

        plugins>
    build>

project>
  • 建立一个包,写一个Hello world
package com.pdz.helloworld;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorld {
    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }

}
  • 第一种方式:把项目加入web容器 启动浏览器输入 http://localhost:8080/springboot/ 结果:
    快速构建可运行的Spring-boot项目(Hello World)_第2张图片

  • 第二种方式启动:在src/main/java下写一个类 如下面这个类,然后右键运行,启动成功后再浏览器中输入http://localhost:8080/ 就可以看到Hello World 这种启动方式后面不用跟项目名称

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.pdz.helloworld.HelloWorld;
//HelloWorld.class是上面写的类名 
@SpringBootApplication(scanBasePackageClasses=HelloWorld.class)
public class AppStart {
    public static void main(String[] args) {
        SpringApplication.run(AppStart.class, args);
    }
}
  • 完成一个最基本的spring-boot Hello World项目

你可能感兴趣的:(Spring,spring-boot,java)