简体   繁体   中英

Produce and Consume data in Java RESTful with Jersey and JSON

Folks, I am newbie to Java RESTful,

I need to pass the data from my java application to the RESTful service. I am able to add the RESTful to my application but not able to send any data back to the service.

I used @GET and @Consumes at service. Please help me to get connect and data exchange between the same

As RESTful acts as server in my application

RESTful defined

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

Imported RESTful method

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

inside the java application

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

What you are trying to achieve is not really clear. However, to consume a REST webservice, you can use the JAX-RS Client API, which is part of the JAX-RS 2.0 specification and the reference implementation is Jersey .

For instance, to perform a GET request to a resource available in http://example.com/api/foo/{id} , you can use the following code:

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

For more details, have a look at the Jersey Client API documentation .

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