简体   繁体   English

如何使用jersey在Java IDE中完全模拟客户端-服务器静态Web服务,而无需部署到Web服务器?

[英]How to simulate client-server restful web service completely in Java IDE, without deploying to a web server, using jersey?

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. 我忘了提到我正在使用Jersey 1.19和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. 我使用Jersey API用Java创建了RESTful Web服务以及用于调用Web服务的客户端代码。 The client code is Jersey-based, as well. 客户端代码也是基于Jersey的。 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. 我遇到的问题是,我不想每次进行更改并要测试时都将JAR文件部署到Web服务器上-Web服务器位于远程服务器上,而我正在本地编码电脑。

Is it possible to simulate a client-server web service call completely within the IDE (ie Eclipse)? 是否可以在IDE(即Eclipse)中完全模拟客户端-服务器Web服务调用? In other words, I want to call the web service from my local computer, without having to host it on a web server; 换句话说,我想从本地计算机调用Web服务,而不必将其托管在Web服务器上。 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: 这是Web服务代码:

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? 是否可以在不使用Web服务器的情况下直接调用RestWebService类? Ie `WebResource webResource = client.resource(new RestWebService().EmpData("12345")); 即`WebResource webResource = client.resource(new RestWebService()。EmpData(“ 12345”));

Thank you very much. 非常感谢你。

Use Jersey Test Framework . 使用Jersey测试框架 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. JerseyTest将为每个测试用例启动和停止嵌入式服务器。 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) 这是另一个用法示例 (底部是泽西岛1.x,顶部是2.x)

This might be of use to you: http://www.eclipse.org/webtools/community/education/web/t320/Implementing_a_Simple_Web_Service.pdf 这可能对您有用: http : //www.eclipse.org/webtools/community/education/web/t320/Implementing_a_Simple_Web_Service.pdf

particularly page 12 onwards. 特别是第12页起。 Hope it helps. 希望能帮助到你。

EDIT : just in case, your machine URL will contain "localhost" for services running on your local computer. 编辑:以防万一,您的计算机URL将包含用于在本地计算机上运行的服务的“ localhost”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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