Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jersey Restful服务-无法连接到服务器_Java_Web Services_Rest_Jakarta Ee - Fatal编程技术网

Java Jersey Restful服务-无法连接到服务器

Java Jersey Restful服务-无法连接到服务器,java,web-services,rest,jakarta-ee,Java,Web Services,Rest,Jakarta Ee,我正在学习用Jersey实现restful Web服务。我总是收到“邮件” 返回404未找到的响应状态”-下面的代码有什么问题?服务器端的上下文根是rest,我在类级别有@Path welcome,在方法级别有@pPath post 客户端 public class WelcomeRestJsonClient { @Produces("application/json") @Consumes("text/plain") public void send() {

我正在学习用Jersey实现restful Web服务。我总是收到“邮件”

返回404未找到的响应状态”-下面的代码有什么问题?服务器端的上下文根是rest,我在类级别有@Path welcome,在方法级别有@pPath post

客户端

public class WelcomeRestJsonClient {

    @Produces("application/json")
    @Consumes("text/plain")
    public void send() {
        MyObject myObject = new MyObject();

        ClientConfig clientConfig = new DefaultClientConfig();
        clientConfig.getClasses().add(com.restclient.MyJsonProvider.class);
        Client client = Client.create(clientConfig);
        WebResource webResource = client.resource("http://localhost:8080/rest/welcome/post");
        ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, myObject);
        System.out.println("success");
    }
}
服务器

@Path("/welcome")
public class WelcomeRestJson {

    @POST
    @Path("/post")
    @Produces("text/plain")
    @Consumes("application/json")

    public String processPostData(MyObject myObject) {
        System.out.println("Inside processPostData");
        return "success";
    }
 }
我是否因为JsonProvider配置不正确而面临此问题?在客户端,我使用MyJsonProvider,它扩展了JacksonJaxbJsonProvider,将MyObject转换为Json。我在服务器端的代码只接受MyObject。我是否也需要一些代码来连接服务器端的Json提供程序

这是我的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.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

泽西岛休息服务
com.sun.jersey.spi.container.servlet.ServletContainer
com.sun.jersey.config.property.packages
com.rest
1.
泽西岛休息服务
/休息/*
index.html
我可以访问rest index.html。我在日志文件中看到以下内容。 信息:找到的根资源类:class com.rest.WelcomeRestJson 我删除了路径中的“/”。还是404错误。请帮忙。

  • 不要在path(
    @path(“/welcome”)
    )注释中使用斜杠(/),而不是写
    @path(“welcome”)
    @path(“post”)

  • 我建议您不要使用服务名称
    post
    ,因为我可能是保留关键字。而不是后期使用
    @Path(“test”)
    或其他东西

  • 使用firefox插件测试您的服务状态,以验证服务端是否一切正常。这是404错误,与json错误无关,只是您的服务在
    http://localhost:8080/rest/welcome/post
    。这不是关于代码,而是关于配置

  • 您是否能够访问到
    http://localhost:8080/rest/
    • 1)您似乎没有注册资源。你依赖泽西岛的自动扫描吗?它在某些环境下可能无法正常工作。首选的方法是使用
      javax.ws.rs.core.Application
      类,因为这是一种标准的JAX-rs方法

      2) 您还需要注册您的提供商。首选的方法是使用
      javax.ws.rs.core.Application
      类,因为这是一种标准的JAX-rs方法

      2.1)无论如何,不注册提供商不是获得404的原因。实际上我不记得你会得到什么错误代码,可能是500


      3) 强烈建议:查看码头日志。它打印了大量信息,其中包含所有已注册资源和提供程序的列表(包括默认和自定义)。

      将Jersey Servlet添加到
      web.xml
      后,get无需任何参数即可开始工作。但post仍然失败。将以下init参数添加到Jersey Servlet并将Jersey Servlet映射从
      /rest/*
      替换为
      /*
      修复了该问题

      <init-param>
              <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
              <param-value>true</param-value>
      </init-param>
      
      
      com.sun.jersey.api.json.POJOMappingFeature
      真的
      
      Jersey servlet的映射是什么?
      <init-param>
              <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
              <param-value>true</param-value>
      </init-param>