All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.juneau.rest.annotation.RestResource Maven / Gradle / Ivy

There is a newer version: 9.0.1
Show newest version
// ***************************************************************************************************************************
// * 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.                                              *
// ***************************************************************************************************************************
package org.apache.juneau.rest.annotation;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;

import java.lang.annotation.*;

import javax.servlet.http.*;

import org.apache.juneau.*;
import org.apache.juneau.encoders.Encoder;
import org.apache.juneau.ini.*;
import org.apache.juneau.json.*;
import org.apache.juneau.parser.*;
import org.apache.juneau.rest.*;
import org.apache.juneau.serializer.*;
import org.apache.juneau.transform.*;
import org.apache.juneau.utils.*;
import org.apache.juneau.xml.*;

/**
 * Used to denote that a class is a REST resource and to associate metadata on it.
 *
 * 

* Usually used on a subclass of {@link RestServlet}, but can be used to annotate any class that you want to expose as * a REST resource. * * Refer to org.apache.juneau.rest doc for information on * using this class. */ @Documented @Target(TYPE) @Retention(RUNTIME) @Inherited public @interface RestResource { /** * Identifies the location of the resource bundle for this class. * *

* This annotation is used to provide localized messages for the following methods: *

    *
  • {@link RestRequest#getMessage(String, Object...)} *
  • {@link RestContext#getMessages()} *
* *

* Refer to the {@link MessageBundle} class for a description of the message key formats used in the properties file. * *

* The value can be a relative path like "nls/Messages", indicating to look for the resource bundle * "com.foo.sample.nls.Messages" if the resource class is in "com.foo.sample", or it can be an * absolute path, like "com.foo.sample.nls.Messages" */ String messages() default ""; /** * Class-level guards. * *

* Associates one or more {@link RestGuard RestGuards} with all REST methods defined in this class. * These guards get called immediately before execution of any REST method in this class. * *

* Typically, guards will be used for permissions checking on the user making the request, but it can also be used * for other purposes like pre-call validation of a request. * *

* The programmatic equivalent to this annotation are the {@link RestConfig#addGuards(Class...)}/ * {@link RestConfig#addGuards(RestGuard...)} methods. */ Class[] guards() default {}; /** * Class-level converters. * *

* Associates one or more {@link RestConverter converters} with a resource class. * These converters get called immediately after execution of the REST method in the same order specified in the * annotation. * *

* Can be used for performing post-processing on the response object before serialization. * *

* Default converter implementations are provided in the org.apache.juneau.rest.converters package. * *

* The programmatic equivalent to this annotation are the {@link RestConfig#addConverters(Class...)}/ * {@link RestConfig#addConverters(RestConverter...)} methods. */ Class[] converters() default {}; /** * Class-level bean filters. * *

* Shortcut to add bean filters to the bean contexts of the objects returned by the following methods: *

    *
  • {@link RestContext#getBeanContext()} *
  • {@link RestContext#getSerializers()} *
  • {@link RestContext#getParsers()} *
* *

* If the specified class is an instance of {@link BeanFilterBuilder}, then a filter built from that builder is added. * Any other classes are wrapped in a {@link InterfaceBeanFilterBuilder} to indicate that subclasses should be * treated as the specified class type. * *

* The programmatic equivalent to this annotation is the {@link RestConfig#addBeanFilters(Class...)} method. */ Class[] beanFilters() default {}; /** * Class-level POJO swaps. * *

* Shortcut to add POJO swaps to the bean contexts of the objects returned by the following methods: *

    *
  • {@link RestContext#getBeanContext()} *
  • {@link RestContext#getSerializers()} *
  • {@link RestContext#getParsers()} *
* *

* If the specified class is an instance of {@link PojoSwap}, then that swap is added. * Any other classes are wrapped in a {@link SurrogateSwap}. * *

* The programmatic equivalent to this annotation is the {@link RestConfig#addPojoSwaps(Class...)} method. */ Class[] pojoSwaps() default {}; /** * Class-level Java method parameter resolvers. * *

* By default, the Juneau framework will automatically Java method parameters of various types (e.g. * RestRequest, Accept, Reader). * This annotation allows you to provide your own resolvers for your own class types that you want resolved. * *

* For example, if you want to pass in instances of MySpecialObject to your Java method, define * the following resolver: *

* public class MyRestParam extends RestParam { * * // Must have no-arg constructor! * public MyRestParam() { * // First two parameters help with Swagger doc generation. * super(QUERY, "myparam", MySpecialObject.class); * } * * // The method that creates our object. * // In this case, we're taking in a query parameter and converting it to our object. * public Object resolve(RestRequest req, RestResponse res) throws Exception { * return new MySpecialObject(req.getQuery().get("myparam")); * } * } *

* *

* Note:{@link RestParam} classes must have no-arg constructors. * *

* The programmatic equivalent to this annotation is the {@link RestConfig#addParamResolvers(Class...)} method. */ Class[] paramResolvers() default {}; /** * Class-level properties. * *

* Shortcut for specifying class-level properties on this servlet to the objects returned by the following methods: *

    *
  • {@link RestContext#getBeanContext()} *
  • {@link RestContext#getSerializers()} *
  • {@link RestContext#getParsers()} *
*

* Any of the following property names can be specified: *

    *
  • {@link RestContext} *
  • {@link BeanContext} *
  • {@link SerializerContext} *
  • {@link ParserContext} *
  • {@link JsonSerializerContext} *
  • {@link XmlSerializerContext} *
  • {@link XmlParserContext} *
* *

* Property values will be converted to the appropriate type. * *

* In some cases, properties can be overridden at runtime through the * {@link RestResponse#setProperty(String, Object)} method or through a {@link Properties @Properties} annotated * method parameter. * *

* The programmatic equivalent to this annotation are the {@link RestConfig#setProperty(String, Object)}/ * {@link RestConfig#setProperties(java.util.Map)} methods. */ Property[] properties() default {}; /** * Shortcut for setting {@link #properties()} of simple boolean types. * *

* Setting a flag is equivalent to setting the same property to "true". */ String[] flags() default {}; /** * Specifies a list of {@link Serializer} classes to add to the list of serializers available for this servlet. * *

* This annotation can only be used on {@link Serializer} classes that have no-arg constructors. * *

* The programmatic equivalent to this annotation are the {@link RestConfig#addSerializers(Class...)}/ * {@link RestConfig#addSerializers(Serializer...)} methods. */ Class[] serializers() default {}; /** * Specifies a list of {@link Parser} classes to add to the list of parsers available for this servlet. * *

* This annotation can only be used on {@link Parser} classes that have no-arg constructors. * *

* The programmatic equivalent to this annotation are the {@link RestConfig#addParsers(Class...)}/ * {@link RestConfig#addParsers(Parser...)} methods. */ Class[] parsers() default {}; /** * Specifies a list of {@link ResponseHandler} classes that know how to convert POJOs returned by REST methods or * set via {@link RestResponse#setOutput(Object)} into appropriate HTTP responses. * *

* See {@link ResponseHandler} for details. * *

* The programmatic equivalent to this annotation are the {@link RestConfig#addResponseHandlers(Class...)}/ * {@link RestConfig#addResponseHandlers(ResponseHandler...)} methods. */ Class[] responseHandlers() default {}; /** * Specifies a list of {@link Encoder} to associate with this servlet. * *

* These can be used to enable various kinds of compression (e.g. "gzip") on requests and responses. * *

* This annotation can only be used on {@link Encoder} classes that have no-arg constructors. * *

Example:
*

* // Servlet with automated support for GZIP compression * @RestResource(encoders={GzipEncoder.class}) * public MyRestServlet extends RestServlet { * ... * } *

* *

* The programmatic equivalent to this annotation are the {@link RestConfig#addEncoders(Class...)}/ * {@link RestConfig#addEncoders(Encoder...)} methods. */ Class[] encoders() default {}; /** * Specifies default values for request headers. * *

* Strings are of the format "Header-Name: header-value". * *

* Affects values returned by {@link RestRequest#getHeader(String)} when the header is not present on the request. * *

* The most useful reason for this annotation is to provide a default Accept header when one is not * specified so that a particular default {@link Serializer} is picked. * *

* Only one header value can be specified per entry (i.e. it's not a delimited list of header entries). * *

Example:
*

* // Assume "text/json" Accept value when Accept not specified * @RestResource(defaultRequestHeaders={"Accept: text/json"}) * public MyRestServlet extends RestServlet { * ... * } *

* *

* The programmatic equivalent to this annotation are the {@link RestConfig#addDefaultRequestHeader(String, Object)}/ * {@link RestConfig#addDefaultRequestHeaders(String...)} methods. */ String[] defaultRequestHeaders() default {}; /** * Specifies default values for response headers. * *

* Strings are of the format "Header-Name: header-value". * *

* This is equivalent to calling {@link RestResponse#setHeader(String, String)} programmatically in each of the Java * methods. * *

* The header value will not be set if the header value has already been specified (hence the 'default' in the name). * *

* Only one header value can be specified per entry (i.e. it's not a delimited list of header entries). * *

Example:
*

* // Add a version header attribute to all responses * @RestResource(defaultResponseHeaders={"X-Version: 1.0"}) * public MyRestServlet extends RestServlet { * ... * } *

* *

* The programmatic equivalent to this annotation are the {@link RestConfig#addDefaultResponseHeader(String, Object)}/ * {@link RestConfig#addDefaultResponseHeaders(String...)} methods. */ String[] defaultResponseHeaders() default {}; /** * Defines children of this resource. * *

* A REST child resource is simply another servlet that is initialized as part of the parent resource and has a * servlet path directly under the parent servlet path. * The main advantage to defining servlets as REST children is that you do not need to define them in the * web.xml file of the web application. * This can cut down on the number of entries that show up in the web.xml file if you are defining * large numbers of servlets. * *

* Child resources must specify a value for {@link #path()} that identifies the subpath of the child resource * relative to the parent path. * *

* It should be noted that servlets can be nested arbitrarily deep using this technique (i.e. children can also have * children). * *

*
Servlet initialization:
*
*

* A child resource will be initialized immediately after the parent servlet is initialized. * The child resource receives the same servlet config as the parent resource. * This allows configuration information such as servlet initialization parameters to filter to child * resources. *

*
*
Runtime behavior:
*
*

* As a rule, methods defined on the HttpServletRequest object will behave as if the child * servlet were deployed as a top-level resource under the child's servlet path. * For example, the getServletPath() and getPathInfo() methods on the * HttpServletRequest object will behave as if the child resource were deployed using the * child's servlet path. * Therefore, the runtime behavior should be equivalent to deploying the child servlet in the * web.xml file of the web application. *

*
*
* *

* The programmatic equivalent to this annotation are the {@link RestConfig#addChildResource(String, Object)}/ * {@link RestConfig#addChildResources(Class...)}/{@link RestConfig#addChildResources(Object...)} methods. */ Class[] children() default {}; /** * Identifies the URL subpath relative to the parent resource. * *

* Typically, this annotation is only applicable to resources defined as children through the {@link #children()} * annotation. * However, it may be used in other ways (e.g. defining paths for top-level resources in microservices). * *

* This annotation is ignored on top-level servlets (i.e. servlets defined in web.xml files). * Therefore, implementers can optionally specify a path value for documentation purposes. * *

* The programmatic equivalent to this annotation is the {@link RestConfig#setPath(String)} method. */ String path() default ""; /** * Optional site name. * *

* The site name is intended to be a title that can be applied to the entire site. * *

* This value can be retrieved programmatically through the {@link RestRequest#getSiteName()} method. * *

* One possible use is if you want to add the same title to the top of all pages by defining a header on a * common parent class like so: *

* htmldoc=@HtmlDoc( * header={ * "<h1>$R{siteName}</h1>", * "<h2>$R{servletTitle}</h2>" * } * ) *

* *

* This field can contain variables (e.g. "$L{my.localized.variable}"). *
See {@link RestContext#getVarResolver()} for the list of supported variables. * *

* The programmatic equivalent to this annotation is the {@link RestInfoProvider#getSiteName(RestRequest)} method. */ String siteName() default ""; /** * Optional servlet title. * *

* It is used to populate the Swagger title field. * This value can be retrieved programmatically through the {@link RestRequest#getServletTitle()} method. * *

* The default value pulls the label from the label entry in the servlet resource bundle. * (e.g. "title = foo" or "MyServlet.title = foo"). * *

* This field can contain variables (e.g. "$L{my.localized.variable}"). *
See {@link RestContext#getVarResolver()} for the list of supported variables. * *

* Corresponds to the swagger field /info/title. * *

* The programmatic equivalent to this annotation is the {@link RestInfoProvider#getTitle(RestRequest)} method. */ String title() default ""; /** * Optional servlet description. * *

* It is used to populate the Swagger description field. * This value can be retrieved programmatically through the {@link RestRequest#getServletDescription()} method. * *

* The default value pulls the description from the description entry in the servlet resource bundle. * (e.g. "description = foo" or "MyServlet.description = foo"). * *

* This field can contain variables (e.g. "$L{my.localized.variable}"). *
See {@link RestContext#getVarResolver()} for the list of supported variables. * *

* Corresponds to the swagger field /info/description. * *

* The programmatic equivalent to this annotation is the {@link RestInfoProvider#getDescription(RestRequest)} method. */ String description() default ""; /** * Optional location of configuration file for this servlet. * *

* The configuration file . * *

* This field can contain variables (e.g. "$L{my.localized.variable}"). *
See {@link RestContext#getVarResolver()} for the list of supported variables. * *

* The programmatic equivalent to this annotation is the {@link RestConfig#setConfigFile(ConfigFile)} method. */ String config() default ""; /** * Defines paths and locations of statically served files. * *

* This is a JSON map of paths to packages/directories located on either the classpath or working directory. * *

* Mappings are cumulative from parent to child. Child resources can override mappings made on parent resources. * *

* If the file cannot be located, the request will return {@link HttpServletResponse#SC_NOT_FOUND}. * *

Example:
*

* package com.foo.mypackage; * * @RestResource( * path="/myresource", * staticFiles="{htdocs:'docs'}" * ) * public class MyResource extends RestServletDefault { * } *

* *

* In this example, given a GET request to /myresource/htdocs/foobar.html, the servlet will attempt to * find the foobar.html file in the following ordered locations: *

    *
  1. com.foo.mypackage.docs package. *
  2. org.apache.juneau.rest.docs package (since RestServletDefault is in * org.apache.juneau.rest). *
  3. [working-dir]/docs directory. *
* *

* The programmatic equivalent to this annotation is the {@link RestConfig#addStaticFiles(Class, String)} method. */ String staticFiles() default ""; /** * Specifies the HTTP header name used to identify the client version. * *

* The client version is used to support backwards compatibility for breaking REST interface changes. * Used in conjunction with {@link RestMethod#clientVersion()} annotation. * *

* If not specified, uses "X-Client-Version". * *

* The programmatic equivalent to this annotation is the {@link RestConfig#setClientVersionHeader(String)} method. */ String clientVersionHeader() default ""; /** * Specifies the resolver class to use for resolving child resources by class name. * *

* The default implementation simply instantiates the class using one of the following constructors: *

    *
  • public T(RestConfig) *
  • public T() *
* *

* The former constructor can be used to get access to the {@link RestConfig} object to get access to the config * file and initialization information or make programmatic modifications to the resource before full initialization. * *

* Non-RestServlet classes can also add the following two methods to get access to the * {@link RestConfig} and {@link RestContext} objects: *

    *
  • public void init(RestConfig); *
  • public void init(RestContext); *
* *

* Subclasses can be used to provide customized resolution of REST resource class instances. * *

* If not specified on a child resource, the resource resolver is inherited from the parent resource context. * *

* The programmatic equivalent to this annotation are the {@link RestConfig#setResourceResolver(Class)}/ * {@link RestConfig#setResourceResolver(RestResourceResolver)} methods. */ Class resourceResolver() default RestResourceResolverSimple.class; /** * Specifies the logger class to use for logging. * *

* The default logger performs basic error logging to the Java logger. * Subclasses can be used to customize logging behavior on the resource. * *

* The programmatic equivalent to this annotation are the * {@link RestConfig#setLogger(Class)}/{@link RestConfig#setLogger(RestLogger)} methods. */ Class logger() default RestLogger.Normal.class; /** * Specifies the REST call handler class. * *

* This class handles the basic lifecycle of an HTTP REST call. * Subclasses can be used to customize how these HTTP calls are handled. * *

* The programmatic equivalent to this annotation are the * {@link RestConfig#setCallHandler(Class)}/{@link RestConfig#setCallHandler(RestCallHandler)} methods. */ Class callHandler() default RestCallHandler.class; /** * Specifies the class used to retrieve title/description/swagger information about a resource. * *

* Subclasses can be used to customize the documentation on a resource. * *

* The programmatic equivalent to this annotation are the * {@link RestConfig#setInfoProvider(Class)}/{@link RestConfig#setInfoProvider(RestInfoProvider)} methods. */ Class infoProvider() default RestInfoProvider.class; /** * Specifies the serializer listener class to use for listening for non-fatal errors. */ Class serializerListener() default SerializerListener.class; /** * Specifies the parser listener class to use for listening for non-fatal errors. */ Class parserListener() default ParserListener.class; /** * Provides swagger-specific metadata on this resource. * *

* Used to populate the auto-generated OPTIONS swagger documentation. * *

Example:
*

* @RestResource( * path="/addressBook", * * // Swagger info. * swagger=@ResourceSwagger( * contact="{name:'John Smith',email:'[email protected]'}", * license="{name:'Apache 2.0',url:'http://www.apache.org/licenses/LICENSE-2.0.html'}", * version="2.0", * termsOfService="You're on your own.", * tags="[{name:'Java',description:'Java utility',externalDocs:{description:'Home page',url:'http://juneau.apache.org'}}]", * externalDocs="{description:'Home page',url:'http://juneau.apache.org'}" * ) * ) *

*/ ResourceSwagger swagger() default @ResourceSwagger; /** * Provides HTML-doc-specific metadata on this method. * *

* Used to customize the output from the HTML Doc serializer. *

* @RestResource( * path="/addressBook", * * // Links on the HTML rendition page. * // "request:/..." URIs are relative to the request URI. * // "servlet:/..." URIs are relative to the servlet URI. * // "$C{...}" variables are pulled from the config file. * htmldoc=@HtmlDoc( * // Widgets for $W variables. * widgets={ * PoweredByJuneau.class, * ContentTypeLinks.class * } * navlinks={ * "up: request:/..", * "options: servlet:/?method=OPTIONS", * "source: $C{Source/gitHub}/org/apache/juneau/examples/rest/addressbook/AddressBookResource.java", * }, * aside={ * "<div style='max-width:400px;min-width:200px'>", * " <p>Proof-of-concept resource that shows off the capabilities of working with POJO resources.</p>", * " <p>Provides examples of: </p>", * " <ul>", * " <li>XML and RDF namespaces", * " <li>Swagger documentation", * " <li>Widgets", * " </ul>", * " <p style='text-weight:bold;text-decoration:underline;'>Available Content Types</p>", * " $W{ContentTypeLinks}", * "</div>" * }, * footer="$W{PoweredByJuneau}" * ) * ) *

*/ HtmlDoc htmldoc() default @HtmlDoc; /** * Overrides the context path value for this resource and any child resources. * *

* This setting is useful if you want to use "context:/child/path" URLs in child resource POJOs but * the context path is not actually specified on the servlet container. * The net effect is that the {@link RestRequest#getContextPath()} and {@link RestRequest#getServletPath()} methods * will return this value instead of the actual context path of the web app. */ String contextPath() default ""; /** * Enable header URL parameters. * *

* When enabled, headers such as "Accept" and "Content-Type" to be passed in as URL query * parameters. * For example: "?Accept=text/json&Content-Type=text/json" * *

    *
  • Boolean value. *
  • Defaults to system property "juneau.allowHeaderParams", or "true" if not specified. *
  • Can contain variables. *
  • Parameter names are case-insensitive. *
  • Useful for debugging REST interface using only a browser. *
*/ String allowHeaderParams() default ""; /** * Enable "method" URL parameter for specific HTTP methods. * *

* When specified, the HTTP method can be overridden by passing in a "method" URL parameter on a regular * GET request. * For example: "?method=OPTIONS" * *

* Example: "HEAD,OPTIONS" * *

    *
  • Format is a comma-delimited list of HTTP method names that can be passed in as a method parameter. *
  • Defaults to system property "juneau.allowMethodParam", or "HEAD,OPTIONS" if not specified. *
  • Can contain variables. *
  • Parameter name is case-insensitive. *
  • Use "*" to represent all methods. *
  • For backwards compatibility, "true" also means "*". *
* *

* Note that per the HTTP specification, special care should * be taken when allowing non-safe (POST, PUT, DELETE) methods to be invoked through GET requests. */ String allowMethodParam() default ""; /** * Enable "body" URL parameter. * *

* When enabled, the HTTP body content on PUT and POST requests can be passed in as text using the "body" * URL parameter. * For example: "?body={name:'John%20Smith',age:45}" * *

    *
  • Boolean value. *
  • Defaults to system property "juneau.allowBodyParam", or "true" if not specified. *
  • Can contain variables. *
  • Parameter name is case-insensitive. *
  • Useful for debugging PUT and POST methods using only a browser. *
*/ String allowBodyParam() default ""; /** * Render stack traces. * *

* Render stack traces in HTTP response bodies when errors occur. * *

    *
  • Boolean value. *
  • Defaults to system property "juneau.renderResponseStackTraces", or "false" if not specified. *
  • Can contain variables. *
  • Useful for debugging, although allowing stack traces to be rendered may cause security concerns. *
*/ String renderResponseStackTraces() default ""; /** * Use stack trace hashes. * *

* When enabled, the number of times an exception has occurred will be determined based on stack trace hashsums, * made available through the {@link RestException#getOccurrence()} method. * *

    *
  • Boolean value. *
  • Defaults to system property "juneau.useStackTraceHashes", or "true" if not specified. *
  • Can contain variables. *
*/ String useStackTraceHashes() default ""; /** * Default character encoding. * *

* The default character encoding for the request and response if not specified on the request. * *

    *
  • String value. *
  • Defaults to system property "juneau.defaultCharset", or "utf-8" if not specified. *
  • Can contain variables. *
  • Can be overridden at the method level using {@link RestMethod#defaultCharset() @RestMethod.defaultCharset()}. *
*/ String defaultCharset() default ""; /** * Expected format of request parameters. * * Possible values: *
    *
  • * "UON" - URL-Encoded Object Notation. *
    This notation allows for request parameters to contain arbitrarily complex POJOs. *
  • * "PLAIN" - Plain text. *
    This treats request parameters as plain text. *
    Only POJOs directly convertible from Strings can be represented in parameters when using this * mode. *
* *

* Note that the parameter value "(foo)" is interpreted as "(foo)" when using plain mode, but * "foo" when using UON mode. * *

* The format can also be specified per-parameter using the {@link FormData#format() @FormData.format()} and * {@link Query#format() @Query.format()} annotations. * *

    *
  • String value. *
  • Defaults to system property "juneau.paramFormat", or "UON" if not specified. *
  • Can contain variables. *
  • Can be overridden at the method level using {@link RestMethod#paramFormat() @RestMethod.paramFormat()}. *
*/ String paramFormat() default ""; }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy