Predefined Responses

Predefined response beans are provided for all standard HTTP responses. These can be used as-is or extended to provide customized HTTP responses.

Examples:

@RestMethod(name="POST", path="/pets") public Ok addPet(@Body Pet pet) { addPet(Pet); // Predefined "200 OK" response bean. return new Ok(); // Could also use Ok.OK instance }

@RestMethod(name="POST", path="/pets") public SeeOther addPet(@Body Pet pet) { addPet(Pet); // Predefined "302 See Other" response bean with redirect to /pets. return new SeeOther("servlet:/pets"); }

These predefined response beans are an example of {@link oaj.http.annotation.Response @Response}-annotated objects that are describe in detail later. Without going into details, this is how the {@link oaj.http.response.SeeOther} is defined:

@Response( code=303 // Set automatically on response, description="See Other" // Used in generated Swagger ) public class SeeOther { private final String message; private final URI location; // Constructors omitted. // Used to populate Location response header. @ResponseHeader(name="Location") public URI getLocation() { return location; } // Used during serialization. @ResponseBody public String toString() { return message; } }

The {@link oajr.helper.SeeOtherRoot} class shows how these predefined beans can be extended.

@Response( description="Redirect to servlet root" // Override description in generated Swagger. ) public class SeeOtherServletRoot extends SeeOther { public SeeOtherServletRoot() { super(URI.create("servlet:/")); } }

Note that the runtime behavior of the following code is identical to the example above. However, the important distinction is that in the previous example, the 302 response would show in the generated Swagger (since we can see the response through reflection), whereas it will NOT show up in the following example (since all we see is an Object response).

@RestMethod(name="POST", path="/pets") public Object addPet(@Body Pet pet) { addPet(Pet); // Note the Object return type. return new SeeOther("servlet:/pets"); }