新建一个 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.YAML
- server:
- port: 8080
- context-path: / # 访问地址: http://localhost:8080/
YAML 文件需注意冒号后面要有空格!
启动方式 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 即可
来源: http://www.bubuko.com/infodetail-2957330.html