Martin
Martin

Reputation: 24316

Spring Boot: Jersey ResourceConfig needs annotating?

I am just getting started with Spring Boot and I wanted to implement a ResourceConfig and I finding some conflicting ideas.

Take the following:

@Component
public class JerseyExampleConfig extends ResourceConfig {

The above is annotated with COMPONENT:

@Configuration
public class JerseyExampleConfig extends ResourceConfig {

Which one is correct?

I thought annotating with Configuration would be the correct way but it appears that the Component is used in examples.

What's the difference?

Upvotes: 1

Views: 6505

Answers (3)

Paul Samsotha
Paul Samsotha

Reputation: 209102

They both make the ResourceConfig a Spring Bean, which is all that's really required to make the Spring Boot-Jersey integration work. You could even just do

@SpringBootApplication
class MyApplication {
    public static void main(String... args) {}

    @Bean
    public ResourceConfig jerseyConfig() {
        return new MyResourceConfig();
    }
}

With this, you wouldn't need @Component or @Configuration. It simply makes the ResourceConfig a Spring Bean, which like I said, is all that is required.

That being said, between the two annotations, @Configuration is really meant for Spring configuration. You could place your Spring configuration in the ResourceConfig subclass, but I would just put it in a separate configuration class, just to keep things separate. Note that configuration classes are also Spring Beans, so that's why the @Configuration works.

@Component on the other hand, is a general purpose annotation that will make the class a Spring Bean. This is the reason all the the examples show using the annotation, as the ResourceConfig is generally not meant to be a Spring configuration class, and it is less verbose then the example above, not using any annotations.

Upvotes: 1

Lazar Lazarov
Lazar Lazarov

Reputation: 2532

So even if you can not decide which to use for JerseyConfig you can determinate which is the better in a situation by reading what they actually mean:

@Configuration Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime

@Component Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.

@Configuration is meta-annotated with @Component, therefore @Configuration classes are candidates for component scanning

So by the said above you can annotate your config class with @Configuration as well but it will be an unnecessary overhead.

Upvotes: 1

cassiomolin
cassiomolin

Reputation: 131117

The documentation suggests @Component:

To get started with Jersey 2.x just include the spring-boot-starter-jersey as a dependency and then you need one @Bean of type ResourceConfig in which you register all the endpoints:

@Component
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        register(Endpoint.class);
    }
}

The documentation also says the following:

You can also register an arbitrary number of beans implementing ResourceConfigCustomizer for more advanced customizations.

All the registered endpoints should be @Components with HTTP resource annotations (@GET etc.), e.g.

@Component
@Path("/hello")
public class Endpoint {

    @GET
    public String message() {
        return "Hello";
    }
}

Since the Endpoint is a Spring @Component its lifecycle is managed by Spring and you can @Autowired dependencies and inject external configuration with @Value. The Jersey servlet will be registered and mapped to /* by default. You can change the mapping by adding @ApplicationPath to your ResourceConfig.

Upvotes: 3

Related Questions