java - JBoss WildFly 13 上的 RESTful Web 服务适用于浏览器和 curl,但不适用于单元测试 - 404 错误
问题描述
我在 JBoss WildFly 13.0.0.Final 上部署了一个应用程序,作为在 macOS High Sierra 10.13.6 上的 EE8 上运行的 EAR。从应用程序网页调用时,所有方法都可以正常工作。
我可以 ping 通“我的 IP 地址”、127.0.0.1 和 locahost:
ping 192.999.9.255 PING 192.999.1.255 (192.999.9.255):来自 192.999.1.23 的 56 个数据字节 64 个字节:icmp_seq=0 ttl=64 时间=0.067 毫秒 来自 192.999.1.23 的 64 个字节:icmp_seq=1.tl=6 ms 64 字节,来自 192.99.1.23:icmp_seq=2 ttl=64 time=0.098 ms
PING 127.0.0.1 (127.0.0.1):56 个数据字节 64 个字节来自 127.0.0.1:icmp_seq=0 ttl=64 时间=0.046 毫秒 64 个字节来自 127.0.0.1:icmp_seq=1 ttl=64 时间=0.085 毫秒 64 个字节来自127.0.0.1:icmp_seq=2 ttl=64 时间=0.076 毫秒
PING localhost (127.0.0.1):56 个数据字节 64 个字节来自 127.0.0.1:icmp_seq=0 ttl=64 时间=0.041 毫秒 64 个字节来自 127.0.0.1:icmp_seq=1 ttl=64 时间=0.084 毫秒 64 个字节来自 127.0。 0.1:icmp_seq=2 ttl=64 时间=0.090 毫秒
我的 usr/hosts 是标准的:
127.0.0.1 本地主机
255.255.255.255 广播主机
::1 本地主机
fe80::1%lo0 本地主机
我可以访问其中一项 Web 服务并显示来自 curl 的 JSON:
MacBook-Pro:bin NOTiFY$ curl "http://localhost:8080/NOTiFYwell/notifywell/get-all-enumbers/"
[
{
"id": "5b6c5dbefac4f7105b3cca2e",
"code": "E100",
"name": "Curcumin (from turmeric)",
"colour": "Yellow-orange",
"status": "Approved in the EU.Approved in the US."
},
并从浏览器:
但是,当我运行 JUnit 5 测试时:
@Test
public final void test01GetListEnumbers() throws IOException {
System.out.println(">>>>> test01GetListEnumbers .....");
String url = "http://localhost:8080/NOTiFYwell/notifywell/get-all-enumbers/";
System.out.println(">>>>> test01GetListEnumbers url = " + url);
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader(CONTENT_TYPE, APPLICATION_JSON);
//Execute and get the response.
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = httpClient.execute(httpGet);
System.out.println(">>>>> test01GetListEnumbers response getStatus = " + response.getStatusLine().getStatusCode());
System.out.println(">>>>> test01GetListEnumbers response getEntity = " + EntityUtils.toString(response.getEntity()));
}
我得到:
test01GetListEnumbers ..... test01GetListEnumbers url = http://localhost:8080/NOTiFYwell/notifywell/get-all-enumbers/ test01GetListEnumbers response getStatus = 404 test01GetListEnumbers response getEntity =
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing /NOTiFYwell/notifywell/get-all-enumbers/. Reason:
<pre> Not Found</pre></p><hr><a
href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.z-SNAPSHOT</a>
<hr/>
</body>
</html>
在 WireShark 我看到:
3027 26.303256 127.0.0.1 127.0.0.1 HTTP 275 GET /NOTiFYwell/notifywell/get-all-enumbers/HTTP/1.1
3028 26.303279 127.0.0.1 127.0.0.1 TCP 56 8080 → 50922 [ACK] Seq=1 Ack=220 Win=408064 Len=0 TSval=635600988 TSecr=635600988
3029 26.304989 127.0.0.1 127.0.0.1 HTTP 652 HTTP/1.1 404 未找到(文本/html)
我所有的 JUnit 测试都在我之前的 MacBook Pro(2018 年 7 月)上使用 WildFly 12.0.0.Final 在 EE8 和 macOS High Sierra 10.13.6 上运行。我最近升级到在 EE8 上运行的 WildFly 13.0.0.Final。在这两种情况下,我访问 Web 服务的一系列单元测试都使用了 Apache HTTP Core 4.4.10、Client 4.5.6 JAR。
尝试了许多 SO 建议均无济于事。
解决方案
@James R. Perkins 感谢您为我指明正确的方向。我停止了 Wildfly 13,果然,我仍然在端口 8080 上监听了一些东西
MacBook-Pro:bin NOTiFY$ sudo lsof -i :8080 密码:命令 PID
用户 FD 类型设备大小/关闭节点名称 java 437 NOTiFY 163u IPv6 0x299d8c12df3e5311 0t0 TCP localhost:http-alt (LISTEN)
杀死 PID 并再次运行我的 JUnit 测试并(如希望的那样)得到:
org.apache.http.conn.HttpHostConnectException: 连接到 localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1, localhost/fe80:0:0:0: 0:0:0:1%1] 失败:连接被拒绝
重新启动 WildFly 13 并成功重新运行了我的 JUnit 测试:
test01GetListEnumbers url = http://localhost:8080/NOTiFYwell/notifywell/get-all-enumbers/ test01GetListEnumbers 响应 getStatus = 200 test01GetListEnumbers 响应 getEntity = [ { "id": "5b6c5dbefac4f7105b3cca2e", "code": "E100", "name “:“姜黄素(来自姜黄)”,“颜色”:“黄橙色”,“状态”:“在欧盟获得批准。在美国获得批准。” },
仍然不确定 Jetty 9.x 来自哪里。
这对应于在我使用“迁移助手”将所有内容从旧 MacBook 复制到新 MacBook 后必须重新启动 Mac。Jetty 似乎在重新启动时启动。会调查。
推荐阅读
- c++ - c++ 进程以退出代码完成
- postgresql - 如何从 Postgresql 中的最大值 3 中的最后两位数中获取最大值?
- python-2.7 - 在尝试除外期间未附加列表
- r - ggplot2 y 值作为“行数”?
- javascript - 传递一个对象方法并在函数之外调用这些方法。随机密码生成器
- regex - 正则表达式可选地提取两个字符之间的字符
- google-bigquery - 从 Pubsub 在 BigQuery 中编写查询
- kotlin - 在挂起的函数中调用协程构建器(启动、异步)时出现模棱两可的 coroutineContext
- neo4j - 使用 neo4j 连接字符串存储用户名和密码
- sql - 完全匹配 sql 表中的标签/标签列表