SpringBoot入门

前言

本文记录对于SpringBoot的第一次接触。

一、环境要求

SpringBoot 2.5.5
jdk 1.8+
maven 3.3+

二、Maven配置

  1. 给maven的settings.xml配置文件的profiles标签添加,让Maven使用jdk1.8进行编译,就可以避免项目
    开发中出现的一些问题。
<profile>
   <id>jdk-1.8</id>
   <activation>
 <activeByDefault>true</activeByDefault>
    <jdk>1.8</jdk>
   </activation>
   <properties>
 <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
 <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
   </properties>
</profile>
  1. 添加阿里云镜像,加速jar包的下载速度
<mirror>    
<id>nexus-aliyun</id>    
<mirrorOf>*</mirrorOf>    
<name>Nexus aliyun</name>    
<url>https://maven.aliyun.com/repository/central</url>  
</mirror>

三、创建Maven项目

  1. 配置Maven配置文件(setting.xml)路径和本地仓库路径
  2. 修改pom.xml文件,导入父工程和SpringBoot的Web场景依赖。
    SpringBoot提供了一系列的starters(启动器),用于自动的依赖管理与版本控制。比如要用web
    功能,则导入web启动器,Web应用中需要包含的jar包,以及每个jar包的版本,SpringBoot都可
    以帮我们控制好。
<!--父工程-->
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.5.5</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
<!--web场景依赖-->
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>

四、添加控制器

文件目录:
文件目录
其中Greeting为实体类:

package com.example.demo;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

控制器内容:

package com.example.demo;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/greeting")
    public Greeting greetingGet(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
    @PostMapping("/greeting")
    public Greeting greetingPost(@RequestParam(value = "user", defaultValue = "default") String user,@RequestParam(value = "pwd", defaultValue = "default") String pwd) {
        return new Greeting(3, "Hello SpringBoot!");
    }
}

五、启动SpringBoot应用

注意: SpringBoot的主程序类不能直接放到java包下
目录结构
主程序类:

package com.example.demo;

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

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

六、部署

  1. 在pom.xml文件中添加 spring-boot-maven-plugin 插件
<build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
</build>
  1. 把项目打成jar包或war包
    打成jar包
  2. 通过运行java -jar命令即可开启web服务
  3. 运行结果

响应Get请求
响应Get请求
响应Post请求
响应Post请求

七、总结

SpringBoot相比于SpringMVC来说要简便很多,这是因为SpringBoot自动为项目添加了许多maven依赖,避免了繁琐的人工添加.。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值