0

I don't if this is even possible, which is why I thought I'd ask.

I forgot to mention I'm using Jersey 1.19 and Java 1.6.

I created a RESTful web service in Java using the Jersey API, as well as client code to call the web service. The client code is Jersey-based, as well. The problem I'm running into is I don't want to deploy the JAR file to the web server every time I make a change and want to test -- the web server is on a remote server and I"m coding on a local computer.

Is it possible to simulate a client-server web service call completely within the IDE (i.e. Eclipse)? In other words, I want to call the web service from my local computer, without having to host it on a web server; no different than calling a function.

Here is the client code:

package com.xyzcorp.webservices;

import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class RestClient {

    public static void main(String[] args) {

        Client client = Client.create();

        /*
        Right now, it's calling the web service on the web server.
        I want to call this same web service but from within the code local
        to my computer, without hosting it on a web server.
        */ 
        WebResource webResource = client.resource("http://myserver.com/rest/ids/12345");

        ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);

        if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
        }
    }
}

Here is the web service code:

package com.xyzcorp.webservices;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.xyzcorp.webservices.EmpData;

@Path("/rest")
public class RestWebService {
    @GET
    @Path("/ids/{ids}")
    @Produces(MediaType.APPLICATION_JSON)
    public EmpData getEmpDataJSON(
        @PathParam("ids") String ids)
        ...
        return empData;
    }
}

Is it possible to call the RestWebService class directly without having to use a web server? I.e. `WebResource webResource = client.resource(new RestWebService().EmpData("12345"));

Thank you very much.

user3621633
  • 1,681
  • 3
  • 32
  • 46
  • 1
    You can host it on your computer, but for running your web service you need an application server that must be running on it. – perissf Jul 27 '15 at 15:18
  • Ah. Interesting. Thank you so much for your input. Basically, the answer is there is no way to run a web service without an application (web) server, is that right? – user3621633 Jul 27 '15 at 15:23
  • 1
    Correct, there is no way without an application (web) server. But this can run on your local (development) PC – perissf Jul 27 '15 at 15:25
  • Thank you for your help! I'd be more than happy to checkmark an answer created by yourself and containing the info you provided. – user3621633 Jul 27 '15 at 15:28

2 Answers2

1

Use Jersey Test Framework. Run (semi) Integration/Unit tests on your resources like you would a normal unit test. For example

public class MainTest extends JerseyTest {

    public MainTest() throws Exception {
        super("com.sun.jersey.samples.helloworld.resources");
    }

    @Test
    public void testHelloWorld() {
        WebResource webResource = resource();
        String responseMsg = webResource.path("helloworld").get(String.class);
        assertEquals("Hello World", responseMsg);
    }
}

The JerseyTest will start and stop an embedded server for each test case. It could even be an in memory server (so as not to take so much load time) depending on what server dependency you want to use. Here's an example dependency

<dependency>
    <groupId>com.sun.jersey.jersey-test-framework</groupId>
    <artifactId>jersey-test-framework-grizzly2</artifactId>
    <version>${project.version}</version>
    <scope>test</scope>
</dependency>

The link I provided shows other dependencies you can use.

Here is another example usage (the bottom part is for Jersey 1.x - the top is 2.x)

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

This might be of use to you: http://www.eclipse.org/webtools/community/education/web/t320/Implementing_a_Simple_Web_Service.pdf

particularly page 12 onwards. Hope it helps.

EDIT : just in case, your machine URL will contain "localhost" for services running on your local computer.

Bradley Morris
  • 415
  • 1
  • 4
  • 10
  • Thank you very much for that. I'll try to determine if I can apply something like that, since that document seems to be for SOAP service calls, as opposed to RESTful. There's an article at http://stackoverflow.com/questions/11204688/can-a-java-web-service-hosted-without-deploying-it-on-a-server-like-tomcat-jboss which answers how to call a SOAP service that's local to the machine without a web server, but it's not applicable for RESTful, unfortunately. – user3621633 Jul 27 '15 at 15:13
  • 1
    The article mentioned in this answer makes use of Tomcat, that is an (application) server. Therefore the answer doesn't answer to the question, and the answer itself adds no (or little) value to the link. It would have better fit as a comment – perissf Jul 27 '15 at 15:16