SpringBoot的搭建(两种方式)

SpringBoot的搭建

一、简介
特点:springboot简化配置,使项目更简单管理和操作,提高开发效率

二、配置环境要求
依赖springboot2.0以上
jdk8及以上
idea

三、搭建方法两种:
使用maven方式和idea自带的spring initializr创建

其一
使用maven创建:

1 使用intelligent IDEA 创建一个名为SpringBootUnion的maven项目

2 在该项目的pom.xml中导入相关依赖
完整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>org.example</groupId>
    <artifactId>springboot2022</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--SpringBoot启动依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.6.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

3 项目包框架
在项目的src包下的main包中的java包下创建自己的项目结构包,如下
在这里插入图片描述

4 在自己创建的包下创建一个主程序SpringBootUnionApplication.java,用于启动springboot

package com.jwh.springboot.springboot;


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


/**
* @author JING
*/
@SpringBootApplication
public class SpringBootUnionApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootUnionApplication.class,args);
    }
}

5 在controller包里新建一个controller SpringBootUnionController.java,测试springboot启动成功

package com.jwh.springboot.springboot.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
* @author JING
* 此处只能用@RestController注解,不能用@Controller,不然会报错
*/
@RestController
@RequestMapping("/springboot")
public class SpringBootUnionController {
        @CrossOrigin
        @RequestMapping(value = "/test",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public String test(){
            return "springboot install successfully!";
        }
}

6 启动SpringBootUnionApplication.java主程序,
启动成功的界面
在这里插入图片描述

7 使用postman 访问路径为 127.0.0.1:8080/springboot/test
在这里插入图片描述

8 结果返回,项目可以正常使用
在这里插入图片描述

其二
使用spring initializr

1 新建项目 如图选择Spring Initializr 创建
在这里插入图片描述

2 选择web - spring web
在这里插入图片描述

3 选择finish 界面如图
在这里插入图片描述

4 项目包结构
在这里插入图片描述

5 启动项也自动创建好了,编写controller类进行测试,同之前maven创建后的测试一样

结束!

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值