This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathjava.html
More file actions
65 lines (61 loc) · 2.27 KB
/
java.html
File metadata and controls
65 lines (61 loc) · 2.27 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<section name="java" class="java">
<p class="ioDesc">Request</p>
<pre class="incoming"><code class="language-java">
// Maven : Add these dependecies to your pom.xml (java6+)
// <dependency>
// <groupId>org.glassfish.jersey.core</groupId>
// <artifactId>jersey-client</artifactId>
// <version>2.8</version>
// </dependency>
// <dependency>
// <groupId>org.glassfish.jersey.media</groupId>
// <artifactId>jersey-media-json-jackson</artifactId>
// <version>2.8</version>
// </dependency>
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.MediaType;
Client client = ClientBuilder.newClient();
<% if @method.toUpperCase() is 'POST' or @method.toUpperCase() is 'PUT' : %>
<% switch @helpers.getContentType @headers : %>
<% when 'application/json' : %>
Entity payload = Entity.json(<%= if @body then @helpers.escape(@body) else '""' %>);
<% end %>
<% when 'application/xml' : %>
Entity payload = Entity.xml(<%= if @body then @helpers.escape(@body) else '""' %>);
<% end %>
<% else : %>
Entity<String> payload = Entity.text(<%= if @body then @helpers.escape(@body) else '""' %>);
<% end %>
<% end %>
<% end %>
Response response = client.target("<%= @apiUrl %><%= @url %>")
<% switch @helpers.getContentType @headers : %>
<% when 'application/json' : %>
.request(MediaType.APPLICATION_JSON_TYPE)
<% end %>
<% when 'application/xml' : %>
.request(MediaType.APPLICATION_XML_TYPE)
<% end %>
<% else : %>
.request(MediaType.TEXT_PLAIN_TYPE)
<% end %>
<% end %>
<% if @headers and @helpers.isNotEmpty @headers: %>
<% for header, value of @headers: %>
<% if header.toLowerCase() != 'content-type' : %>
.header(<%= @helpers.escape header %>, <%= @helpers.escape value %>)
<% end %>
<% end %>
<% end %>
<% if @method.toUpperCase() is 'POST' or @method.toUpperCase() is 'PUT': %>
.<%- @method.toLowerCase() %>(payload);
<% else: %>
.<%- @method.toLowerCase() %>();
<% end %>
System.out.println("status: " + response.getStatus());
System.out.println("headers: " + response.getHeaders());
System.out.println("body:" + response.readEntity(String.class));
</code></pre></section>