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

标签 java json jakarta-ee jersey

刚开始为拼车引擎编写我的 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/

相关文章:

java - icepdf 未在 JFrame 中的 Linux 实例上渲染

php - 用jquery返回json数据?

eclipse - 当代码的某些部分发生变化时,是否有任何工具可以自动重建项目?

java - 如何在 GlassFish 上指定过滤器映射的顺序?

java - 我在哪里可以找到tomcat本地部署的 war ?

java - 将少量信息保存为 android 中的设置(例如第一次运行该应用程序)

java - 尝试在java中返回HashMap值时出现空指针异常

php - 在iphone应用程序中解析json数据

json - JAX-RS JAXB JSON 响应不起作用(MessageBodyProviderNotFoundException)

java - 如何将返回值和验证错误传达给方法的调用者?