新建一个maven项目
pom.xml增加如下配置
UTF-8 1.8 1.8 org.springframework.boot spring-boot-starter-parent 1.5.0.RELEASE org.springframework.boot spring-boot-starter-web ${project.artifactId} org.apache.maven.plugins maven-compiler-plugin org.springframework.boot spring-boot-maven-plugin
创建一个HelloController
package com.zns.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic 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;@SpringBootApplicationpublic 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含有下面依赖
org.springframework.boot spring-boot-maven-plugin
点击 Run Configurations,找到maven build,点击new,输入name
点击browse workspace选中要启动的项目
Goals处填写spring-boot:run
点击apply和run即可