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.mzph.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…