{title:'REST Server Overview'}
@Rest-Annotated Resources

A REST resource is simply a Java class annotated with {@link oajr.annotation.Rest}. The most common case is a class that extends {@link oajr.servlet.BasicRestServlet}, which itself is simply an extension of {@link javax.servlet.http.HttpServlet} which allows it to be deployed as a servlet.

| // Sample REST resource that prints out a simple "Hello world!" message. | @Rest( | path="/helloWorld", | title="Hello World", | description="An example of the simplest-possible resource" | ) | @HtmlDoc( | navlinks={ | "up: request:/..", | "options: servlet:/?method=OPTIONS" | }, | aside={ | "<div style='max-width:400px' class='text'>", | " <p>This page shows a resource that simply response with a 'Hello world!' message</p>", | " <p>The POJO serialized is a simple String.</p>", | "</div>" | } | ) | @BeanConfig(sortProperties="true") | public class HelloWorldResource extends BasicRestServlet implements BasicUniveralConfig { | | @RestGet(path="/*", summary="Responds with \"Hello world!\"") | public String sayHello() { | return "Hello world!"; | } | }

This is what it looks like in a browser.

| http://localhost:10000/helloWorld

REST Children

Child Resources are REST servlets or objects that are linked to parent resources through the {@link oajr.annotation.Rest#children() @Rest(children)} annotation.

Example:

| /** Parent Resource */ | @Rest( | path="/parent", | children={ | MyChildResource.class | } | ) | public MyParentResource extends BasicRestServlet implements BasicUniversalConfig {...}

| /** Child Resource */ | @Rest( | path="/child" // Path relative to parent resource. | ) | public MyChildResource {...} // Note that we don't need to extend from RestServlet.

The path of the child resource gets appended to the path of the parent resource. So in the example above, the child resource is accessed through the URL /parent/child.

A HUGE advantage of using child resources is that they do not need to be declared in the JEE web.xml file. Initialization of and access to the child resources occurs through the parent resource. Children can be nested arbitrary deep to create complex REST interfaces with a single top-level REST servlet.

REST Group Pages

The {@link oajr.servlet.BasicRestServletGroup} class provides a default "router" page for child resources when a parent resource is nothing more than a grouping of child resources.

The RootResources class in the Samples project is an example of a router page:

| /** | * Sample REST resource showing how to implement a "router" resource page. | */ | @Rest( | path="/", | title="Root resources", | description="Example of a router resource page.", | children={ | HelloWorldResource.class, | PetStoreResource.class, | DtoExamples.class, | ConfigResource.class, | LogsResource.class, | ShutdownResource.class | } | ) | public class RootResources extends BasicRestServletGroup implements BasicUniversalConfig { | // NO CODE!!! | }

When you bring up this resource in a browser, you see the following that provides a list of navigable links to your child resources:

| http://localhost:10000

REST Resource Methods

The real power behind the REST server API is the ability to define Java methods as REST endpoints.

Example:

| @RestPost(path="/pets", guards=AdminGuard.class) | public Ok addPet( | @Content CreatePet createPetBean, | @Header("E-Tag") UUID etag, | @Query("debug") boolean debug | ) throws BadRequest, Unauthorized, InternalServerError { | // Process request. | return Ok.OK; | }

Java methods on {@link oajr.annotation.Rest @Rest}-annotated classes have the following format:

| @RestOp(method="...", path="...") | <config-annotations> | public <return-type> method(<args>) throws <throwables> { | ... | }

The various parts require their own topics to fully appreciate the scope of abilities, but the following is a summary:

Deploying as a Servlet

The {@link oajr.servlet.BasicRestServlet} class is the entry point for your REST resources. It extends directly from HttpServlet and is deployed like any other servlet (such as a standard web.xml file).

When the servlet init() method is called, it triggers the code to find and process the @Rest annotations on that class and all child classes. These get constructed into a {@link oajr.RestContext} object that holds all the configuration information about your resource in a read-only object.

Most developers are not going to be using the RestServlet class itself, and instead will extend from one of the preconfigured default servlets such as {@link oajr.servlet.BasicRestServlet} and {@link oajr.servlet.BasicRestServletGroup} which provides universal language support, basic instrumentation, and auto-generated Swagger UI.

Deploying in Spring Boot

The {@link oajr.springboot.BasicSpringRestServlet} class is typically entry point for your REST resources when working within a Spring Boot environment.

The SpringRestServlet class provides additional capabilities including:

Most developers are not going to be using the RestServlet class itself, and instead will extend from one of the preconfigured default servlets such as {@link oajr.springboot.BasicSpringRestServlet} and {@link oajr.springboot.BasicSpringRestServletGroup} that have the same capabilites as the {@link oajr.servlet.BasicRestServlet} and {@link oajr.servlet.BasicRestServletGroup} counterparts.

Example configuration file:

| @Configuration | public class MySpringConfiguration { | | /** | * Our root REST bean. | * Note that this must extend from SpringRestServlet so that child resources can be | * resolved as Spring beans. | * All REST objects are attached to this bean using the {@link oajr.annotation.Rest#children()} annotation. | */ | @Bean | public RootResources getRootResources() { | return new RootResources(); | } | | /** | * Optionally return the HelloWorldResource object as an injectable bean. | */ | @Bean | public HelloWorldResource getHelloWorldResource() { | return new HelloWorldResource(); | } | | /** | * Map our servlet to a path. | */ | @Bean | public ServletRegistrationBean<Servlet> getRootServlet(RootResources rootResources) { | return new ServletRegistrationBean<>(rootResources, "/*"); | } | }

| @Rest( | children={ | HelloWorldResource.class | } | ) | public class RootResources extends BasicSpringRestServletGroup implements BasicUniversalConfig { | // No code! | }

Additional Information