1
1
package { {invokerPackage} };
2
2
3
- import com.fasterxml.jackson.core.JsonGenerator.Feature;
4
3
import com.fasterxml.jackson.databind.*;
5
4
import com.fasterxml.jackson.annotation.*;
6
- import com.fasterxml.jackson.databind.annotation.JsonSerialize;
7
5
8
6
import com.sun.jersey.api.client.Client;
9
7
import com.sun.jersey.api.client.ClientResponse;
@@ -48,6 +46,7 @@ public class ApiClient {
48
46
private Map< String, String> defaultHeaderMap = new HashMap< String, String> ();
49
47
private boolean debugging = false ;
50
48
private String basePath = " {{basePath}}" ;
49
+ private JSON json = new JSON();
51
50
52
51
private Map< String, Authentication> authentications;
53
52
@@ -340,50 +339,38 @@ public class ApiClient {
340
339
}
341
340
342
341
/**
343
- * Deserialize the given JSON string to Java object.
344
- *
345
- * @param json The JSON string
346
- * @param containerType The container type, one of "list", "array" or ""
347
- * @param cls The type of the Java object
348
- * @return The deserialized Java object
342
+ * Serialize the given Java object into string according the given
343
+ * Content-Type (only JSON is supported for now).
349
344
*/
350
- public Object deserialize(String json, String containerType, Class cls) throws ApiException {
351
- if (null != containerType) {
352
- containerType = containerType.toLowerCase();
353
- }
354
- try{
355
- if (" list" .equals(containerType) || " array" .equals(containerType)) {
356
- JavaType typeInfo = JsonUtil.getJsonMapper().getTypeFactory().constructCollectionType(List.class, cls);
357
- List response = (List< ?> ) JsonUtil.getJsonMapper().readValue(json, typeInfo);
358
- return response;
359
- }
360
- else if(String.class.equals(cls)) {
361
- if (json != null && json.startsWith(" \" " ) && json.endsWith(" \" " ) && json.length() > 1)
362
- return json.substring(1, json.length() - 2);
363
- else
364
- return json;
365
- }
366
- else {
367
- return JsonUtil.getJsonMapper().readValue(json, cls);
368
- }
369
- }
370
- catch (IOException e) {
371
- throw new ApiException(500, e.getMessage(), null, json);
345
+ public String serialize(Object obj, String contentType) throws ApiException {
346
+ if (contentType.startsWith(" application/json" )) {
347
+ return json.serialize(obj);
348
+ } else {
349
+ throw new ApiException(400, " can not serialize object into Content-Type: " + contentType);
372
350
}
373
351
}
374
352
375
353
/**
376
- * Serialize the given Java object into JSON string .
354
+ * Deserialize response body to Java object according to the Content-Type .
377
355
*/
378
- public String serialize(Object obj) throws ApiException {
379
- try {
380
- if (obj != null)
381
- return JsonUtil.getJsonMapper().writeValueAsString(obj);
382
- else
383
- return null;
384
- }
385
- catch (Exception e) {
386
- throw new ApiException(500, e.getMessage());
356
+ public <T > T deserialize(ClientResponse response, TypeRef returnType) throws ApiException {
357
+ String contentType = null;
358
+ List< String> contentTypes = response.getHeaders().get(" Content-Type" );
359
+ if (contentTypes != null && ! contentTypes.isEmpty())
360
+ contentType = contentTypes.get(0);
361
+ if (contentType == null)
362
+ throw new ApiException(500, " missing Content-Type in response" );
363
+
364
+ String body;
365
+ if (response.hasEntity())
366
+ body = (String) response.getEntity(String.class);
367
+ else
368
+ body = " " ;
369
+
370
+ if (contentType.startsWith(" application/json" )) {
371
+ return json.deserialize(body, returnType);
372
+ } else {
373
+ throw new ApiException(500, " can not deserialize Content-Type: " + contentType);
387
374
}
388
375
}
389
376
@@ -399,9 +386,10 @@ public class ApiClient {
399
386
* @param accept The request's Accept header
400
387
* @param contentType The request's Content-Type header
401
388
* @param authNames The authentications to apply
389
+ * @param returnType The return type into which to deserialize the response
402
390
* @return The response body in type of string
403
391
*/
404
- public String invokeAPI(String path, String method, List<Pair > queryParams, Object body, Map<String , String > headerParams, Map<String , Object > formParams, String accept, String contentType, String[] authNames) throws ApiException {
392
+ public < T > T invokeAPI(String path, String method, List<Pair > queryParams, Object body, Map<String , String > headerParams, Map<String , Object > formParams, String accept, String contentType, String[] authNames, TypeRef returnType ) throws ApiException {
405
393
updateParamsForAuth(authNames, queryParams, headerParams);
406
394
407
395
Client client = getClient();
@@ -465,23 +453,23 @@ public class ApiClient {
465
453
} else if (body instanceof FormDataMultiPart) {
466
454
response = builder.type(contentType).post(ClientResponse.class, body);
467
455
} else {
468
- response = builder.type(contentType).post(ClientResponse.class, serialize(body));
456
+ response = builder.type(contentType).post(ClientResponse.class, serialize(body, contentType ));
469
457
}
470
458
} else if ("PUT".equals(method)) {
471
459
if (encodedFormParams != null) {
472
460
response = builder.type(contentType).put(ClientResponse.class, encodedFormParams);
473
461
} else if(body == null) {
474
- response = builder.put(ClientResponse.class, serialize(body));
462
+ response = builder.put(ClientResponse.class, serialize(body, contentType ));
475
463
} else {
476
- response = builder.type(contentType).put(ClientResponse.class, serialize(body));
464
+ response = builder.type(contentType).put(ClientResponse.class, serialize(body, contentType ));
477
465
}
478
466
} else if ("DELETE".equals(method)) {
479
467
if (encodedFormParams != null) {
480
468
response = builder.type(contentType).delete(ClientResponse.class, encodedFormParams);
481
469
} else if(body == null) {
482
470
response = builder.delete(ClientResponse.class);
483
471
} else {
484
- response = builder.type(contentType).delete(ClientResponse.class, serialize(body));
472
+ response = builder.type(contentType).delete(ClientResponse.class, serialize(body, contentType ));
485
473
}
486
474
} else {
487
475
throw new ApiException(500, " unknown method type " + method);
@@ -490,11 +478,10 @@ public class ApiClient {
490
478
if (response.getStatusInfo() == ClientResponse.Status.NO_CONTENT) {
491
479
return null;
492
480
} else if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
493
- if (response.hasEntity()) {
494
- return (String) response.getEntity(String.class);
495
- } else {
496
- return " " ;
497
- }
481
+ if (returnType == null)
482
+ return null;
483
+ else
484
+ return deserialize(response, returnType);
498
485
} else {
499
486
String message = " error" ;
500
487
String respBody = null;
0 commit comments