This commit is contained in:
Phillip Webb
2014-10-06 12:03:51 -07:00
parent 09d5812c3b
commit 62eb01f0b8
20 changed files with 262 additions and 217 deletions

View File

@@ -1031,59 +1031,60 @@ upon successful completion of a servlet's service method. You should disable th
behaviour by setting `com.ibm.ws.webcontainer.invokeFlushAfterService` to `false`
[[boot-features-jersey]]
=== JAX-RS and Jersey
If you prefer the JAX-RS programming model for REST endpoints you can use one of the
available implementations instead of Spring MVC. Jersey 1.x and Apache Celtix work
quite well out of the box if you just register their `Servlet` or `Filter` as a
quite well out of the box if you just register their `Servlet` or `Filter` as a
`@Bean` in your application context. Jersey 2.x has some native Spring support so
we also provide autoconfiguration support for it in Spring Boot together with a
starter.
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:
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:
[source,java]
[source,java,indent=0,subs="verbatim,quotes,attributes"]
----
@Component
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(Endpoint.class);
@Component
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(Endpoint.class);
}
}
}
----
All the registered endpoints should be `@Components` with HTTP resource annotations (`@GET` etc.), e.g.
All the registered endpoints should be `@Components` with HTTP resource annotations
(`@GET` etc.), e.g.
[source,java]
[source,java,indent=0,subs="verbatim,quotes,attributes"]
----
@Component
@Path("/hello")
public class Endpoint {
@Component
@Path("/hello")
public class Endpoint {
@GET
public String message() {
return "Hello";
}
@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
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`.
There is a {github-code}/spring-boot-samples/spring-boot-sample-jersey[Jersey sample] so
you can see how to set things up. There is also a {github-code}/spring-boot-samples/spring-boot-sample-jersey1[Jersey 1.x sample].
you can see how to set things up. There is also a {github-code}/spring-boot-samples/spring-boot-sample-jersey1[Jersey 1.x sample].
Note that in the Jersey 1.x sample that the spring-boot maven plugin has been configured to
unpack some Jersey jars so they can be scanned by the JAX-RS implementation (the sample
asks for them to be scanned in its `Filter` registration.
[[boot-features-embedded-container]]
=== Embedded servlet container support
Spring Boot includes support for embedded Tomcat and Jetty servers. Most developers will