1

I have a library that contains the DTO and service interface.

@RequestMappping("/api/foo")
public interface FooApi {

   @GetMapping("/{id}")
   Foo getFooById(@pathVariable long id);

}

The server implements that interface:

@RestController
public FooController implements FooApi {

    Foo getFooById(long id) {
         return fooService.getById(id);
    }

}

And I have clients that also implement that interface:

@Component
public FooClient implements FooApi {

    Foo getFooById(long id) {
         return restTemplate.getObject(baseUrl + "/api/foo/{id}", ...);
    }

}

(in the future the client implementation will get derived from the annotations at runtime: clientFactory.createInstance(FooApi.class))

Is there a way to tell Spring to not treat the FooClient as a Controller, because I didn't annotate it as such? (Without removing the @RequestMappings from the FooApi interface entirely?)

I'm using Spring-Boot 2.7.

ST-DDT
  • 2,615
  • 3
  • 30
  • 51
  • do you place all of them into the same module(same spring context)? The common deal is split api declaretion , its client and its server implement to three modules(so there is no need to process context problem). – smileis2333 Jul 17 '23 at 09:38
  • Why add RequestMapping to an interface, particularly when you intend to have classes implement that interface that are not expecting to serve web requests? Seems like a confusing pattern at best. You could, for instance, have a base server-side implementation class and add the common mappings there. – dbreaux Jul 17 '23 at 16:39
  • The idea behind using the interface is that you have the controller implement the interface and generate a client using the annotations. The annotations are on the interface to ensure that there aren't any incompatible changes between the controller and the client. – ST-DDT Jul 18 '23 at 10:26
  • The client implementation would be done via JDK Proxies. There I have access to the method reflection and thus can analyze it what properties needs to be added where. And my colleagues and I can no longer forget about updating our service methods and no longer have to worry about typos or incorrect formatting of parameters. – ST-DDT Jul 18 '23 at 10:27
  • The client is currently implemented in a separate package, in the future it will be generated as runtime bean. The interface is in a shared jar used by both server and client. Since the client inherits the methods, it also inherits the annotations. And since the client service is also a web service it tries to map the client methods as well. – ST-DDT Jul 18 '23 at 10:56

0 Answers0