001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.http.annotation; 018 019import static java.lang.annotation.ElementType.*; 020import static java.lang.annotation.RetentionPolicy.*; 021 022import java.lang.annotation.*; 023 024import org.apache.juneau.annotation.*; 025import org.apache.juneau.json.*; 026 027/** 028 * REST request body annotation. 029 * 030 * <p> 031 * Identifies a POJO to be used as the body of an HTTP request. 032 * 033 * <p> 034 * Can be used in the following locations: 035 * <ul> 036 * <li>Arguments and argument-types of server-side <ja>@RestOp</ja>-annotated methods. 037 * <li>Arguments and argument-types of client-side <ja>@RemoteOp</ja>-annotated interfaces. 038 * <li>Methods and return types of server-side and client-side <ja>@Request</ja>-annotated interfaces. 039 * </ul> 040 * 041 * <h5 class='topic'>Arguments and argument-types of server-side @RestOp-annotated methods</h5> 042 * 043 * <p> 044 * On server-side REST, this annotation can be applied to method parameters or parameter classes to identify them as the body of an HTTP request. 045 * 046 * <h5 class='section'>Examples:</h5> 047 * <p class='bjava'> 048 * <jc>// Used on parameter</jc> 049 * <ja>@RestPost</ja>(<js>"/pets"</js>) 050 * <jk>public void</jk> addPet(<ja>@Content</ja> Pet <jv>pet</jv>) {...} 051 * </p> 052 * <p class='bjava'> 053 * <jc>// Used on class</jc> 054 * <ja>@RestPost</ja>(<js>"/pets"</js>) 055 * <jk>public void</jk> addPet(Pet <jv>pet</jv>) {...} 056 * 057 * <ja>@Content</ja> 058 * <jk>public class</jk> Pet {...} 059 * </p> 060 * 061 * <p> 062 * This is functionally equivalent to the following code... 063 * <p class='bjava'> 064 * <ja>@RestPost</ja>(<js>"/pets"</js>) 065 * <jk>public void</jk> addPet(RestRequest <jv>req</jv>) { 066 * Pet <jv>pet</jv> = <jv>req</jv>.getContent().as(Pet.<jk>class</jk>); 067 * ... 068 * } 069 * </p> 070 * 071 * <h5 class='topic'>Arguments and argument-types of client-side @RemoteResource-annotated interfaces</h5> 072 * 073 * <h5 class='section'>See Also:</h5><ul> 074 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Content">@Content</a> 075 * </ul> 076 * 077 * <h5 class='topic'>Methods and return types of server-side and client-side @Request-annotated interfaces</h5> 078 * 079 * <h5 class='section'>See Also:</h5><ul> 080 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Request">@Request</a> 081 * </ul> 082 * 083 * <h5 class='section'>Notes:</h5><ul> 084 * <li class='note'> 085 * Annotation parameter values will be aggregated when used on POJO parent and child classes. 086 * <br>Values on child classes override values on parent classes. 087 * <li class='note'> 088 * Annotation parameter values will be aggregated when used on both POJOs and REST methods. 089 * <br>Values on methods override values on POJO classes. 090 * <li class='warn'> 091 * If using this annotation on a Spring bean, note that you are likely to encounter issues when using on parameterized 092 * types such as <code>List<MyBean></code>. This is due to the fact that Spring uses CGLIB to recompile classes 093 * at runtime, and CGLIB was written before generics were introduced into Java and is a virtually-unsupported library. 094 * Therefore, parameterized types will often be stripped from class definitions and replaced with unparameterized types 095 * (e.g. <code>List</code>). Under these circumstances, you are likely to get <code>ClassCastExceptions</code> 096 * when trying to access generalized <code>JsonMaps</code> as beans. The best solution to this issue is to either 097 * specify the parameter as a bean array (e.g. <code>MyBean[]</code>) or declare the method as final so that CGLIB 098 * will not try to recompile it. 099 * </ul> 100 * 101 * <h5 class='section'>See Also:</h5><ul> 102 103 * </ul> 104 */ 105@Documented 106@Target({PARAMETER,METHOD,TYPE}) 107@Retention(RUNTIME) 108@Inherited 109@Repeatable(ContentAnnotation.Array.class) 110@ContextApply(ContentAnnotation.Applier.class) 111public @interface Content { 112 113 /** 114 * Optional description for the exposed API. 115 * 116 * @return The annotation value. 117 * @since 9.2.0 118 */ 119 String[] description() default {}; 120 121 /** 122 * Dynamically apply this annotation to the specified classes. 123 * 124 * <h5 class='section'>See Also:</h5><ul> 125 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a> 126 * </ul> 127 * 128 * @return The annotation value. 129 */ 130 String[] on() default {}; 131 132 /** 133 * Dynamically apply this annotation to the specified classes. 134 * 135 * <p> 136 * Identical to {@link #on()} except allows you to specify class objects instead of a strings. 137 * 138 * <h5 class='section'>See Also:</h5><ul> 139 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a> 140 * </ul> 141 * 142 * @return The annotation value. 143 */ 144 Class<?>[] onClass() default {}; 145 146 /** 147 * <mk>schema</mk> field of the <a class='doclink' href='https://swagger.io/specification/v2#parameterObject'>Swagger Parameter Object</a>. 148 * 149 * <p> 150 * The schema defining the type used for parameter. 151 * 152 * <p> 153 * This is a required attribute per the swagger definition. 154 * However, if not explicitly specified, the value will be auto-generated using {@link JsonSchemaSerializer}. 155 * 156 * <p> 157 * The {@link Schema @Schema} annotation can also be used standalone on the parameter or type. 158 * Values specified on this field override values specified on the type, and values specified on child types override values 159 * specified on parent types. 160 * 161 * <h5 class='section'>Used for:</h5> 162 * <ul class='spaced-list'> 163 * <li> 164 * Server-side schema-based parsing and parsing validation. 165 * <li> 166 * Server-side generated Swagger documentation. 167 * <li> 168 * Client-side schema-based serializing and serializing validation. 169 * </ul> 170 * 171 * @return The annotation value. 172 */ 173 Schema schema() default @Schema; 174}