You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -79,31 +79,61 @@ You need to use the `GuiceResteasyBootstrapServletContextListener` as follows
79
79
Also notice that there is a `resteasy.guice.modules` context-param.
80
80
This can take a comma delimited list of class names that are Guice Modules.
81
81
82
+
== Registering resources and providers
83
+
84
+
Guice does not scan the classpath, so RESTEasy Guice can only see what your modules explicitly bind.
85
+
Every `@Path` root resource and every `@Provider` must be bound in one of your modules; an unbound resource is simply not registered and its endpoints return `404`.
86
+
87
+
[source,java]
88
+
----
89
+
public class MyModule implements Module {
90
+
@Override
91
+
public void configure(final Binder binder) {
92
+
binder.bind(HelloResource.class); // a @Path resource
93
+
binder.bind(MyExceptionMapper.class); // an @Provider
Resources are instantiated by Guice, so constructor injection uses Guice bindings.
100
+
This means the Jakarta REST `@Context` annotation cannot be used on a constructor parameter — RESTEasy performs `@Context` injection on fields and setters only, after Guice has created the instance.
101
+
To inject the request-scoped context objects (`UriInfo`, `HttpHeaders`, etc.) through a constructor, install the `RequestScopeModule` (see below) and use a plain `@Inject` constructor.
102
+
82
103
== Request Scope
83
104
84
-
Add the `RequestScopeModule` to your modules to allow objects to be scoped to the HTTP request by adding the `@RequestScoped` annotation to your fields in resource classes.
85
-
All the objects injectable via the `@Context` annotation are also injectable, except `ServletConfig` and `ServletContext`.
86
-
Note that `RequestScopeModule` will already be added if any of your modules extends `com.google.inject.servlet.ServletModule`.
87
-
In such cases you should not add it again to avoid injector creation errors.
105
+
Add the `RequestScopeModule` to your modules to make the Jakarta REST context objects injectable with a plain Guice `@Inject`, bound to the current HTTP request.
106
+
The following types are bound: `UriInfo`, `HttpHeaders`, `Request`, `SecurityContext`, `HttpServletRequest`, and `HttpServletResponse` (`ServletConfig` and `ServletContext` are not bound).
88
107
89
108
[source,java]
90
109
----
91
110
92
111
import jakarta.inject.Inject;
93
-
import jakarta.servlet.http.HttpServletRequest;
94
-
import jakarta.ws.rs.core.Context;
112
+
import jakarta.ws.rs.GET;
113
+
import jakarta.ws.rs.Path;
114
+
import jakarta.ws.rs.core.UriInfo;
115
+
116
+
@Path("example")
117
+
public class ExampleResource {
118
+
private final UriInfo uriInfo;
95
119
96
-
import dev.resteasy.guice.RequestScoped;
120
+
@Inject
121
+
public ExampleResource(final UriInfo uriInfo) {
122
+
this.uriInfo = uriInfo;
123
+
}
97
124
98
-
public class MyClass {
99
-
@Inject @RequestScoped @Context
100
-
private HttpRequest request;
125
+
@GET
126
+
public String get() {
127
+
return uriInfo.getRequestUri().toString();
128
+
}
101
129
}
102
130
----
103
131
132
+
The scope is also available directly as the `dev.resteasy.guice.RequestScoped` annotation for binding your own request-scoped objects.
133
+
104
134
== Binding Jakarta REST utilities
105
135
106
-
Add the `JaxrsModule` to bind `jakarta.ws.rs.ext.RuntimeDelegate`, `jakarta.ws.rs.core.Response.ResponseBuilder`, `jakarta.ws.rs.core.UriBuilder`, `jakarta.ws.rs.core.Variant.VariantListBuilder` and `org.jboss.resteasy.client.jaxrs.ClientHttpEngine`.
136
+
Add the `JaxrsModule` to bind the Jakarta REST utility factories so they can be injected: `jakarta.ws.rs.ext.RuntimeDelegate`, `jakarta.ws.rs.core.Response.ResponseBuilder`, `jakarta.ws.rs.core.UriBuilder`, and `jakarta.ws.rs.core.Variant.VariantListBuilder`.
107
137
108
138
== Configuring Stage
109
139
@@ -135,7 +165,7 @@ If this value is not specified, RESTEasy uses whatever Guice's default is.
@Message(id = 100, value = "Cannot execute expected module {0}''s @{1} method {2} because it has unexpected parameters: skipping.", format = Format.MESSAGE_FORMAT)
0 commit comments