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
This commit is contained in:
Dave Syer 2014-11-22 17:16:21 +00:00
parent e56a1ba561
commit 9f7bd0cddc
4 changed files with 39 additions and 9 deletions

View File

@ -101,7 +101,7 @@ public class JerseyAutoConfiguration implements WebApplicationInitializer {
@ConditionalOnProperty(prefix = "spring.jersey", name = "type", havingValue = "filter")
public FilterRegistrationBean jerseyFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new ServletContainer());
registration.setFilter(new ServletContainer(this.config));
registration.setUrlPatterns(Arrays.asList(this.path));
registration.setOrder(this.jersey.getFilter().getOrder());
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)
public ServletRegistrationBean jerseyServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new ServletContainer(), this.path);
new ServletContainer(this.config), this.path);
addInitParameters(registration);
registration.setName("jerseyServlet");
return 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,
"true");
for (Entry<String, String> entry : this.jersey.getInit().entrySet()) {

View File

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

View File

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

View File

@ -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;
}
}