SpringBoot入門 (一) HelloWorld

一 什么是springboot

  springboot是一個全新的框架,它設計的目的簡化spring項目的初始環境的搭建和開發,主要有以下幾個特點:

  1、簡化初始配置 ,可與主流框架集成;

  2、內置Servlet容器,無需在打War包;

  3、使用了Starter(啟動器)管理依賴并版本控制;

  4、大量的自動配置,簡化開發,方便集成第三方;

  5、提供準生產環境運行時的監控,如指標,健康,外部配置等;

  6、無需XML配置,減少冗余代碼 。

  未使用springboot時,如果我們要搭建一個springweb項目環境,我們需要配置web.xml及各種xml的配置文件來集成其他第三方的框架,而這些springboot已經幫我們做了集成,我們不再需要去配置。

二 入門實例

  創建springboot項目有2中方式,1種是通過ide來創建,一種是官方網站創建(https://start.spring.io創建完成后會將工程下載到本地)然后導入ide即可,本文我們通過idea創建項目。

  1 在idea的工具欄 file-->new project 如下圖

會彈出選擇工程類型的框,如下圖

這個時候我們有2種選擇,一種是使用Spring Initializr創建,一種是使用Maven或者Gradle創建。這兩種方式的不同的地方是使用方法二創建的是空項目,我們需要自己去修改添加所需要的依賴,方式一在創建的過程中,我們可以在ide中直接選擇需要的依賴且會生成一個項目的根啟動類。本文我們使用Spring Initializr來創建項目。點擊上圖的Next

輸入Group、Artifact、Package的對應的信息,再點擊next

這是我們可以選擇我們的項目要運行的springboot的版本和需要的依賴包(本文我們只選擇依賴web),然后點擊Next

這是彈框會顯示我們項目的名稱及項目的路徑,點擊Finish。初始時會下載springboot默認依賴的一些jar包,需要一會時間,我們等待它下載完成。之后我們可以看到項目的結構

pom.xml內容如下,2.0.8版本不是一個發布版本,在我們的實際項目中,最好還是要引用release版本的。

<?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.allen.demo</groupId><artifactId>springboot-helloworld</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springboot-helloworld</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.8</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</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></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

生成的啟動類,一個可執行的main方法。

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

默認在類上邊使用了@SpringBootApplication注解,這個注解是一個符合注解,相當于同時使用

@SpringBootConfiguration  指定類為配置類
@EnableAutoConfiguration  開啟自動配置
@ComponentScan 指定掃描路徑

下來創建一個可以訪問的控制器,使用@RestController注解,它是一個復合注解,相當于同時使用了@Controller和@ResponseBody注解

package org.wl.demo.web;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@RequestMapping("/hello")public String hello(){return "hello world";}}

執行main方法,啟動項目,在控臺可可以看到啟動的日志信息

[           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
[           main] org.wl.demo.HelloWorldApplication     : Started HelloWorldApplication in 3.591 seconds (JVM running for 4.784)

內嵌的tomcat服務器已經啟動,啟動端口是8080,更路徑是'',我們訪問控制器的hello方法

這樣一個web項目環境就搭建成功了,使用起來還是很簡單方便的。

在上邊的日志信息中,默認啟動端口是8080,訪問根路徑是‘’,如果我們希望修改的話也是可以的,只需要在application.properties中修改

修改端口,從默認的8080修改為8090

server.port=8090

修改根路徑 修改前是‘’,修改后為‘/helloworld’

server.servlet.context-path=/helloWorld

?

轉載于:https://www.cnblogs.com/love-wzy/p/10303697.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278421.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278421.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278421.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

gmail附件調用_如何將Gmail附件保存到Google云端硬盤

gmail附件調用While you can access Gmail attachments by opening the related message deep within Google’s client, it’s not very convenient. You need a central location to access saved documents and images. This guide shows you how to save Gmail attachments…

spring boot攔截器中獲取request post請求中的參數(轉)

文章轉自 https://www.jianshu.com/p/69c6fba08c92 轉載于:https://www.cnblogs.com/shuaiandjun/p/10306242.html

絕地求生大逃殺,改配置

提取效果設置配置文件 通過Procmon工具分析&#xff0c;絕地求生大逃殺效果設置的配置文件為 “C:\Users\Administrator\AppData\Local\TslGame\Saved\Config\WindowsNoEditor\GameUserSettings.ini”&#xff0c;設置好網吧需要的游戲效果后將“TslGame”文件夾提取出來即可&a…

如何使用VLOOKUP在Google表格中查找數據

VLOOKUP is one of the most misunderstood functions in Google Sheets. It allows you to search through and link together two sets of data in your spreadsheet with a single search value. Here’s how to use it. VLOOKUP是Google表格中最容易被誤解的功能之一。 它使…

共享內存

https://blog.csdn.net/tojohnonly/article/details/70246965 轉載于:https://www.cnblogs.com/132818Creator/p/10307072.html

WPF項目學習.一

WPF項目搭建 版權聲明&#xff1a;本文為博主初學經驗&#xff0c;未經博主允許不得轉載。 一、前言 記錄在學習與制作WPF過程中遇到的解決方案。 使用MVVM的優點是 數據和視圖分離&#xff0c;雙向綁定&#xff0c;低耦合&#xff0c;可重用行&#xff0c;相對獨立的設計和邏輯…

airpods_如何通過AirPods與其他人共享音樂

airpodsKhamosh PathakKhamosh PathakUsing the new Audio Sharing feature introduced in iOS 13.1 and iPadOS 13.1, you can share audio from one iPhone with two AirPods. You can watch a video or listen to a song along with your friend in just a tap! 使用iOS 13.…

Laravel 5 多個視圖共享數據的方法

我們都知道模板一般會用到繼承&#xff0c;導航欄就是一個很好的例子&#xff0c;但是導航欄的數據如何共享&#xff0c;比如有個導航的文件叫在view/navigation.blade.php為了簡單一點&#xff0c;文件里只有設置了一個變量1{{ $cqh }}現在的要求是每個頁面都會用到這個變量&a…

HR面 - 十大經典提問

1、HR&#xff1a;你希望通過這份工作獲得什么&#xff1f; 1&#xff09;、自殺式回答&#xff1a;我希望自己為之工作的企業能夠重視質量&#xff0c;而且會給做得好的員工予以獎勵。我希望通過這份工作鍛煉自己&#xff0c;提升自己的能力&#xff0c;能讓公司更加重視我。 …

谷歌云使用賬號密碼_如何使用Google密碼檢查

谷歌云使用賬號密碼Google has a tool designed to securely analyze your passwords against a database of ones that are known to be compromised and breached. Password Checkup is available as an extension or a web service. Here’s how to use it. Google提供了一種…

HTML特殊字符編碼對照表

HTML特殊字符編碼對照表 特殊符號命名實體十進制編碼特殊符號命名實體十進制編碼特殊符號命名實體十進制編碼Α&Alpha;Β&Beta;Γ&Gamma;Δ&Delta;Ε&Epsilon;Ζ&Zeta;Η&Eta;Θ&Theta;Ι&Iota;Κ&Kappa;Λ&Lambda;Μ&Mu;Ν&a…

CentOS 7.0下使用yum安裝MySQL

CentOS7默認數據庫是mariadb,配置等用著不習慣,因此決定改成mysql,但是CentOS7的yum源中默認好像是沒有mysql的。為了解決這個問題&#xff0c;我們要先下載mysql的repo源。1.下載mysql的repo源$ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm2.安裝my…

Jolicloud是一款適合上網本的漂亮新操作系統

Want to breathe new life into your netbook? Here’s a quick look at Jolicloud, a unique new Linux based OS that lets you use your netbook in a whole new way. 想為您的上網本注入新的活力嗎&#xff1f; 快速瀏覽一下Jolicloud&#xff0c;這是一個獨特的基于Linu…

Repeater片段

1.字段過長截取字符串 1.1 截取字符串類 可以直接substring 也可以<%# Utility.Common.GetShow( Eval("NewTitle").ToString(),20,true) %><td><%#fcwms.Common.GetContent.GetShow(Eval("com_address").ToString(), 19, true)%> </t…

谷歌瀏覽器的翻譯功能在哪_如何在Google表格中使用AND和OR功能

谷歌瀏覽器的翻譯功能在哪If you’ve ever wanted to check whether data from your Google Sheets spreadsheet meets certain criteria, you can use AND and OR. These logical functions give you TRUE and FALSE responses, which you can use to sort through your data.…

Reptile:requests + Xpath 爬取段子網的段子

2019/1/24 中午路飛學成 爬蟲課程 實驗及筆記。 Xpath是路飛爬蟲課程中老師說的三種解析方式之一&#xff0c;前面是re正則表達式的解析方式&#xff0c;現在是xpath的解析方式&#xff0c;后面還有一個是bs4的解析方式。 re其實我理解的很困難&#xff0c;而且到現在都還不怎么…

Android 系統權限

Android 是一個權限分隔的操作系統&#xff0c;其中每個應用都有其獨特的系統標識&#xff08;Linux 用戶 ID 和組 ID&#xff09;。系統各部分也分隔為不同的標識。Linux 據此將不同的應用之間、應用與系統之間分隔開來 ##一、安全架構 Android 安全架構的中心設計點是&#x…

【轉載】負數的二進制

https://jingyan.baidu.com/article/29697b9106eb52ab21de3c7a.html 將十進制的負數變成二進制數的過程&#xff1a; 1.寫出絕對值的二進制碼&#xff08;原碼&#xff09; 2.取反&#xff08;反碼&#xff09; 3.1,&#xff08;補碼&#xff09; 同理&#xff0c;將二進制的負…

保存網絡文章以供以后使用Instapaper閱讀

Have you ever come across a bunch of great articles that you want to read online, but just don’t have the time? Today we take a look at an online service that allows you to read your articles later, either online, or on an iPhone, or eReader. 您是否曾經遇…

谷歌chrome xp_將非Google任務列表添加到Chrome

谷歌chrome xpMost people rely on a task list to help them remember what they need to do but not everyone wants one that is tied to a Google account. If you have been wanting an independent tasks list then join us as we look at the Tasks extension for Googl…