您的位置:首页 > 编程语言 > Java开发

用Spring Boot搭建简单web项目

2017-06-09 00:00 1006 查看

一,新建maven 项目

新建一个maven工程
输入相应的 groupId,artifactId

项目建好后,目录结构应该是这样的:





接下来修改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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<groupId>com.lee</groupId>
<artifactId>spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot</name>
<url>http://maven.apache.org</url>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

可以看到,继承了spring-boot-starter-parent,依赖了junit,spring-boot-starter-web,spring-boot-starter-velocity

以前我们在spring的配置,spring-boot都会按照默认配置,帮我们弄好

二,Hello World

先写一个controller

package com.lee.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloWorldController {

@RequestMapping("/hello")
@ResponseBody
public String hello(){
return "hello world";
}
}

再写一个启动程序

package com.lee;

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

/**
* Hello world!
*
*/
@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

PS:注意包路径,启动程序应该放在包目录的最外层。

三,启动,测试

运行main函数

启动成功后,在浏览器输入http://localhost:8080/hello

可以看到 hello world
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Spring Boot