This is an automated email from the ASF dual-hosted git repository.
reta pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/master by this push:
new 9bca695 CXF-7908 - Allow to customise JAXRS server in spring boot
when using cxf-spring-boot-autoconfigure (#477)
9bca695 is described below
commit 9bca6952c145b6ec6d2fb5726676ad7c3d67b7f7
Author: Maciej Swiderski <[email protected]>
AuthorDate: Mon Dec 3 12:47:59 2018 +0100
CXF-7908 - Allow to customise JAXRS server in spring boot when using
cxf-spring-boot-autoconfigure (#477)
---
.../boot/autoconfigure/CxfAutoConfiguration.java | 3 +
.../autoconfigure/CxfAutoConfigurationTests.java | 42 ++++++++++++--
.../cxf/spring/boot/jaxrs/CustomJaxRSServer.java | 65 ++++++++++++++++++++++
.../cxf/spring/boot/jaxrs/SampleJaxRSResource.java | 31 +++++++++++
4 files changed, 137 insertions(+), 4 deletions(-)
diff --git
a/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfiguration.java
b/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfiguration.java
index 6231de9..9b5c3df1 100644
---
a/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfiguration.java
+++
b/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfiguration.java
@@ -21,6 +21,7 @@ package org.apache.cxf.spring.boot.autoconfigure;
import java.util.Map;
import org.apache.cxf.bus.spring.SpringBus;
+import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.spring.SpringComponentScanServer;
import org.apache.cxf.jaxrs.spring.SpringJaxrsClassesScanServer;
@@ -81,6 +82,7 @@ public class CxfAutoConfiguration {
@Configuration
@ConditionalOnClass(JAXRSServerFactoryBean.class)
@ConditionalOnExpression("'${cxf.jaxrs.component-scan}'=='true' &&
'${cxf.jaxrs.classes-scan}'!='true'")
+ @ConditionalOnMissingBean(Server.class)
@Import(SpringComponentScanServer.class)
protected static class JaxRsComponentConfiguration {
@@ -89,6 +91,7 @@ public class CxfAutoConfiguration {
@Configuration
@ConditionalOnClass(JAXRSServerFactoryBean.class)
@ConditionalOnExpression("'${cxf.jaxrs.classes-scan}'=='true' &&
'${cxf.jaxrs.component-scan}'!='true'")
+ @ConditionalOnMissingBean(Server.class)
@Import(SpringJaxrsClassesScanServer.class)
protected static class JaxRsClassesConfiguration {
diff --git
a/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfigurationTests.java
b/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfigurationTests.java
index 0e9e4b8..3c34b2a 100644
---
a/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfigurationTests.java
+++
b/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfigurationTests.java
@@ -20,6 +20,9 @@ package org.apache.cxf.spring.boot.autoconfigure;
import java.util.Map;
+import org.apache.cxf.endpoint.Server;
+import org.apache.cxf.endpoint.ServerImpl;
+import org.apache.cxf.spring.boot.jaxrs.CustomJaxRSServer;
import org.hamcrest.Matcher;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.test.util.TestPropertyValues;
@@ -36,9 +39,9 @@ import org.junit.rules.ExpectedException;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasItem;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
-
-
+import static org.junit.Assert.assertTrue;
/**
* Tests for {@link CxfAutoConfiguration}.
@@ -112,14 +115,45 @@ public class CxfAutoConfigurationTests {
assertThat(registrationBean.getInitParameters(), v1);
assertThat(registrationBean.getInitParameters(), v2);
}
+
+ @Test
+ public void customizedJaxRsServer() {
+ load(new Class<?>[] {CxfAutoConfiguration.class,
CustomJaxRSServer.class},
+ "cxf.jaxrs.classes-scan=true",
+
"cxf.jaxrs.classes-scan-packages=org.apache.cxf.spring.boot.jaxrs");
+ Map<String, Server> beans =
+ this.context.getBeansOfType(Server.class);
+ assertThat(beans.size(),
+ equalTo(1));
+
+ Object serverInstance = beans.values().iterator().next();
+ assertFalse(serverInstance instanceof ServerImpl);
+ }
+
+ @Test
+ public void defaultJaxRsServer() {
+ load(CxfAutoConfiguration.class,
+ "cxf.jaxrs.classes-scan=true",
+
"cxf.jaxrs.classes-scan-packages=org.apache.cxf.spring.boot.jaxrs");
+ Map<String, Server> beans =
+ this.context.getBeansOfType(Server.class);
+ assertThat(beans.size(),
+ equalTo(1));
+
+ Object serverInstance = beans.values().iterator().next();
+ assertTrue(serverInstance instanceof ServerImpl);
+ }
private void load(Class<?> config, String... environment) {
+ load(new Class<?>[] {config}, environment);
+ }
+
+ private void load(Class<?>[] configs, String... environment) {
AnnotationConfigWebApplicationContext ctx = new
AnnotationConfigWebApplicationContext();
ctx.setServletContext(new MockServletContext());
TestPropertyValues.of(environment).applyTo(ctx);
- ctx.register(config);
+ ctx.register(configs);
ctx.refresh();
this.context = ctx;
}
-
}
diff --git
a/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/jaxrs/CustomJaxRSServer.java
b/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/jaxrs/CustomJaxRSServer.java
new file mode 100644
index 0000000..78f6298
--- /dev/null
+++
b/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/jaxrs/CustomJaxRSServer.java
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.cxf.spring.boot.jaxrs;
+
+import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.endpoint.Server;
+import org.apache.cxf.jaxrs.spring.AbstractJaxrsClassesScanServer;
+import org.apache.cxf.transport.Destination;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class CustomJaxRSServer extends AbstractJaxrsClassesScanServer {
+
+ @Bean
+ public Server jaxRsServer() {
+ return new Server() {
+
+ @Override
+ public void stop() {
+ }
+
+ @Override
+ public void start() {
+ }
+
+ @Override
+ public boolean isStarted() {
+ return false;
+ }
+
+ @Override
+ public Endpoint getEndpoint() {
+ return null;
+ }
+
+ @Override
+ public Destination getDestination() {
+ return null;
+ }
+
+ @Override
+ public void destroy() {
+
+ }
+ };
+ }
+
+}
diff --git
a/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/jaxrs/SampleJaxRSResource.java
b/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/jaxrs/SampleJaxRSResource.java
new file mode 100644
index 0000000..a6ee994
--- /dev/null
+++
b/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/jaxrs/SampleJaxRSResource.java
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.cxf.spring.boot.jaxrs;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+
+@Path("/sample")
+public class SampleJaxRSResource {
+
+ @GET
+ public String test() {
+ return "";
+ }
+}