代码下载: https://download.csdn.net/download/u010476739/11467281
试验目的
通过maven的普通jar工程创建一个最简单的springboot的web应用
试验环境:
win10
eclipse[2018-12 (4.10.0)]
maven 3.6
jdk8
1 创建目录结构和文件
在目录C:\jackletter\springboot
下建立maven的jar工程
2 pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jackletter</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-demo</name>
<description>springboot-demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3 application.properties
server.port=8099
server.servlet.context-path=/demo
注意:这里是配置了运行的端口名称和路径,也可以不配置甚至没有这个文件,那么应用将使用的端口为:8080,路径为:/
4 DemoApplication.java
package com.jackletter.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5 TestController.java
package com.jackletter.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping("/test")
public String Test() {
return "hello";
}
}
6 使用maven进行打包
mvn package
7 控制台运行这个jar包
java -jar .\springboot-demo-0.0.1-SNAPSHOT.jar
8 浏览器访问
http://localhost:8099/demo/test