Spring Boot学习教程-----3.Spring boot 第一个web项目

1.创建一个maven项目(hello world)

打开IDEA

导入springboot的父工程

2.引入依赖,web-starter场景启动器

<?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>HelloWorld</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
</project>

 创建一个类,在一个包下,本文的类名为MainApplication

package com.ch.boot;

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

/*
主程序类,所有启动的入口
@SpringBootApplication:这是一个springboot应用
*/
@SpringBootApplication
public class MainApplication {

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

创建其他的controller

package com.ch.boot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/*@ResponseBody:代表直接写给浏览器,而不是跳转到某个页面
@Controller*
RestController :是上面两个注解的合体
/
 */
@RestController
public class HelloController {


    @RequestMapping("/hello")
    public String handler01() {
        return "Hello Spring boot";
    }


}

然后直接运行MainApplication里面的main方法即可 ,启动效果图如下,即为启动成功

 然后在浏览器访问localhost:8080/hello,即可看到运行结果

从一个这样的小应用就看出了很多

我们不用像以前一样,需要配置很多的xml文件,甚至连tomcat都不用安装,是不是特别方便

然后所有的修改原来配置都可以放在一个文件,application.properties

修改tomcat端口,所有的配置都可以参考官方开发文档:

https://docs.spring.io/spring-boot/docs/2.4.4/reference/html/appendix-application-properties.html#common-application-properties

 部署web应用,不需要像以前一样达成war包再放到tomcat里面运行,只需使用springboot的插件打成jar,

这个jar包含了所有运行环境

引入插件,在pom.xml中添加

  <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

maven package

然后进去项目目录的target目录,运行 java -jar jar包名称

在访问localhost:8888/hello,出现之前hello springboot 说明打包运行成功

注意点:取消cmd的快捷编辑模式

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值