001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.http.annotation; 014 015import static java.lang.annotation.ElementType.*; 016import static java.lang.annotation.RetentionPolicy.*; 017 018import java.lang.annotation.*; 019 020import org.apache.juneau.*; 021import org.apache.juneau.annotation.*; 022 023/** 024 * REST response status annotation. 025 * 026 * <p> 027 * Annotation used to denote an HTTP response status code. 028 * 029 * <p> 030 * Can be used in the following locations: 031 * <ul> 032 * <li>Arguments of server-side <ja>@RestOp</ja>-annotated methods. 033 * <li>Methods and return types of server-side and client-side <ja>@Response</ja>-annotated interfaces. 034 * </ul> 035 * 036 * <h5 class='topic'>Arguments of server-side <ja>@RestOp</ja>-annotated methods</h5> 037 * 038 * <p> 039 * On server-side REST, this annotation can be applied to method parameters to identify them as an HTTP response value. 040 * 041 * <h5 class='section'>Example:</h5> 042 * <p class='bjava'> 043 * <ja>@RestPost</ja> 044 * <jk>public void</jk> addPet(<ja>@Content</ja> Pet <jv>pet</jv>, <ja>@StatusCode</ja> Value<Integer> <jv>status</jv>) { 045 * <jsm>addPet</jsm>(<jv>pet</jv>); 046 * <jv>status</jv>.set(200); 047 * } 048 * </p> 049 * 050 * <p> 051 * The parameter type must be {@link Value} with a parameterized type of {@link Integer}. 052 * 053 * <h5 class='topic'>Public methods of <ja>@Response</ja>-annotated types</h5> 054 * 055 * <p> 056 * On {@link Response @Response}-annotated classes, this method can be used to denote an HTTP status code on a response. 057 * 058 * <h5 class='section'>Example:</h5> 059 * <p class='bjava'> 060 * <ja>@RestPost</ja> 061 * <jk>public</jk> Success addPet(Pet <jv>pet</jv>) { 062 * <jsm>addPet</jsm>(<jv>pet</jv>); 063 * <jk>return new</jk> Success(); 064 * } 065 * </p> 066 * 067 * <p class='bjava'> 068 * <ja>@Response</ja> 069 * <jk>public class</jk> Success { 070 * 071 * <ja>@StatusCode</ja> 072 * <jk>public int</jk> getStatus() { 073 * <jk>return</jk> 201; 074 * } 075 * 076 * <ja>@Override</ja> 077 * <jk>public</jk> String toString() { 078 * <jk>return</jk> <js>"Pet was successfully added"</js>; 079 * } 080 * } 081 * </p> 082 * 083 * <p> 084 * The method being annotated must be public and return a numeric value. 085 * 086 * 087 * <h5 class='section'>See Also:</h5><ul> 088 * <li class='link'><a class="doclink" href="../../../../../index.html#jrs.Swagger">Swagger</a> 089 * </ul> 090 * 091 * <h5 class='topic'>Methods and return types of server-side and client-side @Response-annotated interfaces</h5> 092 * 093 * <h5 class='section'>See Also:</h5><ul> 094 * <li class='link'><a class="doclink" href="../../../../../index.html#jrc.Response">@Response</a> 095 * </ul> 096 * 097 * <p> 098 * <h5 class='section'>See Also:</h5><ul> 099 100 * </ul> 101 */ 102@Documented 103@Target({PARAMETER,METHOD,TYPE}) 104@Retention(RUNTIME) 105@Inherited 106@Repeatable(StatusCodeAnnotation.Array.class) 107@ContextApply(StatusCodeAnnotation.Applier.class) 108public @interface StatusCode { 109 110 /** 111 * Dynamically apply this annotation to the specified classes. 112 * 113 * <h5 class='section'>See Also:</h5><ul> 114 * <li class='link'><a class="doclink" href="../../../../../index.html#jm.DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a> 115 * </ul> 116 * 117 * @return The annotation value. 118 */ 119 String[] on() default {}; 120 121 /** 122 * Dynamically apply this annotation to the specified classes. 123 * 124 * <p> 125 * Identical to {@link #on()} except allows you to specify class objects instead of a strings. 126 * 127 * <h5 class='section'>See Also:</h5><ul> 128 * <li class='link'><a class="doclink" href="../../../../../index.html#jm.DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a> 129 * </ul> 130 * 131 * @return The annotation value. 132 */ 133 Class<?>[] onClass() default {}; 134 135 /** 136 * The HTTP response codes. 137 * 138 * The default value is <c>500</c> for exceptions and <c>200</c> for return types. 139 * 140 * @return The annotation value. 141 */ 142 int[] value() default {}; 143}