1.新建Maven项目
2.添加相关依赖,打开项目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>spring.boot.helloworld</groupId>
<artifactId>helloworld</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>helloworld Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--
spring boot父节点依赖,引入这个之后相关的引入就不需要加version配置,spring boot
会自动选择最合适的版本进行添加
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>helloworld</finalName>
</build>
</project>
3.新建包以及相关类App,HelloController
package com.cjq.boot;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by struggle on 2017/7/29.
*/
@RestController//等价于@Controller和@ResponseBody
public class HelloController {
@RequestMapping("/hello")//建立请求映射
public String hello(){
return "hello";
}
}
package com.cjq.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by struggle on 2017/7/29.
*/
@SpringBootApplication//指定这是一个Spring boot应用程序
public class App {
public static void main(String[] args) {
/**
* 在main方法中启动应用程序
*/
SpringApplication.run(App.class,args);
}
}
4.运行App启动类
在浏览器访问http://localhost:8080/hello