06-SpringBootWeb快速入门

 

1.创建springboot模块,勾选web开发相关依赖

2. 配置pom.xml文件,并通过maven加载相关依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.itheima</groupId>
    <artifactId>springboot-web-quickstart</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-web-quickstart</name>
    <description>springboot-web-quickstart</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>rg.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.7.10</version>
        </dependency>
    </dependencies>

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

</project>

3. SpringbootWebQuickstartApplication.java启动文件

package com.itheima;

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

@SpringBootApplication
public class SpringbootWebQuickstartApplication {

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

}

4. 定义HelloWorld.java类,在浏览器展示hello world

package com.itheima.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

// @RestController注解表示该类下的方法返回的数据直接写给浏览器(如果是对象转为json数据)
@RestController
public class HelloWorld {
    // @RequestMapping注解表示该方法对应的url
    @RequestMapping("/hello")
    public String hello() {
        System.out.println("物联网,根据设备编码获取所有属性接口");
        return "hello world quickstart!";
    }
}

测试接口:http://localhost:8080/hello

 

 

posted @ 2024-04-08 17:08  马铃薯1  阅读(1)  评论(0编辑  收藏  举报