第一个SpringBoot程序

第一个SpringBoot程序

环境:

  • Maven 3.6.0
  • jdk1.8
  • springboot
  • IDEA

官方:提供了一个快速生成springboot的网站!IDEA集成了这个网站

  • SpringBoot中可以快速生成,之后下载一个压缩包,之后IDEA导入就可以了,一个SpringBoot程序生成了

  • IDEA创建SpringBoot程序==(推荐)==

    • New Project -> springboot initializr (不是Maven项目) -> 之后next -> dependencies(这个选择Web->spring web) 配置完成
    • 分析初始项目的结构
      在这里插入图片描述
      创建的包的规则

在这里插入图片描述
在controller下面建立一个类,这个直接返回值就行,不需要创建了一个对应的页面

package com.example.springboot01.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//本身就是SpringBoot的一个组件
@Controller
@RestController
public class HelloController {


    //接口:http://localhost:8080/hello
    @RequestMapping("/hello")
    public String hello(){
        //调用业务,接受前端的参数
        return "hello";
    }

}

在这里插入图片描述

pom.xml中依赖的作用

<!-- 父依赖 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath/>
</parent>

<dependencies>
    <!-- springboot核心依赖,包括tomcat,xml,dispatcherServlet。。。。 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- springboot自带测试类,当然用junit也可以 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<!-- 打包插件 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

如何打jar包:

在这里插入图片描述

若是遇见错误可以使用以下的代码

<!--
    在工作中,很多情况下我们打包是不想执行测试用例的
    可能是测试用例不完事,或是测试用例会影响数据库数据
    跳过测试用例执
    -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <!--跳过项目运行测试用例-->
        <skipTests>true</skipTests>
    </configuration>
</plugin>

如果打包成功,则会在target目录下生成一个 jar 包

在这里插入图片描述

在application.properties中更改服务器的端口号:

server.port:8081
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值