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

SpringBoot2笔记1:helloworld程序

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

SpringBoot2笔记1:helloworld程序


SpringBoot官方文档

Spring5升级内容


1.创建一个Maven项目


2.配置项目文件pom.xml 如果出现红色报错,先重载一下Maven,然后重启Idea pom.xml文件


    4.0.0

    org.atgg
    boot-01-helloworld
    1.0-SNAPSHOT


    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.4.RELEASE
    

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


3.创建一个MainApplication.class文件
package com.atgg.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}
4.创建一个控制器controller,处理响应请求

HelloController.class
package com.atgg.boot.controller;


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

@RestController
public class HelloController {
	
	//请求映射,当发出/hello的请求时,回复一个字符串:Hello, Spring Boot 2!
    @RequestMapping("/hello")
    public String handle01(){
        return "Hello, Spring Boot 2!";
    }
}
4.点击MainApplication.class的main函数的run开始运行程序:

在浏览器输入本地请求:

http://localhost:8080/hello

页面会显示字符串:Hello, Spring Boot 2!

5.简化配置 在resources包中新建一个文件:application.properties

修改请求的端口号为8888
server.port=8888
相应的,请求页面为8888时才会显示对应的字符串:

6.打包成可执行文件(.jar) 在pom.xml中添加以下字段:
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.3.4.RELEASE
            
        
    

点击Maven,将其打成jar包

进入target文件夹就可以找到.jar包了,进入cmd

输入命令执行:

java -jar boot-01-helloworld-1.0-SNAPSHOT.jar(jar包名)

照样可以执行响应请求


注意点:
取消掉cmd的快速编辑模式

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

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

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