spring boot 项目简单搭建

本文介绍了如何一步步创建一个Spring Boot项目,从建立Maven项目结构开始,详细讲解了配置pom.xml文件、编写启动类和Controller,到设置Thymeleaf视图层,并提供了通过http://localhost:8080/hello/view进行访问的方法,最后还提到了测试类的编写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建一个maven项目Spring-boot-demo,,结构如下:

2.配置pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
	<artifactId>spring-boot-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-boot-demo</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.5.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
        <!--热部署配置工具  -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>


	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
                <!--热部署配置工具  -->
				<dependency>
					<groupId>org.springframework</groupId>
					<artifactId>springloaded</artifactId>
					<version>1.2.6.RELEASE</version>
				</dependency>
			</plugin>
		</plugins>
	</build>


</project>

3.编写启动类SpringbootDemoApplication.java

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

@SpringBootApplication//开启自动扫面和自动配置
public class SpringBootDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootDemoApplication.class, args);//负责启动引动应用程序
	}
}

4.编写Controller类

package com.example.demo.controller;

import java.util.HashMap;
import java.util.Map;

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

/**
 * @Controller注解是返回页面,比如hello.html 注意这里不要使用@RestController,这样会导致页面没法展示的问题
 * 如果需要返回Json串,直接在方法上加@ResponseBody即可
 *
 */
@Controller
@RequestMapping("/")
public class HelloController {
	// 获取页面
	@RequestMapping(value = "/hello/view", method = RequestMethod.GET)
	public String hello(Model model) {
		model.addAttribute("msg", "hello thymeleaf!");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "jack");
		model.addAttribute("map", map);
		return "hello";
	}

	// 获取对象
	@RequestMapping(method = RequestMethod.GET, value = "/map")
	@ResponseBody // 增加注解说明是返回对象
	public Map<String, Object> getJson() {
		System.out.println(">>>>测试/boot/getMap");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "jack");
		return map;
	}
}

4.编写视图层thymeleaf

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>title</h1>
	<span th:text="${msg}"></span>
	<span th:text="${map['name']}"></span>
</body>
</html>

5.浏览器输入 http://localhost:8080/hello/view

6.可以编写测试类SpringBootDemoApplicationTersts.java

package com.example.demo;

import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.example.demo.controller.HelloController;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootDemoApplicationTests {
	@Autowired
    HelloController helloController;
    @Test
    public void testDemo() {
        Map<String,Object> map = helloController.getJson();
        Assert.assertEquals(map.get("name"), "jack");
    }
	@Test
	public void contextLoads() {
		System.out.println("-----------------------测试类------------------");
	}

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值