Skip to content

Commit 69b5001

Browse files
committed
updated java resttemplate library template to be thread safe
1 parent ee42987 commit 69b5001

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

src/main/resources/handlebars/Java/libraries/resttemplate/ApiClient.mustache

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ public class ApiClient {
9191
private RestTemplate restTemplate;
9292

9393
private Map<String, Authentication> authentications;
94-
95-
private HttpStatus statusCode;
96-
private MultiValueMap<String, String> responseHeaders;
9794

9895
private DateFormat dateFormat;
9996

@@ -147,22 +144,6 @@ public class ApiClient {
147144
return this;
148145
}
149146

150-
/**
151-
* Gets the status code of the previous request
152-
* @return HttpStatus the status code
153-
*/
154-
public HttpStatus getStatusCode() {
155-
return statusCode;
156-
}
157-
158-
/**
159-
* Gets the response headers of the previous request
160-
* @return MultiValueMap a map of response headers
161-
*/
162-
public MultiValueMap<String, String> getResponseHeaders() {
163-
return responseHeaders;
164-
}
165-
166147
/**
167148
* Get authentications (key: authentication name, value: authentication).
168149
* @return Map the currently configured authentication types
@@ -518,9 +499,9 @@ public class ApiClient {
518499
* @param contentType The request's Content-Type header
519500
* @param authNames The authentications to apply
520501
* @param returnType The return type into which to deserialize the response
521-
* @return The response body in chosen type
502+
* @return ResponseEntity&lt;T&gt; The response of the chosen type
522503
*/
523-
public <T> T invokeAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
504+
public <T> ResponseEntity<T> invokeAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
524505
updateParamsForAuth(authNames, queryParams, headerParams);
525506
526507
final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
@@ -542,20 +523,12 @@ public class ApiClient {
542523
RequestEntity<Object> requestEntity = requestBuilder.body(selectBody(body, formParams, contentType));
543524

544525
ResponseEntity<T> responseEntity = restTemplate.exchange(requestEntity, returnType);
545-
546-
statusCode = responseEntity.getStatusCode();
547-
responseHeaders = responseEntity.getHeaders();
548526

549-
if (responseEntity.getStatusCode() == HttpStatus.NO_CONTENT) {
550-
return null;
551-
} else if (responseEntity.getStatusCode().is2xxSuccessful()) {
552-
if (returnType == null) {
553-
return null;
554-
}
555-
return responseEntity.getBody();
527+
if (responseEntity.getStatusCode().is2xxSuccessful()) {
528+
return responseEntity;
556529
} else {
557530
// The error handler built into the RestTemplate should handle 400 and 500 series errors.
558-
throw new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler");
531+
throw new RestClientException("API returned " + responseEntity.getStatusCode() + " and it wasn't handled by the RestTemplate error handler");
559532
}
560533
}
561534

src/main/resources/handlebars/Java/libraries/resttemplate/api.mustache

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import org.springframework.http.HttpHeaders;
2323
import org.springframework.http.HttpMethod;
2424
import org.springframework.http.HttpStatus;
2525
import org.springframework.http.MediaType;
26+
import org.springframework.http.ResponseEntity;
2627

2728
{{>generatedAnnotation}}
2829
@Component("{{package}}.{{classname}}")
@@ -56,7 +57,7 @@ public class {{classname}} {
5657
* <p><b>{{code}}</b>{{#message}} - {{message}}{{/message}}
5758
{{/responses}}
5859
{{#parameters}}
59-
* @param {{paramName}} {{description}}{{^description}}The {{paramName}} parameter{{/description}}
60+
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
6061
{{/parameters}}
6162
{{#returnType}}
6263
* @return {{returnType}}
@@ -67,7 +68,38 @@ public class {{classname}} {
6768
* @see <a href="{{url}}">{{summary}} Documentation</a>
6869
{{/externalDocs}}
6970
*/
71+
{{#isDeprecated}}
72+
@Deprecated
73+
{{/isDeprecated}}
7074
public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#parameters}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/parameters}}) throws RestClientException {
75+
{{#returnType}}
76+
return {{operationId}}WithHttpInfo({{#parameters}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/parameters}}).getBody();
77+
{{/returnType}}
78+
{{^returnType}}
79+
{{operationId}}WithHttpInfo({{#parameters}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/parameters}});
80+
{{/returnType}}
81+
}
82+
83+
/**
84+
* {{summary}}
85+
* {{notes}}
86+
{{#responses}}
87+
* <p><b>{{code}}</b>{{#message}} - {{message}}{{/message}}
88+
{{/responses}}
89+
{{#parameters}}
90+
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
91+
{{/parameters}}
92+
* @return ResponseEntity&lt;{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Void{{/returnType}}&gt;
93+
* @throws RestClientException if an error occurs while attempting to invoke the API
94+
{{#externalDocs}}
95+
* {{description}}
96+
* @see <a href="{{url}}">{{summary}} Documentation</a>
97+
{{/externalDocs}}
98+
*/
99+
{{#isDeprecated}}
100+
@Deprecated
101+
{{/isDeprecated}}
102+
public ResponseEntity<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{operationId}}WithHttpInfo({{#parameters}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/parameters}}) throws RestClientException {
71103
Object {{localVariablePrefix}}postBody = {{^isForm}}{{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}{{/isForm}}{{#isForm}}null{{/isForm}};
72104
{{#parameters}}
73105
{{#required}}
@@ -121,7 +153,7 @@ public class {{classname}} {
121153
String[] {{localVariablePrefix}}authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };
122154

123155
{{#returnType}}ParameterizedTypeReference<{{{returnType}}}> {{localVariablePrefix}}returnType = new ParameterizedTypeReference<{{{returnType}}}>() {};{{/returnType}}{{^returnType}}ParameterizedTypeReference<Void> {{localVariablePrefix}}returnType = new ParameterizedTypeReference<Void>() {};{{/returnType}}
124-
{{#returnType}}return {{/returnType}}{{localVariablePrefix}}apiClient.invokeAPI({{localVariablePrefix}}path, HttpMethod.{{httpMethod}}, {{localVariablePrefix}}queryParams, {{localVariablePrefix}}postBody, {{localVariablePrefix}}headerParams, {{localVariablePrefix}}formParams, {{localVariablePrefix}}accept, {{localVariablePrefix}}contentType, {{localVariablePrefix}}authNames, {{localVariablePrefix}}returnType);
156+
return {{localVariablePrefix}}apiClient.invokeAPI({{localVariablePrefix}}path, HttpMethod.{{httpMethod}}, {{localVariablePrefix}}queryParams, {{localVariablePrefix}}postBody, {{localVariablePrefix}}headerParams, {{localVariablePrefix}}formParams, {{localVariablePrefix}}accept, {{localVariablePrefix}}contentType, {{localVariablePrefix}}authNames, {{localVariablePrefix}}returnType);
125157
}
126158
{{/contents}}
127159
{{/operation}}

0 commit comments

Comments
 (0)