Skip to content

Extract Controller API to separate package #25494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rolaca11 opened this issue Jul 30, 2020 · 4 comments
Closed

Extract Controller API to separate package #25494

rolaca11 opened this issue Jul 30, 2020 · 4 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue

Comments

@rolaca11
Copy link

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();
    }

    // ...

}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 30, 2020
@rstoyanchev
Copy link
Contributor

@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?

@rstoyanchev rstoyanchev added status: waiting-for-feedback We need additional information before we can continue in: web Issues in web modules (web, webmvc, webflux, websocket) labels Aug 14, 2020
@spring-projects-issues
Copy link
Collaborator

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.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Aug 21, 2020
@rolaca11
Copy link
Author

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?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels Aug 21, 2020
@rstoyanchev
Copy link
Contributor

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

@rstoyanchev rstoyanchev added status: duplicate A duplicate of another issue and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants