Affects: All


There is a really nice consequence of how the official Jax-RS API is structured. Since the implementation of the Jax-RS API is not packaged with the API itself (obviously) there is a way to define REST APIs by creating a package, let's call it service-api so that not only the methods, but all the URI paths, the request methods, or even the accepted mime-types can be defined for every method in the service-api package.

Based on this package we can create the service-client and service-impl modules so that both packages instantly know how to communicate with eachother through the defined REST API and none of the API information needs to get duplicated.

A similar feature (although not necessarily bounded to REST APIs) could be achieved with the Spring framework by extracting the @Controller, ResponseEntity and related annotations/classes to a different package.

It could be a nice addition to the way teams implement REST APIs. The reason a team might not want to use Jax-RS instead of Spring controller is that there are some nice features already in the Spring Controllers, for example, that the ResponseEntity can take a generic type, as opposed to the Jax-RS's Response, or the way Spring controllers handle exceptions.


Maybe the above description is a little cluttered, so I'll try and show an example of what I'm thinking about:

We have 3 packages: service-api, service-impl, service-client:

service-api.jar:


@RequestMapping("/api/service")
public interface Service {
    @GetMapping
    ResponseEntity<Resp> getStuff();
}

service-impl.jar:

@RestController
public class ServiceImpl implements Service {
    // ...
}

service-client.jar:

@Configuration
public class Config {
    @Bean
    public Service client() {
        return RestClient.api(Service.class);
    }
}

@Component
public class SomeBean {
    @Autowired
    private Service client;

    // ...

    public void callService() {
        client.getStuff();
    }

    // ...

}

Comment From: rstoyanchev

@rolaca11 I believe this is a duplicate of #16747. Do you agree? If I understand correctly, your goal is to have an automatic client simply by pointing to an interface with @Controller and @RequestMapping annotations on it. Have you seen the Feign client in Spring Cloud?

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: rolaca11

I'm sorry for the late answer. Yes, it is quite similar to what OpenFeign does. I'm not familiar with that library, but on first look, it is really similar. Can I use a standard Spring controller with an OpenFeign interface on the server-side?

Comment From: rstoyanchev

Yes it does. I'm closing this as duplicate.