Reputation: 767
I am using spring boot 1.5, Swagger client api(not rest template) for making calls to source.
I am using the PATCH approach for partial updates, and had no problem creating a server side annotation and implementation for this.
However, when I try to write client code for testing, it fails.
Invalid HTTP method: PATCH; nested exception is java.net.ProtocolException: Invalid HTTP method: PATCH
Interestingly, when our applications are deployed in docker, It works but in local it fails with above error.
We have the same problem with integration tests
Not sure if I am missing on something? Is it the problem with spring boot?
Its spring boot microservices.
We use codegen (group: 'io.swagger', name: 'swagger-codegen-cli', version: '2.3.0-SNAPSHOT', classifier: 'HATEOAS') { transitive = false }
in source to generate client api.
Import api client in consumer service like
compile(group: 'com.xy.xy', name: 'product-service', version: '0.1.1', classifier: 'clientApi')
To make a patch call we use
productControllerApi.updateProductAssociationUsingPATCH(id, unitIds);
Upvotes: 1
Views: 995
Reputation: 672
You can create a custom API client class extended from ApiClient
with the overridden buildRestTemplate
which sets request factory HttpComponentsClientHttpRequestFactory
, and pass it through swagger-generated API class constructors when creating an object.
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import io.swagger.client.ApiClient;
public class CustomApiClient extends ApiClient {
@Override
protected RestTemplate buildRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
return restTemplate;
}
}
When instantiating the API classes, don't use the default constructor, because, it uses the default ApiClient
class, but you need a custom one with a custom rest template instance. So using the constructor with the parameter of ApiClient
will solve the issue.
@Test
void testApiMethod() {
TestApi testApi = new TestApi(new CustomApiClient());
}
Upvotes: 1