Maven构建一个最简单的Spring Boot + Spring MVC项目

本文介绍了如何使用Maven创建一个简单的Spring Boot结合Spring MVC的项目。通过配置POM.xml,添加必要依赖,编写Spring Boot启动类和Spring MVC配置类,以及Controller,最后通过命令行构建并启动项目,实现访问'localhost:9000/hello'显示'hello world'。

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

使用Maven构建一个最简单的Spring MVC + Spring Boot项目,完全基于java config

一、新建一个maven项目,模板使用quickstart
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.luenxin.study</groupId>
  <artifactId>review</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>review</name>
  <url>http://maven.apache.org</url>
  
  <parent>
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-parent</artifactId>
  	<version>1.3.0.RELEASE</version>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>4.2.3.RELEASE</spring.version>
    <spring.boot.version>1.3.0.RELEASE</spring.boot.version>
    <tomcat.version>8.0.28</tomcat.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope><!-- 编译需要而发布不需要的jar包 -->
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>${spring.boot.version}</version>
    </dependency>
  </dependencies>
  
  <build>
    <finalName>review</finalName>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>1.3.0.RELEASE</version>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>


Spring Boot 启动类:

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

@SpringBootApplication
public class App {
    public static void main(String[] args){
        System.out.println("Hello World!");
        SpringApplication.run(App.class, args);
    }
}

Spring MVC配置类:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
public class WebConfig {

}

Controller类,注意这里的注解要写@RestController:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
	
	@RequestMapping("/hello")
	public String hello() {
		return "hello world";
	}

}



一个最简单的项目完成了,接着进入到项目目录,输入安装命令:
mvn clean install ,此时会把项目打成jar包,放在target目录下
接着输入启动命令:
java -jar target/review.jar --server.port=9000  我习惯用9000做端口
接着在浏览器输入:http://127.0.0.1:9000/hello
搞定!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值