Inject ResourceConfig instance (not class) into Jersey

If you inject the class (via a servlet parameter) it seems that
Jersey tries to create all the beans for you (and fails). I thought
it was supposed to work (according to the docs), so I'm a bit confused
but the sample now has Spring DI and the tests pass.

Fixes gh-1981
pull/1979/merge
Dave Syer 10 years ago
parent e56a1ba561
commit 9f7bd0cddc

@ -101,7 +101,7 @@ public class JerseyAutoConfiguration implements WebApplicationInitializer {
@ConditionalOnProperty(prefix = "spring.jersey", name = "type", havingValue = "filter") @ConditionalOnProperty(prefix = "spring.jersey", name = "type", havingValue = "filter")
public FilterRegistrationBean jerseyFilterRegistration() { public FilterRegistrationBean jerseyFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean(); FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new ServletContainer()); registration.setFilter(new ServletContainer(this.config));
registration.setUrlPatterns(Arrays.asList(this.path)); registration.setUrlPatterns(Arrays.asList(this.path));
registration.setOrder(this.jersey.getFilter().getOrder()); registration.setOrder(this.jersey.getFilter().getOrder());
registration.addInitParameter(ServletProperties.FILTER_CONTEXT_PATH, registration.addInitParameter(ServletProperties.FILTER_CONTEXT_PATH,
@ -124,16 +124,13 @@ public class JerseyAutoConfiguration implements WebApplicationInitializer {
@ConditionalOnProperty(prefix = "spring.jersey", name = "type", havingValue = "servlet", matchIfMissing = true) @ConditionalOnProperty(prefix = "spring.jersey", name = "type", havingValue = "servlet", matchIfMissing = true)
public ServletRegistrationBean jerseyServletRegistration() { public ServletRegistrationBean jerseyServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean( ServletRegistrationBean registration = new ServletRegistrationBean(
new ServletContainer(), this.path); new ServletContainer(this.config), this.path);
addInitParameters(registration); addInitParameters(registration);
registration.setName("jerseyServlet"); registration.setName("jerseyServlet");
return registration; return registration;
} }
private void addInitParameters(RegistrationBean registration) { private void addInitParameters(RegistrationBean registration) {
Class<? extends ResourceConfig> configType = this.config.getClass();
registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS,
configType.getName());
registration.addInitParameter(CommonProperties.METAINF_SERVICES_LOOKUP_DISABLE, registration.addInitParameter(CommonProperties.METAINF_SERVICES_LOOKUP_DISABLE,
"true"); "true");
for (Entry<String, String> entry : this.jersey.getInit().entrySet()) { for (Entry<String, String> entry : this.jersey.getInit().entrySet()) {

@ -18,6 +18,7 @@
</organization> </organization>
<properties> <properties>
<main.basedir>${basedir}/../..</main.basedir> <main.basedir>${basedir}/../..</main.basedir>
<java.version>1.7</java.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>

@ -19,19 +19,19 @@ package sample.jersey;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
@Path("/hello") @Path("/hello")
public class Endpoint { public class Endpoint {
@Value("${message:World}") @Autowired
private String msg; private Service service;
@GET @GET
public String message() { public String message() {
return "Hello " + this.msg; return "Hello " + this.service.message();
} }
} }

@ -0,0 +1,32 @@
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.jersey;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Service {
@Value("${message:World}")
private String msg;
public String message() {
return this.msg;
}
}
Loading…
Cancel
Save