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"); }

- {@link oaj.http.response}
  - {@link oaj.http.response.Accepted}
  - {@link oaj.http.response.AlreadyReported}
  - {@link oaj.http.response.Continue}
  - {@link oaj.http.response.Created}
  - {@link oaj.http.response.EarlyHints}
  - {@link oaj.http.response.Found}
  - {@link oaj.http.response.IMUsed}
  - {@link oaj.http.response.MovedPermanently}
  - {@link oaj.http.response.MultipleChoices}
  - {@link oaj.http.response.MultiStatus}
  - {@link oaj.http.response.NoContent}
  - {@link oaj.http.response.NonAuthoritiveInformation}
  - {@link oaj.http.response.NotModified}
  - {@link oaj.http.response.Ok}
  - {@link oaj.http.response.PartialContent}
  - {@link oaj.http.response.PermanentRedirect}
  - {@link oaj.http.response.Processing}
  - {@link oaj.http.response.ResetContent}
  - {@link oaj.http.response.SeeOther}
  - {@link oaj.http.response.SwitchingProtocols}
  - {@link oaj.http.response.TemporaryRedirect}
  - {@link oaj.http.response.UseProxy}

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"); }