Springboot第一课——快速搭建web

SpringBoot 的概念什么的,这里就不赘述了,官方文档最齐全。

下面开始搭建第一个SpringBoot Web,这里我是使用maven来构建项目的:

1.pom文件引入spring-boot-starter-parent和springboot-web 依赖

<!--这个是必须引入的-->
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.4.RELEASE</version>
	<relativePath /> 
</parent>

引入web依赖:


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

 

 2.在src/main/java 下创建com.controller包,包里创建我们的RestController类(这里的controller相当于struts里面的action):

package com.controller;

import org.springframework.web.bind.annotation.RequestMapping;
// restful 风格的controller类,类里方法返回的都是json格式
@org.springframework.web.bind.annotation.RestController
public class RestController {
    @RequestMapping("/hello")
	public String helloSprinBoot() {
		return "helloSprinBoot";
	}
}

3.启动项目的主方法,在src/main/java下创建com.app包,包下创建APP类,如下:

package com.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
// 扫描的包
@ComponentScan(basePackages="com.controller")
@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

由于springboot内嵌了tomcat,不用我们再去集成tomcat直接运行这个主方法,项目就跑起来了,非常简单方便!下面来看看效果:

跑起来后控制台如下:

我们来用浏览器访问一下看看效果:

至此就成功搭建了简单的web服务

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值