1

I am using the maven plugin to generate angular client for spring rest services.

I have a rest service called UserResources. Here is the generated code :

export interface HttpClient {

    request<R>(requestConfig: { method: string; url: string; queryParams?: any; data?: any; copyFn?: (data: R) => R; }): RestResponse<R>;
}

export class UserResourcesClient {

    constructor(protected httpClient: HttpClient) {
    }
...

I created an implementation for the interface HttpClient in a seprate file :

@Injectable()
export class HttpClientImpl implements HttpClient {
    request<R>(
        requestConfig: { 
            method: string; 
            url: string;
            queryParams?: any; 
            data?: any; 
            copyFn?: (data: R) => R; 
        }
    ): RestResponse<R> {
        return null;
    }
}

In my app.module.ts I declared HttpClientImpl as a provider

providers: [
        HttpClientImpl
    ]

The code compiles but doesn't work. The error is : Unhandled Promise rejection: Can't resolve all parameters for UserResourcesClient: (?). ; Zone: <root> ; Task: Promise.then ; Value: Error: "Can't resolve all parameters for UserResourcesClient: (?)."

It seems that angular doesn't use HttpClientImpl when there is a reference to HttpClient

If you can help

Thank you

lczapski
  • 4,026
  • 3
  • 16
  • 32
kchetoua
  • 21
  • 1
  • 1
  • 6
  • So you want to inject an Interface? [Is it possible to inject interface with angular2?](https://stackoverflow.com/q/37002522/9423231), [Inject an Interface with Angular 4](https://stackoverflow.com/q/45117934/9423231), [Angular 6 service with interface](https://stackoverflow.com/q/51174859/9423231), [Inject all Services that implement some Interface](https://stackoverflow.com/q/35916542/9423231) – frido Jan 21 '20 at 15:42

2 Answers2

0

You should use HttpClientImpl instead of HttpClient in the constructor

export class UserResourcesClient {

constructor(protected httpClientImpl: HttpClientImpl) {
}

...

Chunbin Li
  • 2,196
  • 1
  • 17
  • 31
0

The solution was to give the same name to the class:

@Injectable()
export class HttpClientimplements HttpClient {

Very strange but it works

If you have a better solution...

kchetoua
  • 21
  • 1
  • 1
  • 6