@RestResource(path)
The {@link oajr.annotation.RestResource#path() @RestResource(path)} annotation is used in the following situations:
-
On child resources (resource classes attached to parents via the {@link oajr.annotation.RestResource#children() @RestResource(children)} annotation) to identify
the subpath used to access the child resource relative to the parent.
-
On top-level {@link oajr.RestServlet} classes deployed as Spring beans when {@link oajr.springboot.JuneauRestInitializer} is being used.
On child resources
The typical usage is to define a path to a child resource relative to the parent resource.
@RestResource(
children={
ChildResource.class
}
)
public class TopLevelResource extends BasicRestServlet {...}
@RestResource(
path="/child",
children={
GrandchildResource.class
}
)
public class ChildResource {...}
@RestResource(
path="/grandchild"
)
public class GrandchildResource {
@RestMethod(
path="/"
)
public String sayHello() {
return "Hello!";
}
}
In the example above, assuming the TopLevelResource servlet is deployed to path /myContext/myServlet,
then the sayHello method is accessible through the URI /myContext/myServlet/child/grandchild.
Note that in this scenario, the path attribute is not defined on the top-level resource.
Specifying the path on the top-level resource has no effect, but can be used for readability purposes.
On top-level resources deployed as Spring beans
The path can also be used on top-level resources deployed as Spring beans when used with the {@link oajr.springboot.JuneauRestInitializer}
Spring Boot initializer class:
@SpringBootApplication
@Controller
public class App {
// Our entry-point method.
public static void main(String[] args) {
new SpringApplicationBuilder(App.class)
.initializers(new JuneauRestInitializer(App.class))
.run(args);
}
// Our top-level servlet.
@Bean
@JuneauRestRoot
public MyResource getMyResource() {
return new MyResource();
}
}
@RestResource(
path="/myResource"
)
public class MyResource extends BasicRestServlet {...}
In this case, the servlet will get registered using the path defined on the resource class.
Path variables
The path can contain variables that get resolved to {@link oaj.http.annotation.Path @Path} parameters
or access through the {@link oajr.RestRequest#getPathMatch()} method.
@RestResource(
path="/myResource/{foo}/{bar}"
)
public class MyResource extends BasicRestServlet {
@RestMethod(
path="/{baz}"
)
public void String doX(@Path String foo, @Path int bar, @Path MyPojo baz) {
...
}
}
Variables can be used on either top-level or child resources and can be defined on multiple levels.
All variables in the path must be specified or else the target will not resolve and a 404 will result.
When variables are used on a path of a top-level resource deployed as a Spring bean in a Spring Boot application,
the first part of the URL must be a literal which will be used as the servlet path of the registered servlet.