Springboot的入门1-None-Layer

Spring Boot不是一门新技术, 基于Spring framework。使用“习惯优于配置” , “开箱即用”,

只需要很少的Spring配置,就可构建一套 Web 项目,Restful API项目或者微服务。


​​​​​​​

动手实践:

创建 Application.java,其注解 @SpringBootApplication 表示这是一个SpringBoot应用,运行其主方法main就会启动tomcat,默认端口是8080

@SpringBootApplication:标识Spring Boot Application Class的Annotation

• 触发Spring的自动配置

• 触发Component Scan自动加载Controller等

main方法中的SpringApplication.run(MyTestApplication.class, args);

就是让MyTestApplication.class跑起来

具体操作:

启动应用服务器 • 选中SimpleApplication ->点击鼠标右键-> 选择Run As -> 选择Java Application

控制台中出现:Tomcat started on port(s): 8080 (http) with context path 

package com.example.myTest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;



@SpringBootApplication
public class MyTestApplication {

	
	public static void main(String[] args) {
		SpringApplication.run(MyTestApplication.class, args);
	}

}

接着创建控制器类HelloController, 这个类就是Spring MVC里的一个普通的控制器。
@RestController 是spring4里的新注解,是@ResponseBody和@Controller的缩写。

@RestController:标识Restful API Controller Class的Annotation.

其内方法:

• 接受HTTP Request

• 产生HTTP Response

@GetMapping:标识该方法将接受HTTP Request GET请求。/hello为对象资源的URL

要注意HelloController记得放在Application同一个目录下,就是说将Application类放在上一级包里。比如com.xxx.controller里放控制器,com.xxx里放Application类

如下图 Application类@SpringBootApplication 在控制器@RestController的上级目录

package com.example.myTest.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 Spring Boot!";
    }
 
}

接下来就运行程序, 然后访问地址http://localhost:8080/hello

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值