You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by GitBox <gi...@apache.org> on 2018/12/03 11:48:01 UTC

[GitHub] reta closed pull request #477: CXF-7908 - Allow to customise JAXRS server in spring boot when using …

reta closed pull request #477: CXF-7908 - Allow to customise JAXRS server in spring boot when using …
URL: https://github.com/apache/cxf/pull/477
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 6231de94dc4..9b5c3df1af4 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 @@
 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 @@
     @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 @@
     @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 0e9e4b83269..3c34b2a9399 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 @@
 
 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 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 void customInitParameters() {
         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 00000000000..78f62984d3a
--- /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 00000000000..a6ee9949302
--- /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 "";
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services