Skip to content

Commit 6194545

Browse files
philwebbwilkinsona
andcommitted
Restore support for Jersey
Restore support for Jersey now that it supports JAX-RS 4. Closes gh-47967 Co-authored-by: Andy Wilkinson <andy.wilkinson@broadcom.com>
1 parent d0d332b commit 6194545

File tree

120 files changed

+7035
-17
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+7035
-17
lines changed

configuration-metadata/spring-boot-configuration-metadata-changelog-generator/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def dependenciesOf(String version) {
9292
"spring-boot-jackson2",
9393
"spring-boot-jdbc",
9494
"spring-boot-jdbc-test",
95+
"spring-boot-jersey",
9596
"spring-boot-jetty",
9697
"spring-boot-jms",
9798
"spring-boot-jooq",

documentation/spring-boot-docs/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ dependencies {
165165
implementation("org.assertj:assertj-core")
166166
implementation("org.cache2k:cache2k-spring")
167167
implementation("org.apache.groovy:groovy")
168+
implementation("org.glassfish.jersey.containers:jersey-container-servlet")
169+
implementation("org.glassfish.jersey.core:jersey-server")
168170
implementation("org.hibernate.orm:hibernate-jcache") {
169171
exclude group: "javax.activation", module: "javax.activation-api"
170172
exclude group: "javax.persistence", module: "javax.persistence-api"

documentation/spring-boot-docs/src/docs/antora/modules/ROOT/pages/documentation.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ xref:reference:features/index.adoc[The following content is for you]:
6464

6565
If you develop Spring Boot web applications, take a look at the following content:
6666

67-
* *Servlet Web Applications:* xref:reference:web/servlet.adoc[Spring MVC, Embedded Servlet Containers]
67+
* *Servlet Web Applications:* xref:reference:web/servlet.adoc[Spring MVC, Jersey, Embedded Servlet Containers]
6868
* *Reactive Web Applications:* xref:reference:web/reactive.adoc[Spring Webflux, Embedded Servlet Containers]
6969
* *Graceful Shutdown:* xref:reference:web/graceful-shutdown.adoc[Graceful Shutdown]
7070
* *Spring Security:* xref:reference:web/spring-security.adoc[Default Security Configuration, Auto-configuration for OAuth2, SAML]

documentation/spring-boot-docs/src/docs/antora/modules/ROOT/pages/redirect.adoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,12 @@
550550
* xref:how-to:index.adoc#howto[#howto]
551551
* xref:how-to:index.adoc[#howto]
552552
* xref:how-to:index.adoc[howto]
553+
* xref:how-to:jersey.adoc#howto.jersey.alongside-another-web-framework[#howto-jersey-alongside-another-web-framework]
554+
* xref:how-to:jersey.adoc#howto.jersey.alongside-another-web-framework[#howto.jersey.alongside-another-web-framework]
555+
* xref:how-to:jersey.adoc#howto.jersey.spring-security[#howto-jersey-spring-security]
556+
* xref:how-to:jersey.adoc#howto.jersey.spring-security[#howto.jersey.spring-security]
557+
* xref:how-to:jersey.adoc#howto.jersey[#howto-jersey]
558+
* xref:how-to:jersey.adoc#howto.jersey[#howto.jersey]
553559
* xref:how-to:logging.adoc#howto.logging.log4j.composite-configuration[#howto.logging.log4j.composite-configuration]
554560
* xref:how-to:logging.adoc#howto.logging.log4j.yaml-or-json-config[#howto-configure-log4j-for-logging-yaml-or-json-config]
555561
* xref:how-to:logging.adoc#howto.logging.log4j.yaml-or-json-config[#howto.logging.log4j.yaml-or-json-config]
@@ -1124,6 +1130,8 @@
11241130
* xref:reference:actuator/metrics.adoc#actuator.metrics.supported.http-clients[#production-ready-metrics-http-clients]
11251131
* xref:reference:actuator/metrics.adoc#actuator.metrics.supported.jdbc[#actuator.metrics.supported.jdbc]
11261132
* xref:reference:actuator/metrics.adoc#actuator.metrics.supported.jdbc[#production-ready-metrics-jdbc]
1133+
* xref:reference:actuator/metrics.adoc#actuator.metrics.supported.jersey[#actuator.metrics.supported.jersey]
1134+
* xref:reference:actuator/metrics.adoc#actuator.metrics.supported.jersey[#production-ready-metrics-jersey-server]
11271135
* xref:reference:actuator/metrics.adoc#actuator.metrics.supported.jetty[#actuator.metrics.supported.jetty]
11281136
* xref:reference:actuator/metrics.adoc#actuator.metrics.supported.jms[#actuator.metrics.supported.jms]
11291137
* xref:reference:actuator/metrics.adoc#actuator.metrics.supported.jvm[#actuator.metrics.supported.jvm]
@@ -2097,6 +2105,9 @@
20972105
* xref:reference:web/servlet.adoc#web.servlet.embedded-container[#boot-features-embedded-container]
20982106
* xref:reference:web/servlet.adoc#web.servlet.embedded-container[#features.developing-web-applications.embedded-container]
20992107
* xref:reference:web/servlet.adoc#web.servlet.embedded-container[#web.servlet.embedded-container]
2108+
* xref:reference:web/servlet.adoc#web.servlet.jersey[#boot-features-jersey]
2109+
* xref:reference:web/servlet.adoc#web.servlet.jersey[#features.developing-web-applications.jersey]
2110+
* xref:reference:web/servlet.adoc#web.servlet.jersey[#web.servlet.jersey]
21002111
* xref:reference:web/servlet.adoc#web.servlet.spring-mvc.auto-configuration[#boot-features-spring-mvc-auto-configuration]
21012112
* xref:reference:web/servlet.adoc#web.servlet.spring-mvc.auto-configuration[#features.developing-web-applications.spring-mvc.auto-configuration]
21022113
* xref:reference:web/servlet.adoc#web.servlet.spring-mvc.auto-configuration[#web.servlet.spring-mvc.auto-configuration]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[[howto.jersey]]
2+
= Jersey
3+
4+
5+
6+
[[howto.jersey.spring-security]]
7+
== Secure Jersey Endpoints with Spring Security
8+
9+
Spring Security can be used to secure a Jersey-based web application in much the same way as it can be used to secure a Spring MVC-based web application.
10+
However, if you want to use Spring Security's method-level security with Jersey, you must configure Jersey to use `setStatus(int)` rather `sendError(int)`.
11+
This prevents Jersey from committing the response before Spring Security has had an opportunity to report an authentication or authorization failure to the client.
12+
13+
The `jersey.config.server.response.setStatusOverSendError` property must be set to `true` on the application's javadoc:org.glassfish.jersey.server.ResourceConfig[] bean, as shown in the following example:
14+
15+
include-code::JerseySetStatusOverSendErrorConfig[]
16+
17+
18+
19+
[[howto.jersey.alongside-another-web-framework]]
20+
== Use Jersey Alongside Another Web Framework
21+
22+
To use Jersey alongside another web framework, such as Spring MVC, it should be configured so that it will allow the other framework to handle requests that it cannot handle.
23+
First, configure Jersey to use a filter rather than a servlet by configuring the configprop:spring.jersey.type[] application property with a value of `filter`.
24+
Second, configure your javadoc:org.glassfish.jersey.server.ResourceConfig[] to forward requests that would have resulted in a 404, as shown in the following example.
25+
26+
include-code::JerseyConfig[]

documentation/spring-boot-docs/src/docs/antora/modules/how-to/partials/nav-how-to.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
** xref:how-to:properties-and-configuration.adoc[]
55
** xref:how-to:webserver.adoc[]
66
** xref:how-to:spring-mvc.adoc[]
7+
** xref:how-to:jersey.adoc[]
78
** xref:how-to:http-clients.adoc[]
89
** xref:how-to:logging.adoc[]
910
** xref:how-to:data-access.adoc[]

documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/actuator/endpoints.adoc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Subject to xref:actuator/endpoints.adoc#actuator.endpoints.sanitization[sanitiza
9595
| Performs a thread dump.
9696
|===
9797

98-
If your application is a web application (Spring MVC or Spring WebFlux), you can use the following additional endpoints:
98+
If your application is a web application (Spring MVC, Spring WebFlux, or Jersey), you can use the following additional endpoints:
9999

100100
[cols="2,5"]
101101
|===
@@ -388,7 +388,8 @@ TIP: See javadoc:org.springframework.boot.actuate.autoconfigure.endpoint.web.Cor
388388
== Implementing Custom Endpoints
389389

390390
If you add a javadoc:org.springframework.context.annotation.Bean[format=annotation] annotated with javadoc:org.springframework.boot.actuate.endpoint.annotation.Endpoint[format=annotation], any methods annotated with javadoc:org.springframework.boot.actuate.endpoint.annotation.ReadOperation[format=annotation], javadoc:org.springframework.boot.actuate.endpoint.annotation.WriteOperation[format=annotation], or javadoc:org.springframework.boot.actuate.endpoint.annotation.DeleteOperation[format=annotation] are automatically exposed over JMX and, in a web application, over HTTP as well.
391-
Endpoints can be exposed over HTTP by using Spring MVC or Spring WebFlux.
391+
Endpoints can be exposed over HTTP by using Jersey, Spring MVC, or Spring WebFlux.
392+
If both Jersey and Spring MVC are available, Spring MVC is used.
392393

393394
The following example exposes a read operation that returns a custom object:
394395

@@ -451,7 +452,8 @@ Before calling an operation method, the input received over JMX or HTTP is conve
451452
[[actuator.endpoints.implementing-custom.web]]
452453
=== Custom Web Endpoints
453454

454-
Operations on an javadoc:org.springframework.boot.actuate.endpoint.annotation.Endpoint[format=annotation], javadoc:org.springframework.boot.actuate.endpoint.web.annotation.WebEndpoint[format=annotation], or javadoc:org.springframework.boot.actuate.endpoint.web.annotation.EndpointWebExtension[format=annotation] are automatically exposed over HTTP using Spring MVC or Spring WebFlux.
455+
Operations on an javadoc:org.springframework.boot.actuate.endpoint.annotation.Endpoint[format=annotation], javadoc:org.springframework.boot.actuate.endpoint.web.annotation.WebEndpoint[format=annotation], or javadoc:org.springframework.boot.actuate.endpoint.web.annotation.EndpointWebExtension[format=annotation] are automatically exposed over HTTP using Jersey, Spring MVC, or Spring WebFlux.
456+
If both Jersey and Spring MVC are available, Spring MVC is used.
455457

456458

457459

@@ -539,6 +541,8 @@ If an operation is invoked without a required parameter or with a parameter that
539541
You can use an HTTP range request to request part of an HTTP resource.
540542
When using Spring MVC or Spring Web Flux, operations that return a javadoc:org.springframework.core.io.Resource[] automatically support range requests.
541543

544+
NOTE: Range requests are not supported when using Jersey.
545+
542546

543547

544548
[[actuator.endpoints.implementing-custom.web.security]]

documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/actuator/metrics.adoc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,39 @@ Applications can opt in and record exceptions by xref:web/reactive.adoc#web.reac
765765

766766

767767

768+
[[actuator.metrics.supported.jersey]]
769+
=== Jersey Server Metrics
770+
771+
Auto-configuration enables the instrumentation of all requests handled by the Jersey JAX-RS implementation.
772+
By default, metrics are generated with the name, `http.server.requests`.
773+
You can customize the name by setting the configprop:management.observations.http.server.requests.name[] property.
774+
775+
By default, Jersey server metrics are tagged with the following information:
776+
777+
|===
778+
| Tag | Description
779+
780+
| `exception`
781+
| The simple class name of any exception that was thrown while handling the request.
782+
783+
| `method`
784+
| The request's method (for example, `GET` or `POST`)
785+
786+
| `outcome`
787+
| The request's outcome, based on the status code of the response.
788+
1xx is `INFORMATIONAL`, 2xx is `SUCCESS`, 3xx is `REDIRECTION`, 4xx is `CLIENT_ERROR`, and 5xx is `SERVER_ERROR`
789+
790+
| `status`
791+
| The response's HTTP status code (for example, `200` or `500`)
792+
793+
| `uri`
794+
| The request's URI template prior to variable substitution, if possible (for example, `/api/person/\{id}`)
795+
|===
796+
797+
To customize the tags, provide a javadoc:org.springframework.context.annotation.Bean[format=annotation] that implements javadoc:io.micrometer.core.instrument.binder.jersey.server.JerseyObservationConvention[].
798+
799+
800+
768801
[[actuator.metrics.supported.ssl]]
769802
=== SSL Bundle Metrics
770803

documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/actuator/monitoring.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ If you are developing a web application, Spring Boot Actuator auto-configures al
55
The default convention is to use the `id` of the endpoint with a prefix of `/actuator` as the URL path.
66
For example, `health` is exposed as `/actuator/health`.
77

8-
TIP: Actuator is supported natively with Spring MVC and Spring WebFlux.
8+
TIP: Actuator is supported natively with Spring MVC, Spring WebFlux, and Jersey.
9+
If both Jersey and Spring MVC are available, Spring MVC is used.
910

1011
NOTE: Jackson is a required dependency in order to get the correct JSON responses as documented in the xref:api:rest/actuator/index.adoc[API documentation].
12+
Jackson 3 should be used for Spring MVC and Spring WebFlux.
13+
Jersey does not yet have a Jackson 3 module, so you will need to use Jackson 2.
1114

1215

1316

documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/web/servlet.adoc

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[[web.servlet]]
22
= Servlet Web Applications
33

4-
If you want to build servlet-based web applications, you can take advantage of Spring Boot's auto-configuration for Spring MVC.
4+
If you want to build servlet-based web applications, you can take advantage of Spring Boot's auto-configuration for Spring MVC or Jersey.
55

66

77

@@ -495,6 +495,43 @@ See xref:io/rest-client.adoc#io.rest-client.apiversioning[] for details.
495495

496496

497497

498+
[[web.servlet.jersey]]
499+
== JAX-RS and Jersey
500+
501+
If you prefer the JAX-RS programming model for REST endpoints, you can use one of the available implementations instead of Spring MVC.
502+
https://jersey.github.io/[Jersey] and https://cxf.apache.org/[Apache CXF] work quite well out of the box.
503+
CXF requires you to register its javadoc:jakarta.servlet.Servlet[] or javadoc:jakarta.servlet.Filter[] as a javadoc:org.springframework.context.annotation.Bean[format=annotation] in your application context.
504+
Jersey has some native Spring support, so we also provide auto-configuration support for it in Spring Boot, together with a starter.
505+
506+
To get started with Jersey, include the `spring-boot-starter-jersey` as a dependency and then you need one javadoc:org.springframework.context.annotation.Bean[format=annotation] of type javadoc:org.glassfish.jersey.server.ResourceConfig[] in which you register all the endpoints, as shown in the following example:
507+
508+
include-code::MyJerseyConfig[]
509+
510+
WARNING: Jersey's support for scanning executable archives is rather limited.
511+
For example, it cannot scan for endpoints in a package found in a xref:how-to:deployment/installing.adoc[fully executable jar file] or in `WEB-INF/classes` when running an executable war file.
512+
To avoid this limitation, the `packages` method should not be used, and endpoints should be registered individually by using the `register` method, as shown in the preceding example.
513+
514+
For more advanced customizations, you can also register an arbitrary number of beans that implement javadoc:org.springframework.boot.autoconfigure.jersey.ResourceConfigCustomizer[].
515+
516+
All the registered endpoints should be a javadoc:org.springframework.stereotype.Component[format=annotation] with HTTP resource annotations (`@GET` and others), as shown in the following example:
517+
518+
include-code::MyEndpoint[]
519+
520+
Since the javadoc:org.springframework.boot.actuate.endpoint.annotation.Endpoint[format=annotation] is a Spring javadoc:org.springframework.stereotype.Component[format=annotation], its lifecycle is managed by Spring and you can use the javadoc:org.springframework.beans.factory.annotation.Autowired[format=annotation] annotation to inject dependencies and use the javadoc:org.springframework.beans.factory.annotation.Value[format=annotation] annotation to inject external configuration.
521+
By default, the Jersey servlet is registered and mapped to `/*`.
522+
You can change the mapping by adding javadoc:jakarta.ws.rs.ApplicationPath[format=annotation] to your javadoc:org.glassfish.jersey.server.ResourceConfig[].
523+
524+
By default, Jersey is set up as a servlet in a javadoc:org.springframework.context.annotation.Bean[format=annotation] of type javadoc:org.springframework.boot.web.servlet.ServletRegistrationBean[] named `jerseyServletRegistration`.
525+
By default, the servlet is initialized lazily, but you can customize that behavior by setting `spring.jersey.servlet.load-on-startup`.
526+
You can disable or override that bean by creating one of your own with the same name.
527+
You can also use a filter instead of a servlet by setting `spring.jersey.type=filter` (in which case, the javadoc:org.springframework.context.annotation.Bean[format=annotation] to replace or override is `jerseyFilterRegistration`).
528+
The filter has an javadoc:org.springframework.core.annotation.Order[format=annotation], which you can set with `spring.jersey.filter.order`.
529+
When using Jersey as a filter, a servlet that will handle any requests that are not intercepted by Jersey must be present.
530+
If your application does not contain such a servlet, you may want to enable the default servlet by setting configprop:server.servlet.register-default-servlet[] to `true`.
531+
Both the servlet and the filter registrations can be given init parameters by using `spring.jersey.init.*` to specify a map of properties.
532+
533+
534+
498535
[[web.servlet.embedded-container]]
499536
== Embedded Servlet Container Support
500537

0 commit comments

Comments
 (0)