01_Spring Boot 之 HelloWorld详解
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.0</modelVersion>
<groupId>com.linjb</groupId>
<artifactId>springboot-helloword</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-helloword Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<!-- Spring Boot web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency> -->
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>
<finalName>springboot-helloword</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<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>
</plugins>
</build>
</project>
2、Controller层
package com.linjb.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* restful接口
* @Copyright Copyright (C) 2019 linjb
* @author linjb
* @version 1.0
* @CreateDate 2019年1月31日下午10:27:31
*/
@RestController
public class HelloWorldController {
@RequestMapping("/")
public String sayHello() {
return "Hello,World!";
}
}
@RestController和@RequestMapping注解是来自SpringMVC的注解,它们不是SpringBoot的特定部分。
1. @RestController:提供实现了REST API,可以服务JSON,XML或者其他。这里是以String的形式渲染出结果。
2. @RequestMapping:提供路由信息,"/“路径的HTTP Request都会被映射到sayHello方法进行处理。
3、 启动应用类
package com.linjb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
/**
* Spring Boot应用启动类
* @Copyright Copyright (C) 2019 linjb
* @author linjb
* @version 1.0
* @CreateDate 2019年1月31日下午10:25:16
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
1. @SpringBootApplication:Spring Boot 应用的标识
2. Application很简单,一个main函数作为主入口。SpringApplication引导应用,并将Application本身作为参数传递给run方法。具体run方法会启动嵌入式的Tomcat并初始化Spring环境及其各Spring组件。
4.1、使用内置 Tomcat 运行
直接右键Run运行Application类。同样你也可以Debug Run。在浏览器中输入127.0.0.1:8080
4.2、使用独立的 Tomcat 运行
1)、修改pom文件,添加如下配置:
2)、pom文件中添加如下节点:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
3)、修改 Application 启动类:
4)、Maven clean;
5)、Maven install;
[INFO] Installing D:\EclipseWorkspace\SpringBootDemo\springboot-helloword\target\springboot-helloword.war to D:\maven\repository\com\linjb\springboot-helloword\0.0.1-SNAPSHOT\springboot-helloword-0.0.1-SNAPSHOT.war
[INFO] Installing D:\EclipseWorkspace\SpringBootDemo\springboot-helloword\pom.xml to D:\maven\repository\com\linjb\springboot-helloword\0.0.1-SNAPSHOT\springboot-helloword-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
出现 springboot-helloword.war 文件标识打包成功。
6)、将 war 包复制到 Tomcat 中启动即可;
7)、在浏览器中输入127.0.0.1:8080/springboot-helloword/