Using the Spring Boot CLI (or Spring Initializr) to init a Jersey JAX-RS app

To use the Spring Boot CLI to init an app using Jersey JAX-RS apis, use:

spring init --build=maven --dependencies=jersey --packaging=jar SpringBootREST.zip

Unzip the packaged app, import into your IDE.

To get Spring Boot to initialize Jersey and see your annotated resources, you need to add a Spring component that extends Jersey’s ResourceConfig class, and call register() for each of your Resources (seems odd that you need to explicitly register your endpoints like this, as in a War deployed to a Servlet container they would get found automatically?)

[code]
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;

@Component
public class JerseyExampleConfig extends ResourceConfig {

public JerseyExampleConfig() {
register(SimpleResource.class);
}

}
[/code]

Additionally, each Resource endpoint class needs to be @Component annotated so Spring can managed them.

Your JAX-RS resources classes other than above are implemented as normal.

Spring Boot default app structure and component scan

I’m familiar with the Spring Framework’s concept of a ‘component scan’ to find its managed beans when the app initializes, but was looking for how to configure the package(s) for the component scan in a Spring Boot app.

Turns out (see docs here), by convention if your main app class annotated with @SpringBootApplication and is in a package with all your code in packages below this point, then those subpackages are scanned by default.

Here’s a simple Spring Boot app class:

[code]

package kh.simplespringboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

[/code]

All packages below kh.simplespringboot in the example by default will be scanned for Spring components.