gpt4 book ai didi

java - 如何使用 OSGi 声明性服务正确实现 servlet?

转载 作者:行者123 更新时间:2023-11-30 01:51:04 25 4
gpt4 key购买 nike

我正在尝试(学习如何)使用 OSGi 提供的声明性服务 (DS) 创建 Servlet ( javax.servlet.Servlet ),如下所示:

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.osgi.service.component.annotations.Component;

@Component(service = Servlet.class, property = { "alias:String=/hello" }, immediate = true)
public class LogsHTTPServlet extends HttpServlet {

private static final long serialVersionUID = 2302372200114479484L;

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
resp.setContentType("text/plain");
resp.getWriter().println("It works!");
} catch (Exception e) {
e.printStackTrace();
}
resp.setStatus(200);
}
}

当我运行the project时上Eclipse (2019-03 (4.11.0),版本 ID:20190314-1200) this target platform ,使用custom run configuration ,我设置的地方 org.eclipse.jetty.server (以及其他必需的依赖项)与 this VM argument ( -Dorg.osgi.service.http.port=8080 ),我在 osgi 控制台中看到类似下面的内容:

osgi> 2019-05-07 18:22:28.236:INFO::Start Level: Equinox Container: 0d7d3d67-401b-48eb-b016-6e6c0c16352c: Logging initialized @1590ms to org.eclipse.jetty.util.log.StdErrLog
2019-05-07 18:22:28.388:INFO:oejs.session:Start Level: Equinox Container: 0d7d3d67-401b-48eb-b016-6e6c0c16352c: DefaultSessionIdManager workerName=node0
2019-05-07 18:22:28.388:INFO:oejs.session:Start Level: Equinox Container: 0d7d3d67-401b-48eb-b016-6e6c0c16352c: No SessionScavenger set, using defaults
2019-05-07 18:22:28.389:INFO:oejs.session:Start Level: Equinox Container: 0d7d3d67-401b-48eb-b016-6e6c0c16352c: node0 Scavenging every 660000ms
2019-05-07 18:22:28.427:INFO:oejs.session:Start Level: Equinox Container: 0d7d3d67-401b-48eb-b016-6e6c0c16352c: node0 Scavenging every 660000ms

现在,当我使用 Postman 执行此请求时(http://127.0.0.1:8080/hello)我得到:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 404 ProxyServlet: /hello</title>
</head>
<body>
<h2>HTTP ERROR 404</h2>
<p>Problem accessing /hello. Reason:

<pre> ProxyServlet: /hello</pre>
</p>
<hr>
<a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.15.v20190215</a>
<hr/>
</body>
</html>

如何解决这个问题?

最佳答案

使用osgi.http.whiteboard.servlet.pattern属性代替alias:

@Component(service = Servlet.class, property = {"osgi.http.whiteboard.servlet.pattern=/hello/*" })

或者,使用当前的 osgi compendium 依赖项(版本 7.0.0),您可以使用 @org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletPattern 注释,如下所示:

@Component(service=Servlet.class)
@org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletPattern("/hello/*")

参见https://osgi.org/specification/osgi.cmpn/7.0.0/service.http.whiteboard.html#service.http.whiteboard-i21223311

关于java - 如何使用 OSGi 声明性服务正确实现 servlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56026214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com