gpt4 book ai didi

java - Java EE 新手,使用 Jersey SDK 获取 HTTP 404 的 Json web 服务

转载 作者:行者123 更新时间:2023-11-29 05:53:40 24 4
gpt4 key购买 nike

刚开始为拼车引擎编写我的 JSON 网络服务。我在尝试编写注册 API 时收到 HTTP 404 错误。

这是我的问题所在

"http://localhost:8081/mCruiseOnCarPool4All/carpool4all/Registration"
HTTP/1.1 405 Method Not Allowed
"http://localhost:8081/mCruiseOnCarPool4All/carpool4all/Registration/Request"
HTTP/1.1 500 Internal Server Error
"http://localhost:8081/mCruiseOnCarPool4All/Registration/Request"
HTTP/1.1 404 Not Found
"http://localhost:8081/mCruiseOnCarPool4All/Registration"
HTTP/1.1 404 Not Found

我知道我在这里遗漏了一些非常愚蠢的东西。

Web.xml(泽西图书馆)

<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.mcruiseon.carpool4all</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/carpool4all/*</url-pattern>

RegistrationService.java,只写了Post方法。我在所有异常上返回错误,并在成功时使用 sessionkey 确定。 post方法中的代码你可以忽略,我只是想分享一下,让你理解我的错误处理。

package com.mcruiseon.carpool4all;

@Path("/Registration")
public class RegistrationService {
private ClientSession clientSession ;
private String sessionKey ;
private SessionManager sessionManager ;

@Context
UriInfo uriInfo;
@Context
Request request;

@POST
@Path ("Request")
@Consumes({ MediaType.APPLICATION_JSON })
public Response post(JAXBElement<AMessageStrategy> element) {
try {
clientSession = new ClientSession(God.mCruiseOnServer) ;
} catch (InvalidServerDNSorIPException e) {
e.printStackTrace();
return Response.serverError().build() ;
}
sessionKey = sessionManager.setClientSession(clientSession) ;
clientSession.setSessionKey(sessionKey) ;

clientSession.getSendQueue().sendRequest(element.getValue()) ;
try {
clientSession.waitAndGetResponse(element.getValue()) ;
} catch (WaitedLongEnoughException e) {
return Response.serverError().build() ;
} catch (UnableToResolveResponseException e) {
return Response.serverError().build() ;
}
return Response.ok(sessionKey).build();
}
}

Junit 测试用例(删除了所有 HttpConnection 代码)

ClientIdentityConcrete clientIdentity = new ClientIdentityConcrete("username", "password", "secretkey") ;
RegistrationRequest register = new RegistrationRequest(clientIdentity);
String jsonStr = mapper.writeValueAsString(clientIdentity);
HttpPost request = new HttpPost("http://localhost:8081/mCruiseOnCarPool4All/Registration/Request");

最佳答案

相关连接是/mCruiseOnCarPool4All/carpool4all/Registration/Request并返回 500 错误,因此您的服务器控制台上必须有错误堆栈跟踪。

您显示的其他 URL 命中 404,因为这些 URL 没有指向您的 Jersey servlet,它似乎映射到 /carpool4all

您的网址格式是:

 <host>/<app>/<jerseyservlet>/<xml resource>/<method path>

 - host = localhost:8081/ (obviously)
- app = mCruiseOnCarPool4All
- jerseyservlet = carpool4all
- xml resource = Registration
- method path = Request

关于java - Java EE 新手,使用 Jersey SDK 获取 HTTP 404 的 Json web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13008079/

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