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.commons.lang.*; 026 027/** 028 * REST response status annotation. 029 * 030 * <p> 031 * Annotation used to denote an HTTP response status code. 032 * 033 * <p> 034 * Can be used in the following locations: 035 * <ul> 036 * <li>Arguments of server-side <ja>@RestOp</ja>-annotated methods. 037 * <li>Methods and return types of server-side and client-side <ja>@Response</ja>-annotated interfaces. 038 * </ul> 039 * 040 * <h5 class='topic'>Arguments of server-side <ja>@RestOp</ja>-annotated methods</h5> 041 * 042 * <p> 043 * On server-side REST, this annotation can be applied to method parameters to identify them as an HTTP response value. 044 * 045 * <h5 class='section'>Example:</h5> 046 * <p class='bjava'> 047 * <ja>@RestPost</ja> 048 * <jk>public void</jk> addPet(<ja>@Content</ja> Pet <jv>pet</jv>, <ja>@StatusCode</ja> Value<Integer> <jv>status</jv>) { 049 * <jsm>addPet</jsm>(<jv>pet</jv>); 050 * <jv>status</jv>.set(200); 051 * } 052 * </p> 053 * 054 * <p> 055 * The parameter type must be {@link Value} with a parameterized type of {@link Integer}. 056 * 057 * <h5 class='topic'>Public methods of <ja>@Response</ja>-annotated types</h5> 058 * 059 * <p> 060 * On {@link Response @Response}-annotated classes, this method can be used to denote an HTTP status code on a response. 061 * 062 * <h5 class='section'>Example:</h5> 063 * <p class='bjava'> 064 * <ja>@RestPost</ja> 065 * <jk>public</jk> Success addPet(Pet <jv>pet</jv>) { 066 * <jsm>addPet</jsm>(<jv>pet</jv>); 067 * <jk>return new</jk> Success(); 068 * } 069 * </p> 070 * 071 * <p class='bjava'> 072 * <ja>@Response</ja> 073 * <jk>public class</jk> Success { 074 * 075 * <ja>@StatusCode</ja> 076 * <jk>public int</jk> getStatus() { 077 * <jk>return</jk> 201; 078 * } 079 * 080 * <ja>@Override</ja> 081 * <jk>public</jk> String toString() { 082 * <jk>return</jk> <js>"Pet was successfully added"</js>; 083 * } 084 * } 085 * </p> 086 * 087 * <p> 088 * The method being annotated must be public and return a numeric value. 089 * 090 * 091 * <h5 class='section'>See Also:</h5><ul> 092 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanSwagger2">juneau-bean-swagger-v2</a> 093 * </ul> 094 * 095 * <h5 class='topic'>Methods and return types of server-side and client-side @Response-annotated interfaces</h5> 096 * 097 * <h5 class='section'>See Also:</h5><ul> 098 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Response">@Response</a> 099 * </ul> 100 * 101 * <p> 102 */ 103@Documented 104@Target({ PARAMETER, METHOD, TYPE }) 105@Retention(RUNTIME) 106@Inherited 107@Repeatable(StatusCodeAnnotation.Array.class) 108@ContextApply(StatusCodeAnnotation.Applier.class) 109public @interface StatusCode { 110 111 /** 112 * Optional description for the exposed API. 113 * 114 * @return The annotation value. 115 * @since 9.2.0 116 */ 117 String[] description() default {}; 118 119 /** 120 * Dynamically apply this annotation to the specified classes. 121 * 122 * <h5 class='section'>See Also:</h5><ul> 123 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a> 124 * </ul> 125 * 126 * @return The annotation value. 127 */ 128 String[] on() default {}; 129 130 /** 131 * Dynamically apply this annotation to the specified classes. 132 * 133 * <p> 134 * Identical to {@link #on()} except allows you to specify class objects instead of a strings. 135 * 136 * <h5 class='section'>See Also:</h5><ul> 137 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a> 138 * </ul> 139 * 140 * @return The annotation value. 141 */ 142 Class<?>[] onClass() default {}; 143 144 /** 145 * The HTTP response codes. 146 * 147 * The default value is <c>500</c> for exceptions and <c>200</c> for return types. 148 * 149 * @return The annotation value. 150 */ 151 int[] value() default {}; 152}