建立 Maven 專案
使用下列 Maven 原型命令,為您的 JAVA Web 應用程式建立 Maven 專案:
mvn archetype:generate \
-DgroupId=com.microsoft.azure.samples \
-DartifactId=azure-javaweb-app-simple \
-DarchetypeArtifactId=maven-archetype-quickstart \
-Dversion=1.0-SNAPSHOT \
-DinteractiveMode=false
您應該會看見下列輸出:
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /home/ayangupta/LearnProjects/DeployHelloApp
[INFO] Parameter: package, Value: com.microsoft.azure.samples
[INFO] Parameter: groupId, Value: com.microsoft.azure.samples
[INFO] Parameter: artifactId, Value: azure-javaweb-app-simple
[INFO] Parameter: packageName, Value: com.microsoft.azure.samples
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /private/tmp/TMP/azure-javaweb-app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.657 s
[INFO] Finished at: 2024-10-18T12:39:41-07:00
[INFO] ------------------------------------------------------------------------
現可使用下列檔案及目錄:
└── azure-javaweb-app-simple
├── src
│ ├── main
│ │ └── java
│ │ └── com
│ │ └── microsoft
│ │ └── azure
│ │ └── samples
│ │ └── App.java
│ └── test
│ └── java
│ └── com
│ └── microsoft
│ └── azure
│ └── samples
│ └── AppTest.java
└── pom.xml
修改 Maven pom.xml 檔案
以下列程式代碼取代 pom.xml 的內容。 此更新 pom.xml 帶來此 Web 應用程式所需相依性的正確版本。
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.microsoft.azure.samples</groupId>
<artifactId>azure-javaweb-app-simple</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
建立主應用程式類別
編輯 src/main/java/com/microsoft/azure/samples 目錄中的App.java檔案,以使用下列程式代碼來建立簡單的 HTTP 伺服器:
package com.microsoft.azure.samples;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
public class App {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/", new HelloHandler());
server.setExecutor(null); // creates a default executor
server.start();
System.out.println("Server started on http://localhost:8080/");
}
static class HelloHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
String response = "Hello, World!";
exchange.sendResponseHeaders(200, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}