1.创建项目:File-》New-》 Project-》Spring Initializr-》Next-》Next在dependencies页加载web-》Spring web;SQL-》Spring Data JPA、MySQLDriver;Template Engines-》 Thymeleaf;
2. 创建好后,在src/main/java/{demo}下创建一个controller package,并在其下新增java class文件。
3.在新增加的class类中加入以下代码:
package firsttest.firstspringtest.controller;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@EnableAutoConfiguration
@RestController
public class HelloWorldController {
@RequestMapping("/helloworld")
public String helloworld()
{
return "Hello World";
}
}
4.将main函数修改为:
public static void main(String[] args) {
SpringApplication.run(HelloWorldController.class, args);
}
5.在右侧的Maven tab,展开"lifecycle", 在“package"上点击右键,选择“Run Maven Build”,在其编译的地址下就会生成一个jar。
6. cmd 里用java -jar XXXX.jar 即可用行该服务。在浏览器中输入 localhost:8080/helloworld 即可访问该服务。注:因为在controller java class文件中有他@RequestMapping("/helloworld")所以/helloworld。如果修改了(“/{demo}”)替换就可以。
7.其默认的端口号为“8080”,可以通过项目“resources”下的application.properties修改。如下,修改端口为 8016
server.port=8016
8. 将target下的 可执行jar 复制到服务器上。可以用(xshell + xftp 工具 https://download.csdn.net/download/tianyueWindbg/15535309)
10, 在xshell中执行 java -jar XXXX.jar. (有时会报:Error: Unable to access jarfile firstspringtest-0.0.1-SNAPSHOT.jar ,请切换到jar包所在的目录)
如果没有装JDK,可以参考:https://blog.csdn.net/tianyueWindbg/article/details/114292990
11. 开放服务器端口 , 在本地电脑的浏览器地址输入 公网IP:8016/helloworld, 验证可以访问,则部署成功。