在之前的文章中介绍了Maven的安装,现在基于Java的NanoHTTPD这个库来讲下Maven的使用。

创建Maven项目

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

-D为输入指定参数的意思,其中后面就是具体的参数名以及对应的值。

groupIdartifactIdversion三个标准的属性,其中groupId理解为命名空间、artifactId理解为项目名称;version当忽略时,默认为1.0-SNAPSHOT。如果这三个参数都指定,那么在创建的过程中会提示你指定。

archetypeArtifactId为创建工程的类型,还有其它的可以参考官方解释:http://maven.apache.org/archetypes/index.html,每种类型创建的文件夹都不一样,但是总体分不开两种maintest。如果此参数不指定,那么创建的工程就是一个大工程,什么都包含。

interactiveMode当等于false时,表示禁用交互的模式来创建工程。

进入软件目录 cd my-app

配置pom.xml文件

这里使用的是NanoHTTPD所以添加依赖

<dependencies>
  <dependency>
    <groupId>org.nanohttpd</groupId>
    <artifactId>nanohttpd</artifactId>
    <version>2.3.1</version>
  </dependency>    
</dependencies>

另外,打包成jar时要包含NanoHTTPD依赖,所以

<build>
  <plugins>
  <!-- any other plugins -->
    <plugin>
       <artifactId>maven-assembly-plugin</artifactId>
       <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
</build>

依照NanoHTTPD修改App.java
my-app\src\main\java\com\mycompany\app\App.java

package com.mycompany.app;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
    
import fi.iki.elonen.NanoHTTPD;

public class App extends NanoHTTPD {
    public App() throws IOException {
        super(8080);
        start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
    }

    public static void main(String[] args ) throws IOException {
        new App();
    }

    @Override
    public Response serve(IHTTPSession session) {
        Map<String, String> files = new HashMap<String, String>();
        // HTTP GET
        if (session.getMethod() == Method.GET) {
            String itemIdRequestParameter = session.getParameters().get("itemId").get(0);
            return newFixedLengthResponse("Requested itemId = " + itemIdRequestParameter);
        }
        // HTTP POST
        if (session.getMethod() == Method.POST) {
            try {
                session.parseBody(files);
                String requestBody = session.getQueryParameterString();
                return newFixedLengthResponse("Request body = " + requestBody);
            } catch (IOException | ResponseException e) {
                // handle
            }
        }
        return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, 
          "The requested resource does not exist");
    }
}

打包mvn package
打包之后在my-app\target\目录下会生成my-app-1.0-SNAPSHOT-jar-with-dependencies.jar可执行文件

按照官网上的说法,这样就行了,但是我在实际测试中不会生效,干脆直接运行mvn assembly:assembly -DdescriptorId=jar-with-dependencies 来打包好了

执行此可执行文件java -cp target/my-app-1.0-SNAPSHOT-jar-with-dependencies.jar com.mycompany.app.App

使用

# HTTP GET
> curl 'http://localhost:8080/?itemId=23Bk8'
Requested itemId = 23Bk8

# HTTP POST
> curl -X POST -i http://localhost:8080/ --data 'deliveryAddress=Washington nr 4&quantity=5'
Request body = deliveryAddress=Washington nr 4&quantity=5

参考资料:
A Guide to NanoHTTPD

nanohttpd-github

Maven in 5 Minutes

Including dependencies in a jar with Maven

Maven创建Java Application工程(既jar包)

maven--插件篇(assembly插件)

Retrieve HTTP Body in NanoHTTPD

标签: none

评论已关闭