1

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);
Vicky
  • 657
  • 1
  • 8
  • 16
  • 1
    How do you test your API? Can you share your client code? – Bunyamin Coskuner May 27 '19 at 05:55
  • @BunyaminCoskuner 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 using 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); – Vicky May 27 '19 at 06:20
  • Does [this](https://stackoverflow.com/q/25163131/113116) help? – Helen May 27 '19 at 09:02
  • @Helen It doesn't help because I don't want to manually set the url. I just want to use client api. If I use RestTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory() and use RestTemplate.exchange, its working. Just that the client-api is having the problem. That too in local and integration tests – Vicky May 27 '19 at 09:56

1 Answers1

1

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());
}
Seymur Asadov
  • 612
  • 5
  • 19