Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Resttemplate thread safe by using the withHttpInfo pattern #4049

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ public class ApiClient {

private Map<String, Authentication> authentications;

private HttpStatus statusCode;
private MultiValueMap<String, String> responseHeaders;

private DateFormat dateFormat;

public ApiClient() {
Expand Down Expand Up @@ -154,22 +151,6 @@ public class ApiClient {
return this;
}

/**
* Gets the status code of the previous request
* @return HttpStatus the status code
*/
public HttpStatus getStatusCode() {
return statusCode;
}

/**
* Gets the response headers of the previous request
* @return MultiValueMap a map of response headers
*/
public MultiValueMap<String, String> getResponseHeaders() {
return responseHeaders;
}

/**
* Get authentications (key: authentication name, value: authentication).
* @return Map the currently configured authentication types
Expand Down Expand Up @@ -580,9 +561,9 @@ public class ApiClient {
* @param contentType The request's Content-Type header
* @param authNames The authentications to apply
* @param returnType The return type into which to deserialize the response
* @return The response body in chosen type
* @return ResponseEntity&lt;T&gt; The response of the chosen type
*/
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 {
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 {
updateParamsForAuth(authNames, queryParams, headerParams);

final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
Expand Down Expand Up @@ -624,16 +605,11 @@ public class ApiClient {

ResponseEntity<T> responseEntity = restTemplate.exchange(requestEntity, returnType);

statusCode = responseEntity.getStatusCode();
responseHeaders = responseEntity.getHeaders();

if (responseEntity.getStatusCode() == HttpStatus.NO_CONTENT) {
return null;
} else if (responseEntity.getStatusCode().is2xxSuccessful()) {
return responseEntity.getBody();
if (responseEntity.getStatusCode().is2xxSuccessful()) {
return responseEntity;
} else {
// The error handler built into the RestTemplate should handle 400 and 500 series errors.
throw new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler");
throw new RestClientException("API returned " + responseEntity.getStatusCode() + " and it wasn't handled by the RestTemplate error handler");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

{{>generatedAnnotation}}
@Component("{{package}}.{{classname}}")
Expand Down Expand Up @@ -53,16 +54,53 @@ public class {{classname}} {
/**
* {{summary}}
* {{notes}}
{{#responses}} * <p><b>{{code}}</b>{{#message}} - {{message}}{{/message}}
{{/responses}}{{#allParams}} * @param {{paramName}} {{description}}{{^description}}The {{paramName}} parameter{{/description}}
{{/allParams}}{{#returnType}} * @return {{returnType}}
{{/returnType}} * @throws RestClientException if an error occurs while attempting to invoke the API
{{#externalDocs}}
* {{description}}
* @see <a href="{{url}}">{{summary}} Documentation</a>
{{/externalDocs}}
{{#responses}}
* <p><b>{{code}}</b>{{#message}} - {{message}}{{/message}}
{{/responses}}
{{#allParams}}
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}
{{/allParams}}
{{#returnType}}
* @return {{returnType}}
{{/returnType}}
* @throws RestClientException if an error occurs while attempting to invoke the API
{{#externalDocs}}
* {{description}}
* @see <a href="{{url}}">{{summary}} Documentation</a>
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws RestClientException {
{{#returnType}}
return {{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}).getBody();
{{/returnType}}
{{^returnType}}
{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
{{/returnType}}
}

/**
* {{summary}}
* {{notes}}
{{#responses}}
* <p><b>{{code}}</b>{{#message}} - {{message}}{{/message}}
{{/responses}}
{{#allParams}}
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}
{{/allParams}}
* @return ResponseEntity&lt;{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Void{{/returnType}}&gt;
* @throws RestClientException if an error occurs while attempting to invoke the API
{{#externalDocs}}
* {{description}}
* @see <a href="{{url}}">{{summary}} Documentation</a>
{{/externalDocs}}
*/
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public ResponseEntity<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{operationId}}WithHttpInfo({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws RestClientException {
Object postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
{{#allParams}}{{#required}}
// verify the required parameter '{{paramName}}' is set
Expand Down Expand Up @@ -102,7 +140,7 @@ public class {{classname}} {
String[] authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };

{{#returnType}}ParameterizedTypeReference<{{{returnType}}}> returnType = new ParameterizedTypeReference<{{{returnType}}}>() {};{{/returnType}}{{^returnType}}ParameterizedTypeReference<Void> returnType = new ParameterizedTypeReference<Void>() {};{{/returnType}}
{{#returnType}}return {{/returnType}}apiClient.invokeAPI(path, HttpMethod.{{httpMethod}}, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
return apiClient.invokeAPI(path, HttpMethod.{{httpMethod}}, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
}
{{/operation}}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ private String collectionToString(Collection<? extends CharSequence> collection)

private Map<String, Authentication> authentications;

private HttpStatus statusCode;
private MultiValueMap<String, String> responseHeaders;

private DateFormat dateFormat;

public ApiClient() {
Expand Down Expand Up @@ -146,22 +143,6 @@ public ApiClient setBasePath(String basePath) {
return this;
}

/**
* Gets the status code of the previous request
* @return HttpStatus the status code
*/
public HttpStatus getStatusCode() {
return statusCode;
}

/**
* Gets the response headers of the previous request
* @return MultiValueMap a map of response headers
*/
public MultiValueMap<String, String> getResponseHeaders() {
return responseHeaders;
}

/**
* Get authentications (key: authentication name, value: authentication).
* @return Map the currently configured authentication types
Expand Down Expand Up @@ -568,9 +549,9 @@ public String expandPath(String pathTemplate, Map<String, Object> variables) {
* @param contentType The request's Content-Type header
* @param authNames The authentications to apply
* @param returnType The return type into which to deserialize the response
* @return The response body in chosen type
* @return ResponseEntity&lt;T&gt; The response of the chosen type
*/
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 {
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 {
updateParamsForAuth(authNames, queryParams, headerParams);

final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
Expand Down Expand Up @@ -612,16 +593,11 @@ public <T> T invokeAPI(String path, HttpMethod method, MultiValueMap<String, Str

ResponseEntity<T> responseEntity = restTemplate.exchange(requestEntity, returnType);

statusCode = responseEntity.getStatusCode();
responseHeaders = responseEntity.getHeaders();

if (responseEntity.getStatusCode() == HttpStatus.NO_CONTENT) {
return null;
} else if (responseEntity.getStatusCode().is2xxSuccessful()) {
return responseEntity.getBody();
if (responseEntity.getStatusCode().is2xxSuccessful()) {
return responseEntity;
} else {
// The error handler built into the RestTemplate should handle 400 and 500 series errors.
throw new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler");
throw new RestClientException("API returned " + responseEntity.getStatusCode() + " and it wasn't handled by the RestTemplate error handler");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;


@Component("org.openapitools.client.api.AnotherFakeApi")
Expand Down Expand Up @@ -51,11 +52,23 @@ public void setApiClient(ApiClient apiClient) {
* To test special tags
* To test special tags and operation ID starting with number
* <p><b>200</b> - successful operation
* @param body client model
* @param body client model (required)
* @return Client
* @throws RestClientException if an error occurs while attempting to invoke the API
*/
public Client call123testSpecialTags(Client body) throws RestClientException {
return call123testSpecialTagsWithHttpInfo(body).getBody();
}

/**
* To test special tags
* To test special tags and operation ID starting with number
* <p><b>200</b> - successful operation
* @param body client model (required)
* @return ResponseEntity&lt;Client&gt;
* @throws RestClientException if an error occurs while attempting to invoke the API
*/
public ResponseEntity<Client> call123testSpecialTagsWithHttpInfo(Client body) throws RestClientException {
Object postBody = body;

// verify the required parameter 'body' is set
Expand Down
Loading