栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springBoot学习笔记(1)—— 搭建springBoot项目

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springBoot学习笔记(1)—— 搭建springBoot项目

springBoot学习系列笔记文章

springBoot学习笔记(1)—— 搭建springBoot项目


文章目录
  • springBoot学习系列笔记文章
  • 一、搭建项目
    • 1.步骤说明
    • 2.步骤截图
  • 二、项目代码
    • 1.引入jar包
    • 2.java代码
    • 3. 运行截图

说明:此次项目的构建都是基于IDE软件。

一、搭建项目 1.步骤说明
  1. 点击“File”->“New”->“Module”。
  2. 选择"Spring Initializr",选择JDK8环境,点击“Next”
  3. 填入项目名Name,这里我填写的是springbootdemo,选择Java Version为8。
  4. Web中引入Spring Web的jar包。
2.步骤截图


二、项目代码 1.引入jar包

代码如下(示例):



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.2
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    springbootdemo
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



2.java代码

代码如下(示例):

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@SpringBootApplication
@Controller
public class SpringbootdemoApplication {

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

    
    @ResponseBody
    @GetMapping("test")
    public String test(){
        return "这是一次测试";
    }


    
    @ResponseBody
    @PostMapping("testVariable")
    public  String testVariable(String message){
        return  "你输入的信息为:" +  message;
    }

    
    @GetMapping("testPathVariable/{id}")
    @ResponseBody
    public String testPathVariable(@PathVariable(value = "id") String id){
        return "传入变量id为:" + id;
    }

}

代码说明

  1. @ResponseBody表示返回的数据为JSON格式,防止乱码。
  2. @SpringBootApplication注解是springboot的核心注解,目的是开启注解配置。
  3. @SpringBootApplication注解包含@ComponentScan,@EnableAutoConfiguration,@SpringBootConfiguration ,@Inherited 四个注解。
  4. @PathVariable标签必须和@ResponseBody配合使用,不然容易引起异常,或者在控制层使用@RestController标签,两者效果一致。

3. 运行截图



项目源码

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/681027.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号