Java教程

第一个SpringBoot程序

本文主要是介绍第一个SpringBoot程序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

创建的两种方法:


  • 1.可以从springboot官网创建下载,再在idea中打开

  • 2.idea中集成了这个网站,注解使用idea创建一个springboot项目(一般用这种)


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 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.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.kakafa</groupId>
    <artifactId>helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>helloworld</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--web依赖:tomcat,dispatcherServlet,xml等-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <!--所有的springBoot依赖名字都是以spring-boot-starter开头的-->
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <!--打jar包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


编写http接口

package com.kakafa.helloworld.controller;

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

@RestController
public class HelloController {

    //接口 http://localhost:8080/hello
    @RequestMapping("/hello")
    public String hello(){

        return "helloworld!";
    }
}

打包

运行测试后可用即可打包

打包成功,这个jar包就是一个接口程序(是一个可执行程序)

执行该jar包试试看:



jar包和war包的区别

参考链接:https://www.cnblogs.com/banml/p/11767305.html





这篇关于第一个SpringBoot程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!