SpringBoot之第一个应用HelloWorld

新建一个maven项目

pom.xml增加如下配置

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.0.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
<build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- java编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
</build>

创建一个HelloController

package com.zns.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";
    }
}

创建项目启动类Application

package com.zns;

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

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        //设置启动时是否显示banner图
        application.setBannerMode(Banner.Mode.OFF);
        application.run(args);
    }
}

banner图可以在src/main/resources下新增banner.txt文件设置

 

src/main/resources下增加一个application.yml

server:
  port: 8080
  context-path: / # 访问地址:http://localhost:8080/

yml文件需注意冒号后面要有空格!

 

启动方式1:

在启动类文件右键 Run as Java application

浏览器访问http://localhost:8080/hello 

 

启动方式2:

 

利用springboot maven插件启动

确保pom.xml含有下面依赖

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

点击  Run Configurations,找到maven build,点击new,输入name

点击browse workspace选中要启动的项目

Goals处填写spring-boot:run 

点击apply和run即可

猜你喜欢

转载自www.cnblogs.com/zengnansheng/p/10389770.html