|
1 | 1 | == MVC API |
2 | 2 |
|
3 | | -[IMPORTANT] |
4 | | -==== |
5 | | -MVC API is not implemented yet. |
6 | | -==== |
| 3 | +MVC API is an alternative way to define routes in Jooby. It uses annotations and byte code generation |
| 4 | +to define and execute routes. |
| 5 | + |
| 6 | +The package `io.jooby.annotations` contains the annotations available for MVC routes. |
| 7 | + |
| 8 | +.MVC API: |
| 9 | +[source,java,role="primary"] |
| 10 | +---- |
| 11 | +import io.jooby.annotations.*; |
| 12 | +
|
| 13 | +@Path("/mvc") // <1> |
| 14 | +public class MyController { |
| 15 | +
|
| 16 | + @GET // <2> |
| 17 | + public String sayHi() { |
| 18 | + return "Hello Mvc!"; |
| 19 | + } |
| 20 | +} |
| 21 | +
|
| 22 | +App.java |
| 23 | +
|
| 24 | +{ |
| 25 | + use(new MyController()); // <3> |
| 26 | +} |
| 27 | +---- |
| 28 | + |
| 29 | +.Kotlin |
| 30 | +[source,kotlin,role="secondary"] |
| 31 | +---- |
| 32 | +
|
| 33 | +import io.jooby.annotations.*; |
| 34 | +
|
| 35 | +@Path("/mvc") // <1> |
| 36 | +class MyController { |
| 37 | +
|
| 38 | + @GET // <2> |
| 39 | + fun sayHi() : String { |
| 40 | + return "Hello Mvc!" |
| 41 | + } |
| 42 | +} |
| 43 | +
|
| 44 | +
|
| 45 | +fun main(args: Array<String>) { |
| 46 | + run(args) { |
| 47 | + use(MyController()) // <3> |
| 48 | + } |
| 49 | +} |
| 50 | +
|
| 51 | +---- |
| 52 | + |
| 53 | +<1> Set a path pattern. The `@Path` annotation is enable at class or method level |
| 54 | +<2> Add a HTTP method |
| 55 | +<3> Register/install the controller in the main application |
| 56 | + |
| 57 | +[id=mvc-parameters] |
| 58 | +=== Parameters |
| 59 | + |
| 60 | +HTTP parameter provision is available via `*Param` annotations. |
| 61 | + |
| 62 | +[id=mvc-header] |
| 63 | +==== Header |
| 64 | + |
| 65 | +Provision of headers is available via javadoc:annotations.HeaderParam[] annotation: |
| 66 | + |
| 67 | +.Headers |
| 68 | +[source, java, role = "primary"] |
| 69 | +---- |
| 70 | +public class MyController { |
| 71 | +
|
| 72 | + @GET |
| 73 | + public Object provisioning(@HeaderParam String token) { // <1> |
| 74 | + ... |
| 75 | + } |
| 76 | +} |
| 77 | +---- |
| 78 | + |
| 79 | +.Kotlin |
| 80 | +[source, kotlin, role = "secondary"] |
| 81 | +---- |
| 82 | +class MyController { |
| 83 | +
|
| 84 | + @GET |
| 85 | + fun provisioning(@HeaderParam token: String) : Any { // <1> |
| 86 | + ... |
| 87 | + } |
| 88 | +} |
| 89 | +---- |
| 90 | + |
| 91 | +<1> Access to HTTP header named `token` |
| 92 | + |
| 93 | +Compared to JAX-RS the parameter name on `@*Param` annotation is completely optional, but required for |
| 94 | +non valid Java names: |
| 95 | + |
| 96 | + |
| 97 | +.Non valid Java name |
| 98 | +[source, java, role = "primary"] |
| 99 | +---- |
| 100 | +public class MyController { |
| 101 | +
|
| 102 | + @GET |
| 103 | + public Object provisioning(@HeaderParam("Last-Modified-Since") long lastModifiedSince) { |
| 104 | + ... |
| 105 | + } |
| 106 | +} |
| 107 | +---- |
| 108 | + |
| 109 | +.Kotlin |
| 110 | +[source, kotlin, role = "secondary"] |
| 111 | +---- |
| 112 | +class MyController { |
| 113 | +
|
| 114 | + @GET |
| 115 | + fun provisioning(@HeaderParam("Last-Modified-Since") lastModifiedSince: Long) : Any { |
| 116 | + ... |
| 117 | + } |
| 118 | +} |
| 119 | +---- |
| 120 | + |
| 121 | +[id=mvc-path] |
| 122 | +==== Path |
| 123 | + |
| 124 | +For path parameters the javadoc:annotations.PathParam[] annotation is required: |
| 125 | + |
| 126 | +.PathParam |
| 127 | +[source, java, role = "primary"] |
| 128 | +---- |
| 129 | +public class MyController { |
| 130 | +
|
| 131 | + @Path("/{id}") |
| 132 | + public Object provisioning(@PathParam String id) { |
| 133 | + ... |
| 134 | + } |
| 135 | +} |
| 136 | +---- |
| 137 | + |
| 138 | +.Kotlin |
| 139 | +[source, kotlin, role = "secondary"] |
| 140 | +---- |
| 141 | +class MyController { |
| 142 | +
|
| 143 | + @Path("/{id}") |
| 144 | + fun provisioning(@PathParam id: String) : Any { |
| 145 | + ... |
| 146 | + } |
| 147 | +} |
| 148 | +---- |
| 149 | + |
| 150 | +[id=mvc-query] |
| 151 | +==== Query |
| 152 | + |
| 153 | +For query parameters the javadoc:annotations.QueryParam[] annotation is required: |
| 154 | + |
| 155 | +.QueryParam |
| 156 | +[source, java, role = "primary"] |
| 157 | +---- |
| 158 | +public class MyController { |
| 159 | +
|
| 160 | + @Path("/") |
| 161 | + public Object provisioning(@QueryParam String q) { |
| 162 | + ... |
| 163 | + } |
| 164 | +} |
| 165 | +---- |
| 166 | + |
| 167 | +.Kotlin |
| 168 | +[source, kotlin, role = "secondary"] |
| 169 | +---- |
| 170 | +class MyController { |
| 171 | +
|
| 172 | + @Path("/") |
| 173 | + fun provisioning(@QueryParam q: String) : Any { |
| 174 | + ... |
| 175 | + } |
| 176 | +} |
| 177 | +---- |
| 178 | + |
| 179 | +[id=mvc-form] |
| 180 | +==== Formdata/Multipart |
| 181 | + |
| 182 | +For formdata/multipart parameters the javadoc:annotations.FormParam[] annotation is required: |
| 183 | + |
| 184 | +.QueryParam |
| 185 | +[source, java, role = "primary"] |
| 186 | +---- |
| 187 | +public class MyController { |
| 188 | +
|
| 189 | + @Path("/") |
| 190 | + @POST |
| 191 | + public Object provisioning(@FormParam String username) { |
| 192 | + ... |
| 193 | + } |
| 194 | +} |
| 195 | +---- |
| 196 | + |
| 197 | +.Kotlin |
| 198 | +[source, kotlin, role = "secondary"] |
| 199 | +---- |
| 200 | +class MyController { |
| 201 | +
|
| 202 | + @Path("/") |
| 203 | + @POST |
| 204 | + fun provisioning(@FormParam username: String) : Any { |
| 205 | + ... |
| 206 | + } |
| 207 | +} |
| 208 | +---- |
0 commit comments