使用 Jersey 和 JSON 在 Java RESTful 中生产和使用数据

Produce and Consume data in Java RESTful with Jersey and JSON

各位, 我是 Java RESTful,

的新手

我需要将数据从我的 java 应用程序传递到 RESTful 服务。我可以将 RESTful 添加到我的应用程序,但无法将任何数据发送回服务。

我在服务中使用了@GET 和@Consumes。请帮助我在相同的

之间建立连接和数据交换

因为 RESTful 在我的应用程序中充当服务器

RESTful定义

@GET
@Consumes("application/json")
@Path("{strID}")
public String getJson(@PathParam("strID")String strID){
  return strID;
}

导入RESTful方法

public String getJson(String strID) throws UniformInterfaceException {
        WebResource resource = webResource;
        resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{strID}));
        return resource.get(String.class);
    }

在 java 应用程序中

static RESTful objRFIDService = new RESTful();
objRFIDService.getJson("RESTfultest");

你想要达到的目的不是很清楚。但是,要使用 REST 网络服务,您可以使用 JAX-RS 客户端 API,它是 JAX-RS 2.0 specification and the reference implementation is Jersey.

的一部分

例如,要对 http://example.com/api/foo/{id} 中可用的资源执行 GET 请求,您可以使用以下代码:

String id = ...

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://example.com").path("api").path("foo").path(id);
String result = target.request(MediaType.APPLICATION_JSON_TYPE).get(String.class);

有关详细信息,请查看 Jersey Client API documentation