简体   繁体   中英

Jersey Restful Services - Unable to connect to server

I am learning to implement restful webservices with Jersey. I always get "POST

http://localhost:8080/rest/welcome/post 

returned a response status of 404 Not Found" - What is wrong with my code below? Context root on the server side is rest and I have @Path welcome at class level and @pPath post at method level.

Client

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");
    }
}

Server

@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";
    }
 }

Am I facing this problem because of incorrect JsonProvider configuration? On the client side, I am using MyJsonProvider which extends JacksonJaxbJsonProvider to convert MyObject to Json. My code on the server side simply accepts MyObject. Do I need some code to hook up Json provider on server side too?

Here is my 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>

I can access rest index.html. I see the following in the log file. INFO: Root resource classes found: class com.rest.WelcomeRestJson I removed "/" with the path. Still same 404 error. Please help.

After adding Jersey Servlet to web.xml , get with no parameter started working. But post still failed. Adding the following init parameter to Jersey Servlet and replacing the Jersey Servlet mapping from /rest/* to /* fixed the issue.

<init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
</init-param>
  • Do not use slash(/) in path ( @Path("/welcome") ) annotation, instead of that write @Path("welcome") and @Path("post") .

  • I suggest you not to use service name post as i could be reserved keyword. Instead of post use @Path("test") or something else.

  • Use firefox plugin Poster to test your service status to verify is everything fine at service side. It is 404 error, Its not related to json error, Simply Your service is not available at http://localhost:8080/rest/welcome/post . Its not about code, its about configuration.

  • Are you able to reach upto http://localhost:8080/rest/ ?

1) It seems that you didn't register your resource. Do you rely on Jersey's autoscanning? It may not work correctly on some environments. The preferred way is to use the javax.ws.rs.core.Application class, since it's a standard JAX-RS way.

2) You need to register your provider also. The preferred way is to use the javax.ws.rs.core.Application class, since it's a standard JAX-RS way.

2.1) Anyway not registering a provider is not a reason for getting 404. Actually I don't remember what error code you'll get, probably it will be 500.

3) Strong advise: check Jetty log. It prints a lot of information containing the list of all registered resources and providers (including default and custom).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM