idea 新建springboot 的 web 项目

Spring Boot Web项目搭建指南
本文详细介绍如何在IntelliJ IDEA中将一个普通的Maven Web项目转换为Spring Boot项目,并配置基本的Controller进行测试。

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

IDAE中新建web项目见:IntelliJ IDEA中新建JAVA WEB项目、maven项目

默认已有一个 maven 的 web 项目:gentle

 -------- 改装为springboot项目:

1. 在pom中加上springboot必须的parent 和 web jar包。

 

代码如下:

<?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</groupId>
    <artifactId>gentle</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

</project>

 

2. 书写springboot的主函数所在类 ,即程序入口(程序启动类)

 

注意此类不能直接放在java 文件夹下,要放在一个包内。我放在gentle包中,否则会提示不建议放在默认包中。

springboot 项目全工程中只能写一个 main 函数。  不能在其它类中定义main 函数,test类中也不可以。

给此类加注解:

@SpringBootApplication 表示此类为程序入口

 

代码如下:

package gentle;

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

/**
 * spring-boot的一些注解
 * 			ps:@SpringBootApplication 表示开启下述 1、2、5
 *
 *      1.@Configuration 注解是spring去xml配置,开启spring基于java的配置
 *        @Configuration 类级别的注解, 一般这个注解,我们用来标识main方法所在的类,完成元数据bean的初始化。
 *        @Bean 一个带有 @Bean 的注解方法将返回一个对象
 *
 *      2.@ComponentScan 收集自动收集所有的spring组件  搜索beans类级别的注解,自动扫描加载所有的Spring组件包括Bean注入,一般用在main方法所在的类上
 *
 *      3.@Import导入其他的Configuration类
 *
 *      4.@ImportResource附加注入一个外置的xml
 *
 *      5.@EnableAutoConfiguration 和 @SpringBootApplication是类级别的注解,
 *      根据maven依赖的jar来自动猜测完成正确的spring的对应配置,只要引入了spring-test-starter-web的依赖,默认会自动配置Spring MVC和tomcat容器
 *
 *      6.@Component类级别注解,用来标识一个组件,比如我自定了一个filter,则需要此注解标识之后,Spring Boot才会正确识别。
 *
 * Spring-boot加载配置文件的顺序是
 *
 * 		1. ./config/application.properties
 *		2. ./application.properties
 *		3. classpath:config/application.properties
 *		4. classpath:application.properties
 *
 * yaml文件里配置项名和bean里的属性名相同
 */

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
//        SpringApplication application = new SpringApplication(Application.class, "classpath*:/spring/security-*.xml");
        SpringApplication application = new SpringApplication(Application.class);
        application.setWebEnvironment(true);
        application.run(args);
    }
}

 

 

3. 书写一个测试 Controller , 验证是否可以正常访问工程。

 

 
@RestController 注解相当于 @ResponseBody + @Controller合在一起的作用。
package gentle.service;

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

@RestController
@RequestMapping("gentle")
public class First {

    @RequestMapping("/first")
    private String first(){

        return "just say something ... ";
    }
}

 

4. 配置项目端口号为8089:

 

5.启动项目,在 Application 类中直接右键 run 便可以启动项目。

(我习惯点击红框中三角,选择dubug 启动。)

 

6. 浏览器访问,url不用写项目名:

OK ,测试通过。

 

 

 

 

 
### 如何在 IntelliJ IDEA 中创建新的 Spring Boot Maven 项目 #### 准备工作 确保已安装适当版本的 JDK 并配置好环境变量。对于新版本的 IntelliJ IDEA 和 Spring Boot,建议使用 JDK 17 或更高版本[^2]。 #### 创建项目 1. 打开 IntelliJ IDEA 后,在欢迎界面点击 **New Project** 或者通过菜单栏选择 `File` -> `New` -> `Project...` 来启动新建项目的向导。 2. 在弹出的新建项目窗口中,左侧列表里找到并选择 **Spring Initializr** 作为模板源。右侧会显示一些默认设置项: - **Name**: 输入项目名称。 - **Location**: 设置保存路径,默认情况下不推荐放在 C 盘下。 - **Type**: 应该自动选择了 Maven。 - **Language**: 确认为 Java。 - **JDK**: 指定之前已经配置好的 JDK 路径,并且确保其版本满足要求 (>= JDK 17)[^3]。 - **Packaging**: 勾选 JAR 文件打包方式。 3. 继续前进至下一个页面,这里可以选择要使用的 Spring Boot 版本以及依赖库。通常可以直接采用最新的稳定版,并勾选所需的模块,比如构建 RESTful API 可能会选择 `Spring Web`。 4. 完成上述操作后,单击 "Finish" 即可开始下载必要的资源并将项目结构搭建起来。 5. 当所有准备工作完成后,IDE 就会在指定目录内生成完整的 Spring Boot 工程文件夹及其内部组件,包括但不限于 pom.xml、src/main/java 下的应用入口类 Application.java 等重要组成部分。 6. 接下来可以在 src/main/java/ 下面按照标准包名格式创建自己的业务逻辑代码包,并在此基础上开发具体的功能实现部分。 ```xml <!-- 示例pom.xml中的部分内容 --> <dependencies> <!-- 引入spring-boot-starter-web来快速启用web功能 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 测试框架 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值