2021-06-29

山东大学 暑期实训 云主机服务比价与预测系统(二)

上一次我们成功配置了MAVEN的环境,同时通过eclipse创建了一个maven工程,这次我们来真正的运行maven程序以体验Spring boot架构的便捷性。

Hello world

首先建立一个 MAVEN工程如下。
在这里插入图片描述
要实现Spring boot的架构,需先修改pom.xml的配置文件,在其中加入下面两项:

下面展示一些 内联代码片

//导入继承一个父工程,即版本为2.3.4的spring-boot-starter-parent
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.3.4.RELEASE</version>
</parent>
//添加依赖,web的场景启动器,将其导入后有关web开发的部分包会自动下载
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>      

然后创建一个主程序类
在这里插入图片描述
下面展示一些 内联代码片

package maventest.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication //标志着这是一个Spring boot应用
//是主程序  所有启动的入口
public class hello {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SpringApplication.run(hello.class, args);
	}}

若run方法报错:
The type org.springframework.context.ConfigurableApplicationContext cannot be resolved

解决方法:启动命令行终端,找到项目所在目录,进入到与pom.xml同一级目录下,执行 mvn dependency:purge-local-repository ,成功后,run的报错问题解决。

再创建一个controller类
在这里插入图片描述

package maventest.test.controller;

import javax.xml.ws.RespectBinding;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController //说明其是一个Controller类
public class hellocontroller {
	@RequestMapping("/hello")//设置访问路径
	public String hello(){
		return "hello Spring boot !"; //作出回应
	}
}

运行hello主程序
在这里插入图片描述
观察到Spring boot的启动标志和一系列变量数据,
进入浏览器访问 localhost:8080/hello
在这里插入图片描述
可以看到hello程序运行成功。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值