001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest.springboot; 014 015import org.apache.juneau.rest.*; 016import org.apache.juneau.rest.annotation.*; 017import org.springframework.context.ApplicationContext; 018 019/** 020 * Implementation of a {@link RestResourceResolver} for resolving resource classes using Spring. 021 * 022 * <p> 023 * Used for resolving resource classes defined via {@link Rest#children()}. 024 * 025 * <p> 026 * A typical usage pattern for registering a Juneau REST resource class is shown below: 027 * 028 * <p class='bpcode w800'> 029 * <ja>@Configuration</ja> 030 * <jk>public class</jk> MySpringConfiguration { 031 * 032 * <ja>@AutoWired</ja> 033 * <jk>private static volatile</jk> ApplicationContext <jsf>appContext</jsf>; 034 * 035 * <ja>@Bean</ja> 036 * <jk>public</jk> RestResourceResolver restResourceResolver(ApplicationContext appContext) { 037 * <jk>return new</jk> SpringRestResourceResolver(appContext); 038 * } 039 * 040 * <ja>@Bean</ja> 041 * <jk>public</jk> RootRest root(RestResourceResolver resolver) { 042 * <jk>return new</jk> RootRest().setRestResourceResolver(resolver); 043 * } 044 * 045 * <ja>@Bean</ja> 046 * <jk>public</jk> ServletRegistrationBean rootRegistration(RootRest root) { 047 * <jk>return new</jk> ServletRegistrationBean(root, <jsf>CONTEXT_ROOT</jsf>, <jsf>CONTEXT_ROOT</jsf>+<js>"/"</js>, <jsf>CONTEXT_ROOT</jsf>+<js>"/*"</js>); 048 * } 049 * </p> 050 */ 051public class SpringRestResourceResolver extends BasicRestResourceResolver { 052 053 private ApplicationContext ctx; 054 055 /** 056 * Constructor. 057 * 058 * @param ctx The spring application context object. 059 */ 060 public SpringRestResourceResolver(ApplicationContext ctx) { 061 this.ctx = ctx; 062 } 063 064 @Override /* RestResourceResolver */ 065 public <T> T resolve(Object parent, Class<T> c, RestContextBuilder builder, Object...args) { 066 T resource = null; 067 try { 068 resource = ctx.getBean(c); 069 } catch (Exception e) { /* Ignore */ } 070 if (resource == null) 071 resource = super.resolve(parent, c, builder); 072 return resource; 073 } 074}