From 9f7bd0cddcaec7751f4eea9d637bba2a96a884ea Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Sat, 22 Nov 2014 17:16:21 +0000 Subject: [PATCH] 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 --- .../jersey/JerseyAutoConfiguration.java | 7 ++-- .../spring-boot-sample-jersey/pom.xml | 1 + .../src/main/java/sample/jersey/Endpoint.java | 8 ++--- .../src/main/java/sample/jersey/Service.java | 32 +++++++++++++++++++ 4 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 spring-boot-samples/spring-boot-sample-jersey/src/main/java/sample/jersey/Service.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java index 6b2d84e1dbf..75982d3895f 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java @@ -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 configType = this.config.getClass(); - registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, - configType.getName()); registration.addInitParameter(CommonProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); for (Entry entry : this.jersey.getInit().entrySet()) { diff --git a/spring-boot-samples/spring-boot-sample-jersey/pom.xml b/spring-boot-samples/spring-boot-sample-jersey/pom.xml index fe5a7242dff..abc30fdb97e 100644 --- a/spring-boot-samples/spring-boot-sample-jersey/pom.xml +++ b/spring-boot-samples/spring-boot-sample-jersey/pom.xml @@ -18,6 +18,7 @@ ${basedir}/../.. + 1.7 diff --git a/spring-boot-samples/spring-boot-sample-jersey/src/main/java/sample/jersey/Endpoint.java b/spring-boot-samples/spring-boot-sample-jersey/src/main/java/sample/jersey/Endpoint.java index bcf89564ec0..0a1d6cd707d 100644 --- a/spring-boot-samples/spring-boot-sample-jersey/src/main/java/sample/jersey/Endpoint.java +++ b/spring-boot-samples/spring-boot-sample-jersey/src/main/java/sample/jersey/Endpoint.java @@ -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(); } } diff --git a/spring-boot-samples/spring-boot-sample-jersey/src/main/java/sample/jersey/Service.java b/spring-boot-samples/spring-boot-sample-jersey/src/main/java/sample/jersey/Service.java new file mode 100644 index 00000000000..ea93d50cf2a --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-jersey/src/main/java/sample/jersey/Service.java @@ -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; + } + +}