You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2018/11/21 16:53:04 UTC

[juneau] branch master updated: Add spring-boot methods to RestServlet.

This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new b39c665  Add spring-boot methods to RestServlet.
b39c665 is described below

commit b39c6653c2ec8046291f278042946f3860ebd5e7
Author: JamesBognar <ja...@apache.org>
AuthorDate: Wed Nov 21 11:52:51 2018 -0500

    Add spring-boot methods to RestServlet.
---
 juneau-doc/docs/ReleaseNotes/8.0.0.html            |  18 +++++++++
 .../Topics/13.juneau-examples-rest-springboot.html |   5 ++-
 .../01.Classes.html                                |  40 ++++++++++++++++++-
 .../juneau-examples-rest-springboot.Classes.png    | Bin 0 -> 26502 bytes
 .../juneau/examples/rest/springboot/App.java       |   1 -
 .../rest/springboot/AppServletConfiguration.java   |   4 +-
 .../apache/juneau/examples/rest/RootResources.java |  38 +-----------------
 .../apache/juneau/microservice/Microservice.java   |   9 +++++
 .../java/org/apache/juneau/rest/RestContext.java   |   3 +-
 .../java/org/apache/juneau/rest/RestServlet.java   |  43 +++++++++++++++++++--
 10 files changed, 112 insertions(+), 49 deletions(-)

diff --git a/juneau-doc/docs/ReleaseNotes/8.0.0.html b/juneau-doc/docs/ReleaseNotes/8.0.0.html
index dc972ae..0f19fb2 100644
--- a/juneau-doc/docs/ReleaseNotes/8.0.0.html
+++ b/juneau-doc/docs/ReleaseNotes/8.0.0.html
@@ -31,6 +31,24 @@
 		</ul>
 </ul>
 
+<h5 class='topic w800'>juneau-server</h5>
+<ul class='spaced-list'>
+	<li>
+		New methods on {@link oajr.RestServlet}:
+		<ul class='doctree'>
+			<li class='jm'>{@link oajr.RestServlet#setRestResourceResolver(RestResourceResolver) setRestResourceResolver(RestResourceResolver)}
+			<li class='jm'>{@link oajr.RestServlet#getPath() getPath()}
+		</ul>
+</ul>
+
+<h5 class='topic w800'>juneau-microservice-server</h5>
+<ul class='spaced-list'>
+	<li>
+		If the microservice cannot find a config file in the home directory with the same name as the jar and
+		the <js>"juneau.configFile"</js> system property is not found, it will try to find any file in the home
+		directory that ends with <js>".cfg"</js>.
+</ul>
+
 <h5 class='topic w800'>juneau-examples-rest</h5>
 <ul class='spaced-list'>
 	<li>
diff --git a/juneau-doc/docs/Topics/13.juneau-examples-rest-springboot.html b/juneau-doc/docs/Topics/13.juneau-examples-rest-springboot.html
index adcb468..46b648e 100644
--- a/juneau-doc/docs/Topics/13.juneau-examples-rest-springboot.html
+++ b/juneau-doc/docs/Topics/13.juneau-examples-rest-springboot.html
@@ -6,7 +6,7 @@
  * with the License.  You may obtain a copy of the License at
  *  
  *  http://www.apache.org/licenses/LICENSE-2.0
- *  
+ *  As 
  * 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.
@@ -41,4 +41,7 @@
 <p>
 	The microservice can be started from the <code>juneau-examples-rest-springboot.launch</code> file. 
 	It will start up the microservice on port 5000 which you can then view through a browser.
+</p>
+<p>
+	The Spring Boot examples app has identical functionality to the normal examples app except for console command support.
 </p>
\ No newline at end of file
diff --git a/juneau-doc/docs/Topics/13.juneau-examples-rest-springboot/01.Classes.html b/juneau-doc/docs/Topics/13.juneau-examples-rest-springboot/01.Classes.html
index 420d290..d569c44 100644
--- a/juneau-doc/docs/Topics/13.juneau-examples-rest-springboot/01.Classes.html
+++ b/juneau-doc/docs/Topics/13.juneau-examples-rest-springboot/01.Classes.html
@@ -13,8 +13,44 @@
  ***************************************************************************************************************************/
  -->
 
-{todo} Classes
+{new} Classes
 
 <p>
-	TODO
+	The <code>juneau-examples-rest-springboot</code> project contains the same classes as found in <code>juneau-examples-rest</code>
+	with the inclusion of a <code>org.apache.juneau.examples.rest.springboot</code> package containing the following:
 </p>
+<img class='bordered w800' src='doc-files/juneau-examples-rest-springboot.Classes.png'>
+<p>
+	The <code>App</code> class is used to invoke our application using Spring:
+</p>
+<p class='bpcode w800'>
+	<ja>@SpringBootApplication</ja>
+	<ja>@Controller</ja>
+	<ja>@Import</ja>({AppConfiguration.<jk>class</jk>, AppServletConfiguration.<jk>class</jk>})
+	<jk>public class</jk> App {
+	
+		<jk>private static volatile</jk> ConfigurableApplicationContext <jsf>context</jsf>;
+	
+		<jk>public static void</jk> main(String[] args) {
+			<jk>try</jk> {
+				<jsm>setProperty</jsm>(<js>"juneau.configFile"</js>, <js>"examples.cfg"</js>, <jk>false</jk>);
+				<jsf>context</jsf> = SpringApplication.<jsm>run</jsm>(App.<jk>class</jk>, args);
+				<jk>if</jk> (<jsf>context</jsf> == <jk>null</jk>)
+					System.<jsm>exit</jsm>(2); <jc>// Probably port in use?</jc>
+				AppConfiguration.<jsm>setAppContext</jsm>(context);
+				<jsm>setProperty</jsm>(<js>"juneau.serverPort"</js>, <jsf>context</jsf>.getEnvironment().getProperty(<js>"server.port"</js>), <jk>false</jk>);
+			} <jk>catch</jk> (Exception e) {
+				e.printStackTrace();
+			}
+		}
+	
+		<jk>public static void</jk> start() {
+			<jsm>main</jsm>(<jk>new</jk> String[0]);
+		}
+	
+		<jk>public static void</jk> stop() {
+			<jsf>context</jsf>.stop();
+		}
+	}
+</p>
+
diff --git a/juneau-doc/docs/Topics/13.juneau-examples-rest-springboot/doc-files/juneau-examples-rest-springboot.Classes.png b/juneau-doc/docs/Topics/13.juneau-examples-rest-springboot/doc-files/juneau-examples-rest-springboot.Classes.png
new file mode 100644
index 0000000..9310ea9
Binary files /dev/null and b/juneau-doc/docs/Topics/13.juneau-examples-rest-springboot/doc-files/juneau-examples-rest-springboot.Classes.png differ
diff --git a/juneau-examples/juneau-examples-rest-springboot/src/main/java/org/apache/juneau/examples/rest/springboot/App.java b/juneau-examples/juneau-examples-rest-springboot/src/main/java/org/apache/juneau/examples/rest/springboot/App.java
index 69c31ff..4168655 100644
--- a/juneau-examples/juneau-examples-rest-springboot/src/main/java/org/apache/juneau/examples/rest/springboot/App.java
+++ b/juneau-examples/juneau-examples-rest-springboot/src/main/java/org/apache/juneau/examples/rest/springboot/App.java
@@ -28,7 +28,6 @@ import org.springframework.stereotype.*;
 @Import({AppConfiguration.class, AppServletConfiguration.class})
 public class App {
 
-	public static int counter = 0;
 	private static volatile ConfigurableApplicationContext context;
 
 	public static void main(String[] args) {
diff --git a/juneau-examples/juneau-examples-rest-springboot/src/main/java/org/apache/juneau/examples/rest/springboot/AppServletConfiguration.java b/juneau-examples/juneau-examples-rest-springboot/src/main/java/org/apache/juneau/examples/rest/springboot/AppServletConfiguration.java
index 55d6f80..c93564f 100644
--- a/juneau-examples/juneau-examples-rest-springboot/src/main/java/org/apache/juneau/examples/rest/springboot/AppServletConfiguration.java
+++ b/juneau-examples/juneau-examples-rest-springboot/src/main/java/org/apache/juneau/examples/rest/springboot/AppServletConfiguration.java
@@ -28,12 +28,12 @@ public class AppServletConfiguration {
 
 	@Bean
 	public RootResources root(RestResourceResolver resolver) {
-		return new RootResources(resolver);
+		return (RootResources)new RootResources().setRestResourceResolver(resolver);
 	}
 
 	@Bean
 	public ServletRegistrationBean<RootResources> rootRegistration(RootResources root) {
-		return new ServletRegistrationBean<>(root, CONTEXT_ROOT, CONTEXT_ROOT+"/", CONTEXT_ROOT+"/*");
+		return new ServletRegistrationBean<>(root, CONTEXT_ROOT, CONTEXT_ROOT+"/", CONTEXT_ROOT+"/"+root.getPath());
 	}
 
 	/**
diff --git a/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/RootResources.java b/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/RootResources.java
index 4a14e89..6bfedb1 100644
--- a/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/RootResources.java
+++ b/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/RootResources.java
@@ -12,16 +12,12 @@
 // ***************************************************************************************************************************
 package org.apache.juneau.examples.rest;
 
-import static org.apache.juneau.rest.annotation.HookEvent.*;
 import static org.apache.juneau.serializer.WriterSerializer.*;
 
-import javax.inject.*;
-
 import org.apache.juneau.examples.rest.dto.*;
 import org.apache.juneau.examples.rest.petstore.rest.*;
 import org.apache.juneau.microservice.*;
 import org.apache.juneau.microservice.resources.*;
-import org.apache.juneau.rest.*;
 import org.apache.juneau.rest.annotation.*;
 import org.apache.juneau.rest.widget.*;
 
@@ -29,7 +25,7 @@ import org.apache.juneau.rest.widget.*;
  * Sample REST resource showing how to implement a "router" resource page.
  */
 @RestResource(
-	path="/",
+	path="/*",
 	title="Root resources",
 	description="Example of a router resource page.",
 	htmldoc=@HtmlDoc(
@@ -75,36 +71,4 @@ public class RootResources extends BasicRestServletJenaGroup {
 	// It allows you to remove the Jena prerequisite.
 
 	private static final long serialVersionUID = 1L;
-
-	private RestResourceResolver resolver;
-
-	/**
-	 * Constructor used when using Jetty.
-	 */
-	public RootResources() {
-		this(null);
-	}
-
-	/**
-	 * Used when constructed via Spring.
-	 *
-	 * @param resolver The bean resolver for child resources.
-	 */
-	@Inject
-	public RootResources(RestResourceResolver resolver) {
-		this.resolver = resolver;
-	}
-
-	/**
-	 * Used when constructed via Spring.
-	 * <p>
-	 * Sets the resource resolver passed in through the constructor.
-	 *
-	 * @param config
-	 * @throws Exception
-	 */
-	@RestHook(INIT)
-	public void init(RestContextBuilder config) throws Exception {
-		config.resourceResolver(resolver);
-	}
 }
diff --git a/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/Microservice.java b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/Microservice.java
index 026e362..ef7bf2b 100755
--- a/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/Microservice.java
+++ b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/Microservice.java
@@ -575,6 +575,15 @@ public abstract class Microservice implements ConfigEventListener {
 			}
 
 			if (cfPath == null) {
+				for (File f : new File(".").listFiles()) {
+					if (f.getName().endsWith(".cfg")) {
+						cfPath = f.getName();
+						break;
+					}
+				}
+			}
+
+			if (cfPath == null) {
 				cf = cfb.build();
 			} else {
 				cf = cfb.name(cfPath).varResolver(createVarResolver().build()).build();
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
index a142ba0..159b1e6 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
@@ -3866,8 +3866,7 @@ public final class RestContext extends BeanContext {
 	 * {@link RestContextBuilder#path(String)} method concatenated with those on all parent classes.
 	 *
 	 * <p>
-	 * If path is not specified, returns <js>"/"</js>.
-	 * <br>Path always starts with <js>"/"</js>.
+	 * If path is not specified, returns <js>""</js>.
 	 *
 	 * <h5 class='section'>See Also:</h5>
 	 * <ul>
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestServlet.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestServlet.java
index 1427cea..d49236e 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestServlet.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestServlet.java
@@ -23,6 +23,8 @@ import java.util.logging.*;
 import javax.servlet.*;
 import javax.servlet.http.*;
 
+import org.apache.juneau.internal.*;
+import org.apache.juneau.rest.annotation.*;
 import org.apache.juneau.rest.exception.*;
 
 /**
@@ -41,13 +43,18 @@ public abstract class RestServlet extends HttpServlet {
 	private volatile RestContext context;
 	private volatile Exception initException;
 	private boolean isInitialized = false;  // Should not be volatile.
+	private volatile RestResourceResolver resourceResolver;
+
 
 	@Override /* Servlet */
 	public final synchronized void init(ServletConfig servletConfig) throws ServletException {
 		try {
 			if (context != null)
 				return;
-			builder = RestContext.create(servletConfig, this.getClass(), null).init(this);
+			builder = RestContext.create(servletConfig, this.getClass(), null);
+			if (resourceResolver != null)
+				builder.resourceResolver(resourceResolver);
+			builder.init(this);
 			super.init(servletConfig);
 			builder.servletContext(this.getServletContext());
 			setContext(builder.build());
@@ -92,6 +99,34 @@ public abstract class RestServlet extends HttpServlet {
 		context.postInit();
 	}
 
+	/**
+	 * Sets the resource resolver to use for this servlet and all child servlets.
+	 * <p>
+	 * This method can be called immediately following object construction, but must be called before {@link #init(ServletConfig)} is called.
+	 * Otherwise calling this method will have no effect.
+	 *
+	 * @param resourceResolver The resolver instance.  Can be <jk>null</jk>.
+	 * @return This object (for method chaining).
+	 */
+	public synchronized RestServlet setRestResourceResolver(RestResourceResolver resourceResolver) {
+		this.resourceResolver = resourceResolver;
+		return this;
+	}
+
+	/**
+	 * Returns the path defined on this servlet if it's defined via {@link RestResource#path()}.
+	 *
+	 * @return The path defined on this servlet, or an empty string if not specified.
+	 */
+	public synchronized String getPath() {
+		if (context != null)
+			return context.getPath();
+		for (RestResource rr : ClassUtils.getAnnotations(RestResource.class, this.getClass()))
+			if (! rr.path().isEmpty())
+				return trimSlashes(rr.path());
+		return "";
+	}
+
 	@Override /* GenericServlet */
 	public synchronized RestContextBuilder getServletConfig() {
 		return builder;
@@ -213,7 +248,7 @@ public abstract class RestServlet extends HttpServlet {
 	 * @return The current HTTP request, or <jk>null</jk> if it wasn't created.
 	 */
 	public RestRequest getRequest() {
-		if (context == null) 
+		if (context == null)
 			return null;
 		return context.getRequest();
 	}
@@ -224,7 +259,7 @@ public abstract class RestServlet extends HttpServlet {
 	 * @return The current HTTP response, or <jk>null</jk> if it wasn't created.
 	 */
 	public RestResponse getResponse() {
-		if (context == null) 
+		if (context == null)
 			return null;
 		return context.getResponse();
 	}
@@ -238,7 +273,7 @@ public abstract class RestServlet extends HttpServlet {
 
 	/**
 	 * Convenience method for calling <code>getContext().getProperties();</code>
-	 * 
+	 *
 	 * @return The resource properties as an {@link RestContextProperties}.
 	 * @see RestContext#getProperties()
 	 */