-
Notifications
You must be signed in to change notification settings - Fork 0
/
JerseyClient.java
46 lines (39 loc) · 1.39 KB
/
JerseyClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.mycompany.restclient;
import com.mycompany.restclientcommon.EmployeeBean;
import javax.ws.rs.ClientErrorException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.jaxb.internal.JaxbStringReaderProvider;
/**
* Jersey REST client generated for REST resource:GenericResource [generic]<br>
* USAGE:
* <pre>
* JerseyClient client = new JerseyClient();
* Object response = client.XXX(...);
* // do whatever with response
* client.close();
* </pre>
*
* @author atsushi
*/
public class JerseyClient {
private WebTarget webTarget;
private Client client;
private static final String BASE_URI = "http://localhost:8080/RestClientExam/webresources/generic";
public JerseyClient() {
ClientConfig config = new ClientConfig();
config.register(JaxbStringReaderProvider.class);
client = javax.ws.rs.client.ClientBuilder.newClient(config);
webTarget = client.target(BASE_URI);
}
public EmployeeBean search(long id) throws ClientErrorException {
WebTarget resource = webTarget.path("search").queryParam("id", id);
return resource
.request(javax.ws.rs.core.MediaType.APPLICATION_XML)
.get(EmployeeBean.class);
}
public void close() {
client.close();
}
}