创建springboot_【小练习】创建SpringBoot程序

Maven工程在配置文件中添加springboot相关依赖,就能使用注解快速创建springboot工程,生成服务端应用。创建hello world程序查看效果。

目录

  • 程序效果

  • 配置文件修改

  • 注解使用

  • 参考资料

程序效果

75a6874eff534652205f6c82f3694f53.png

截图1.访问URL显示字符 

内置tomcat服务器默认使用8080端口,添加程序中配置的“/hello”后缀能访问hello()方法。

配置文件修改

pom.xml配置文件中添加org.springframework.boot依赖,会自动下载相关的jar包。

<parent>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-parentartifactId>

<version>1.5.9.RELEASEversion>

parent>

<dependencies>

<dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-webartifactId>

dependency>

<dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-testartifactId>

dependency>

dependencies>

注解使用

4c8fa2b0c8b736d2a4ee20a821cf47b7.png

截图2.程序目录 

仅实现2个文件,Application.java文件启动服务,Controller.java文件添加URL请求映射关系。

@SpringBootApplication

public class SpringBootDemoApplication {

public static void main(String[] args) {

SpringApplication.run(SpringBootDemoApplication.class, args);

}

}

@SpringBootApplication是Sprnig Boot项目的核心注解,目的是开启自动配置。

@RestController

@RequestMapping("/hello")

public class Controller {

@RequestMapping("")

public String hello() {

return "hello world";

}

}

@RestController声明该类是一个控制器,@RequestMapping声明映射关系匹配“/hello”,URL访问“localhost:8080/hello”调用hello()返回字符串“hello world”。

参考资料

1.使用eclipse创建Spring Boot项目 https://www.cnblogs.com/txsblog/p/8566416.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值