blob: 2e94798b21caf5add85fed613ba90c3501c80b77 [file] [log] [blame]
<!--
/***************************************************************************************************************************
* 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.
***************************************************************************************************************************/
-->
Child Resources
<p>
When using the <ja>@JuneauRestRoot</ja> annotation, servlet are given an instance of {@link oajr.springboot.SpringRestResourceResolver}.
The resource resolver gets passed down through the children hierarchy, allowing child resources to be defined as injectable Spring beans.
</p>
<h5 class='figure'>Example:</h5>
<p class='bpcode w800'>
<ja>@Configuration</ja>
<jk>public class</jk> AppConfiguration {
<ja>@Bean @JuneauRestRoot</ja>
<jk>public</jk> RootResource getRootResource() {
<jk>return new</jk> RootResource();
}
<ja>@Bean</ja>
<jk>public</jk> ChildResource getChildResource() {
<jk>return new</jk> ChildResource();
}
<ja>@Bean</ja>
<jk>public</jk> GrandChildResource getGrandChildResource() {
<jk>return new</jk> GrandChildResource();
}
}
</p>
<p>
The root resource class must extend from <c>HttpServlet</c> so that it can be registered as a normal
servlet using the Spring Boot architecture. The {@link oajr.BasicRestServletGroup} class is our router class
that extends from <c>HttpServlet</c>:
</p>
<p class='bpcode w800'>
<ja>@RestResource</ja>(
path=<js>"/root"</js>,
children={
ChildResource.<jk>class</jk>
}
)
<jk>public class</jk> RootResource <jk>extends</jk> BasicRestServletGroup {
<jc>// No code needed.</jc>
}
</p>
<p>
Because Spring Boot will automatically register any beans that extend from <c>HttpServlet</c>, we
DON'T want the child classes to extend from <c>HttpServlet</c>. Instead, we extend from
{@link oajr.BasicRestGroup} and {@link oajr.BasicRest} instead:
</p>
<p class='bpcode w800'>
<ja>@RestResource</ja>(
path=<js>"/child"</js>,
children={
GrandChildResource.<jk>class</jk>
}
)
<jk>public class</jk> ChildResource <jk>extends</jk> BasicRestGroup {
<jc>// No code needed.</jc>
}
</p>
<p class='bpcode w800'>
<ja>@RestResource</ja>(
path=<js>"/grandchild"</js>
)
<jk>public class</jk> GrandChildResource <jk>extends</jk> BasicRest {
<jc>// Injectable bean</jc>
}
</p>