首页 > 其他分享 >写一个 Hello SpringBoot2 项目

写一个 Hello SpringBoot2 项目

时间:2023-01-16 20:44:57浏览次数:39  
标签:MainApplication RestController 项目 Hello Controller SpringBoot2 helloSpringBoot2 

 项目目录:helloSpringBoot2、MainApplication、pom.xml

 

 

helloSpringBoot2 逻辑类,标记 @RestController

/*
@RestController的作用等同于@Controller + @ResponseBody
1)
@Controller注解可以将该类交由spring管理,从而能够使用getBean()
或者@Autowired的方式从Spring中获取Bean实例。
@Controller标识的类,在语义上,表示该类代表控制器类。

2)@responseBody
@responseBody注解的作用是将controller的方法返回的
对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,
通常用来返回JSON数据或者是XML数据。
 */

@RestController
public class helloSpringBoot2 {
    @RequestMapping("/hello")
    public String sayHello(){
        return "hello SpringBoot2";
    }
}

  主类 MainApplication,用于运行

/**
 * @SpringBootApplication 标记为SpringBoot应用
 */

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

  pom.xml 只需要引入web依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>helloSpringBoot2</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

<!--        引入web依赖   -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

  用浏览器访问localhost:8080/hello,结果如下:

 

 

标签:MainApplication,RestController,项目,Hello,Controller,SpringBoot2,helloSpringBoot2,
From: https://www.cnblogs.com/Kaelthas/p/17056262.html

相关文章

  • 【Java】eclipse如何导入项目
    【Java】eclipse如何导入项目1.第一步,打开eclipse,点击file->import2.第二步,选择general->existingprojectsintoworkspace(即常规-现有项目导入到工作空间)3.第三步,选择bro......
  • 8.一个项目实战(下载博客文章)
    专栏地址ʅ(‾◡◝)ʃ前言要写一个下载器,首先要实现一个接口函数,而这个函数可以对请求的数据进行处理也就是爬虫,其次才是写图形化界面接口的实现其实CSDN的浏览器页面的......
  • hbuilderx 项目上传至git 私库 gitea20220908
    1、搭建git私库[gitea]   2、hbuilderx 新建项目oapm  3、初始化的项目本地仓库:项目oapm进入项目本地文件夹根目录右键GitBash,输入入命令:gitinit   项目本地......
  • 01:Hello, World!
    ​​原题链接​​总时间限制:1000ms内存限制:65536kB描述对于大部分编程语言来说,编写一个能够输出“Hello,World!”的程序往往是最基本、最简单的。因此,这个程序常常作......
  • 一个项目 多个拦截器
    1. addInterceptors方法中加多条   @ConfigurationpublicclassResourcesConfigimplementsWebMvcConfigurer{@OverridepublicvoidaddInterce......
  • HelloWorld详解
    1、新建一个文件夹名为Code用以存放代码,2、在文件夹中新建文件,后缀名为.java起名为Hello.java3、然后用Notepad++打开进行编写如下代码publicclassHello{ publics......
  • 项目和产品的区别
    做产品和做项目的区别 注:本文转载:做产品与做项目的区别-数通畅联-博客园(cnblogs.com) 1 背景概述在软件行业发展的今天,我们将软件公司分大体分为两类,一......
  • React Hook 系列(三):记一次中台项目的Hook沉淀
    ReactHook系列(三):记一次中台项目的Hook沉淀郭盖_2020年11月17日20:32 ·  阅读1891背景本文旨在分享,Reacthook 在中大型中台项目中的实践,适合熟悉 R......
  • vite项目生产环境去掉console信息【转载】
    环境变量引入通常去掉console为生产环境,即需要引入环境变量。具体请看这篇文章:vite项目初始化之~环境变量注意与webpacak相比,vite已经将这个功能内置到了,所以我们只......
  • 企业实训课程:一个通用后台管理项目
    empSystem企业实训:通用后台管理项目Github地址:https://github.com/Zpss2021/empSystem写在最前本项目限于原作者水平,上传时各个方面功能都不完善,只能作为一个小案例参......