看下使用这个框架的pom依赖,如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sh</groupId>
<artifactId>kmlserver</artifactId>
<version>0.0.1</version>
<name>kml_moudle_server</name>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.29</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.35</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.51.v20230217</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.4.51.v20230217</version>
</dependency>
</dependencies>
</project>
public class TestServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf8");
resp.getWriter().write("ok");
}
}
现在服务器是有了,但是我们还没有指定它去处理什么请求,就是客户端发来什么请求会到这个服务器里面去处理?要怎么指定这个服务器处理请求的路径呢?
接下来就是看一下jetty框架中的核心类,如下图:
上面的三个就是我们定义的核心类,代码如下:
/**
* @author xuan
* @create 2023/10/16
*/
public class Context {
public static ParamConfig paramConfig;
private static HttpServer httpServer;
public static void start(ParamConfig paramConfig) throws Exception {
Context.paramConfig = paramConfig;
httpServer = new HttpServer(paramConfig.getPort());
httpServer.start();
}
}
/**
* @author xuan
* @create 2023/10/16
*/
public class HttpServer {
private final int port;
private final Server server;
private final ServletHandler servletHandler;
public HttpServer(int port) {
this.port = port;
this.server = new Server(port);
this.servletHandler = new ServletHandler();
registerUrlMapping();
server.setHandler(servletHandler);
}
private void registerUrlMapping() {
add(TestServlet.class, "/test");
}
private void add(Class<? extends HttpServlet> servletClass, String url) {
this.servletHandler.addServletWithMapping(servletClass, url);
}
public void start() throws Exception {
server.start();
server.join();
}
}
/**
* @author xuan
* @create 2023/10/16
*/
@Getter
@Setter
public class ParamConfig {
private int port;
}
接下来就是jetty的启动类,启动之后,就可以处理来自客户端的请求了,启动类如下图:
代码如下图:
/**
* @author xuan
* @create 2023/10/17
*/
public class KmlServerBootStrap {
public static void main(String[] args) {
if(args.length < 1) {
throw new IllegalArgumentException("启动参数错误...");
}
String paramConfigPath = args[0];
KmlServerBootStrap kmlServerBootStrap = new KmlServerBootStrap();
kmlServerBootStrap.start(paramConfigPath);
}
private void start(String paramConfigPath) {
try {
Yaml yaml = new Yaml();
ParamConfig paramConfig = yaml.loadAs(Files.newInputStream(Paths.get(paramConfigPath)), ParamConfig.class);
Context.start(paramConfig);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
但是目前启动的时候你会发现启动不起来,因为你还没有设置程序的端口号,端口号需要怎么设置呢?我们可以写在配置文件里面,如下图:
我们这里设置的端口号为9000,然后上面其实已经创建了一个和配置文件kmlserver.yaml对应的配置类了,如下图:
然后我们的配置文件里面的数据会自动映射到ParamConfig配置类里面的同名字段上面,但是需要在启动参数里面把配置文件kmlserver.yaml的路径配置上去,如下图:
启动的时候指定端口号,如下图:
运行KmlServerBootStrap#main启动jetty框架服务器,如下图:
在客户端输入localhost:9000/test,看看TestServler服务器能否处理这个请求,如下图:
因为是post请求,所以这里只能使用postman去发送http连接,发送之后jetty服务器可以成功监听,如下图:
至此,使用jetty框架去处理客户端发送的http请求就构建成功了。