Day01 SpringBoot 第一个程序创建,以及使用

Springboot介绍:

(1)它是Spring的升级版,Spring容器能做到的事情,它都能做到,而且更简便,从配置形式上来说,SpringBoot完全抛弃了繁琐的XML文件配置方式,而是替代性地用注解方式来实现,虽然本质来说,是差不多的(类似包扫描,注解扫描,类加载之类)。
(2)SpringBoot集成的插件更多,从而使用很多服务,都只是引入一个依赖,几个注解和Java类就可以用了,具体的参考相关手册。
(3)在Web应用开发这一块,之前的应用一般来说是打包成war包,再发布到相关服务器容器下(例如Tomcat),虽然SpringBoot也可以这么做,但在SpringBoot下更常见的形式是将SpringBoot应用打包成可执行jar包文件。之所以这么做,源于你可以直接将SpringBoot应用看成是一个Java Application,其Web应用可以没有webapp目录(更不用说web.xml了),它推荐使用html页面,并将其作为静态资源使用

创建Springboot第一个程序

创建Springboot有两种方式:

  • 一种是在官网上选择自己所需要的版本信息,下载下来,再导入到IDEA或者Eclipse
    官网:http://start.spring.io/
    官网截图

    1. 选择什么类型,什么语言的以及什么版本的project
    2. 填写Group,Artifact项目名,以及需要增加的依赖
    3. 最后直接点击Generate Project 就可以下载自己已经定义好的springboot项目。
    4. 导入自己的编程工具。
  • 另一种是在自己的编译工具中选择信息直接创建,(此处我使用的是IDEA)

    1. File -> new -> Project
      创建
      左侧选择Spring Initializr (小树叶), 右侧SDK选择自己的版本号, Default若默认是http://start.spring.io/,则不需要做改动,,若默认不是此网址,则选择Custom,右侧写上网址,最后点击Next
      2.这里写图片描述
      此页配置与官网配置相似,自己选择,自己填写
      Next->增加依赖以及版本号,Next->选择存放路径 ->Finish

检查pom.xml文件中是否有springboot的依赖,若没有,则添加

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

启动Springboot程序

在java下的包下,创建HelloController,以下为内容:

package com.demo.springbootdemo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/*
@RestController  等同于 @Controller和@ResponseBody
 */
@RestController
public class HelloController {
    //写成集合,代表访问哪个url都都可以访问到这段代码
    @RequestMapping(value={"/hello", "/hi"},method= RequestMethod.GET)
    public String say(){
        return "Hello Spring Boot";
    }
}

1.右键点击java下自定义包下*Application ,选择运行,则启动springboot程序。
类中内容为:

package com.demo.springbootdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*springBoot的启动类
,运行此类,
则可以启动springboot
* */
@SpringBootApplication
public class SpringbootdemoApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringbootdemoApplication.class, args);
    }
}

springBoot的启动类,运行此类,则可以启动springboot
2.在网址输入:localhost:8080/url ,controller配置的url,此处为localhost:8080/hello ,或者localhost:8080/hi ,将会输出返回内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值