快速搭建springboot项目(新手入门)

 更新时间:2023年07月12日 15:39:19   作者:弱水三千只取一瓢编号880908  
本文主要介绍了快速搭建springboot项目(新手入门),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、创建项目

1.1、创建项目

1.2、配置编码

1.3、取消无用提示

1.4、取消无用参数提示

二、添加POM父依赖

<!-- 两种方式添加父依赖或者import方式 -->
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.5.7</version>
</parent>

maven的pom文件手动更新

添加完成maven的pom文件之后,会自动更新,也可能不会自动更新,那么我们需要手动更新它。

三、支持SpringMVC

<dependencies>
    <!-- 支持SpringMVC -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

四、创建启动类、rest接口

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StartApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);
    }
}

五、配置插件

配置完成后,maven打包可以生成可执行jar文件

<build>
  <plugins>
      <!-- 打包成可执行jar -->
      <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <!-- 配置跳过测试 -->
      <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <configuration>
              <skip>true</skip>
          </configuration>
      </plugin>
      <!-- 配置jdk版本11 -->
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
              <source>11</source>
              <target>11</target>
              <encoding>utf-8</encoding>
          </configuration>
      </plugin>
  </plugins>
</build>

六、添加thymeleaf模板

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

七、添加配置

在resources文件夹下,创建application.properties

在resources文件夹下,创建templates文件夹

# 应用名称
spring.application.name=thymeleaf
# 应用服务 WEB 访问端口
server.port=8080
# THYMELEAF (ThymeleafAutoConfiguration)
# 开启模板缓存(默认值: true )
spring.thymeleaf.cache=false
# 检查模板是否存在,然后再呈现
spring.thymeleaf.check-template=true
# 检查模板位置是否正确(默认值 :true )
spring.thymeleaf.check-template-location=true
#Content-Type 的值(默认值: text/html )
spring.thymeleaf.content-type=text/html
# 开启 MVC Thymeleaf 视图解析(默认值: true )
spring.thymeleaf.enabled=true
# 模板编码
spring.thymeleaf.encoding=UTF-8
# 要被排除在解析之外的视图名称列表,⽤逗号分隔
spring.thymeleaf.excluded-view-names=
# 要运⽤于模板之上的模板模式。另⻅ StandardTemplate-ModeHandlers( 默认值: HTML5)
spring.thymeleaf.mode=HTML5
# 在构建 URL 时添加到视图名称前的前缀(默认值: classpath:/templates/ )
spring.thymeleaf.prefix=classpath:/templates/
# 在构建 URL 时添加到视图名称后的后缀(默认值: .html )
spring.thymeleaf.suffix=.html

八、添加controller

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class IndexController {
    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }
}
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@RestController
public class HelloController {
    @GetMapping("/Hello")
    public String Hello(){
        return "haha";
    }
}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * rest测试controller
 */
@RestController
public class RestIndexController {
    @GetMapping("/restIndex")
    public String index(){
        return "rest";
    }
}

九、添加html

在templates下创建index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
index
</body>
</html>

十、访问

需要maven执行编译,否则容易404

http://localhost:8080/index

到此这篇关于快速搭建springboot项目(新手入门)的文章就介绍到这了,更多相关搭建springboot项目内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java如何写接口给别人调用的示例代码

    java如何写接口给别人调用的示例代码

    这篇文章主要介绍了java如何写接口给别人调用的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • 浅谈SpringBoot实现异步调用的几种方式

    浅谈SpringBoot实现异步调用的几种方式

    本文主要介绍了浅谈SpringBoot实现异步调用的几种方式,主要包括CompletableFuture异步任务,基于@Async异步任务, TaskExecutor异步任务,感兴趣的可以了解一下
    2023-11-11
  • java IO流文件的读写具体实例

    java IO流文件的读写具体实例

    这篇文章主要介绍了java IO流文件的读写具体实例,有需要的朋友可以参考一下
    2013-12-12
  • MyBatis-Plus:saveOrUpdate根据指定字段更新或插入方式

    MyBatis-Plus:saveOrUpdate根据指定字段更新或插入方式

    这篇文章主要介绍了MyBatis-Plus:saveOrUpdate根据指定字段更新或插入方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-04-04
  • Java的System.getProperty()方法获取大全

    Java的System.getProperty()方法获取大全

    这篇文章主要介绍了Java的System.getProperty()方法获取大全,罗列了System.getProperty()方法获取各类信息的用法,具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-12-12
  • Java中自定义注解类及使用实例解析

    Java中自定义注解类及使用实例解析

    这篇文章主要介绍了Java中自定义注解类并使用过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • IDEA启动Tomcat时控制台出现乱码问题及解决

    IDEA启动Tomcat时控制台出现乱码问题及解决

    这篇文章主要介绍了IDEA启动Tomcat时控制台出现乱码问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • SpringBoot如何使用validator框架优雅地校验参数

    SpringBoot如何使用validator框架优雅地校验参数

    文章介绍了如何使用SpringValidation进行参数校验,包括引入依赖、@requestBody和@requestParam参数校验、统一异常处理、分组校验、嵌套校验、自定义校验、业务规则校验以及@Valid和@Validated的区别,同时,列举了常用的BeanValidation和HibernateValidator注解
    2025-02-02
  • SpringBoot生成PDF的五种实现方法总结

    SpringBoot生成PDF的五种实现方法总结

    这篇文章主要介绍了SpringBoot生成PDF的五种实现方法,在开发中经常会遇到需要进行对一些数据进行动态导出PDF文件,然后让用户自己选择是否需要打印出来,这篇文章我们来介绍五种实现方法,需要的朋友可以参考下
    2024-10-10
  • Java中队列(Queue)和列表(List)的区别解析

    Java中队列(Queue)和列表(List)的区别解析

    Java中的列表(List)和队列(Queue)是两种常用的数据结构,它们分别用于不同的场景,列表是有序的,支持随机访问,允许重复元素,并且可以通过索引插入或删除元素,下面通过本文给大家介绍Java中队列(Queue)和列表(List)的区别,感兴趣的朋友一起看看吧
    2025-03-03

最新评论