IDEA社区版使用Maven archetype 创建Spring boot 项目

发布于:2024-07-14 ⋅ 阅读:(136) ⋅ 点赞:(0)

1.新建new project

2.选择Maven Archetype

3.命名name

4.选择存储地址

5.选择jdk版本

6.Archetype使用webapp

7.create创建项目

创建好长这样。

检查一下自己的Maven是否是自己的。

没问题的话就开始增添java包。

[有的人连resources包也没有,那就需要自己添加,创建好resources包后右键,然后Mark Directory as 选择Test Resources Root就好了]

建好后长这样。

再在java包下面建com.test包,com.test包下再建Start主类和controller包,以及在controller包下创建TestController类。

再在这之后配置pom.xml文件。

[这里只是最简配置]

1.

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

2.

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

配置好后点一下就好了

再编写Start主类代码

package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Start {
    public static void main(String[] args) {
        SpringApplication.run(Start.class,args);

    }
}

再编写TestController代码

package com.test.controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;

@RestController
public class TestController {
    @RequestMapping("/index")
    public String index(Model model, HttpServletResponse response) {
        return "hello spring boot index";
    }
}

最后执行Start主类就好了

运行结果如图就是好了

备注:

有些人这里不行

解决如下:

如果还不行,检查一下你主类的代码是不是少写了什么,比如

@SpringBootApplication

如果最后主类运行没有问题了但是显示红色,你就如下操作

点一下add+就不显示红色了。


网站公告

今日签到

点亮在社区的每一天
去签到