用IDEA创建Spring框架的maven项目

2 篇文章 0 订阅
2 篇文章 0 订阅

用IDEA创建Springboot框架的maven项目,并建立spring的Hello world程序

作为一个跨专业考研上岸的计算机小白,最近正在导师公司作为一名java开发工程师学习,所以发文记录一下自己的零基础学习java的心得呀,如果有问题大家快来纠正我。
首先点开IDEA,新建一个项目,点击file中的new选中new project

在这里插入图片描述
选择Spring Initializr,记得选择好Java的版本,idea里可以自动导入spring的各种包,就不用特地去spring官网下载spring的包啦
在这里插入图片描述
最后记得在settting中重新设置一下Maven,选用自己在Maven官网上下载的maven
在这里插入图片描述
设置好后,根据spring官网文档的教程,我自己建立了一个简单的hello world的spring文件

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class,args);
    }
    @GetMapping("/hello")
    public String hello(@RequestParam(value="name",defaultValue = "World")String name){
        return String.format("Hello %s!",name);
    }
}

具体的操作步骤可以参见官网
https://spring.io/quickstart
官网对这段代码的解释是
The @RestController annotation tells Spring that this code describes an endpoint that should be made available over the web.
The @GetMapping(“/hello”) tells Spring to use our hello() method to answer requests that get sent to the http://localhost:8080/hello address.
Finally, the @RequestParam is telling Spring to expect a name value in the request, but if it’s not there, it will use the word “World” by default.
这里@RestController是一个注解告诉Spring这个代码可以连接到一个端口,而@GetMapping是表示可以连接到http://localhost:8080/hello 这个地址,当然我们也可以设置@GetMapping(“/hh”)连接到http://localhost:8080/hh,这里是可以任意设置的。
最后@RequestParam告诉spring这里显示的hello %s中的name默认为World。
运行后就可以在对应网址看到Hello World啦,那么如何设置Hello %s中的name呢只需要http://localhost:8080/hello?name=User,就可以显示Hello User啦,你也可以设置别的来显示。
这篇就先记录到这里啦。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值