| <!-- |
| /*************************************************************************************************************************** |
| * 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. |
| ***************************************************************************************************************************/ |
| --> |
| |
| REST Proxies |
| |
| <p> |
| The <c>juneau-rest-client</c> library can also be used to define interface proxies against 3rd-party REST interfaces. |
| This is an extremely powerful feature that allows you to quickly define easy-to-use interfaces against |
| virtually any REST interface. |
| </p> |
| <p> |
| Remote resources are instantiated using one of the following methods: |
| </p> |
| <ul class='javatree'> |
| <li class='jc'>{@link oajrc.RestClient} |
| <ul> |
| <li class='jm'>{@link oajrc.RestClient#getRemoteResource(Class) getRemoteResource(Class)} |
| <li class='jm'>{@link oajrc.RestClient#getRemoteResource(Class,Object) getRemoteResource(Class,Object)} |
| <li class='jm'>{@link oajrc.RestClient#getRemoteResource(Class,Object,Serializer,Parser) getRemoteResource(Class,Object,Serializer,Parser)} |
| </ul> |
| </ul> |
| <p> |
| Annotations are used on the interface and interface methods to specify how to convert input and output to HTTP headers, query parameters, form |
| post parameters, or request/response bodies. |
| </p> |
| <ul class='javatree'> |
| <li class='jp'>{@link oaj.http.remote} |
| <ul> |
| <li class='ja'>{@link oaj.http.remote.RemoteResource} |
| <li class='ja'>{@link oaj.http.remote.RemoteMethod} |
| </ul> |
| <li class='jp'>{@link oaj.http.annotation} |
| <ul> |
| <li class='ja'>{@link oaj.http.annotation.Body} |
| <li class='ja'>{@link oaj.http.annotation.Header} |
| <li class='ja'>{@link oaj.http.annotation.FormData} |
| <li class='ja'>{@link oaj.http.annotation.Query} |
| <li class='ja'>{@link oaj.http.annotation.Path} |
| <li class='ja'>{@link oaj.http.annotation.Request} |
| <li class='ja'>{@link oaj.http.annotation.Response} |
| </ul> |
| </ul> |
| |
| <h5 class='figure'>Example:</h5> |
| <p class='bpcode w800'> |
| <ja>@RemoteResource</ja>(path=<js>"/petstore"</js>) |
| <jk>public interface</jk> PetStore { |
| |
| <ja>@RemoteMethod</ja>(httpMethod=<jsf>POST</jsf>, path=<js>"/pets"</js>) |
| String addPet( |
| <ja>@Body</ja> CreatePet pet, |
| <ja>@Header</ja>(<js>"E-Tag"</js>) UUID etag, |
| <ja>@Query</ja>(<js>"debug"</js>) <jk>boolean</jk> debug |
| ); |
| } |
| </p> |
| <p class='bpcode w800'> |
| <jc>// Use a RestClient with default Simple JSON support.</jc> |
| <jk>try</jk> (RestClient client = RestClient.<jsm>create</jsm>().simpleJson().build()) { |
| PetStore store = client.getRemoteResource(PetStore.<jk>class</jk>, <js>"http://localhost:10000"</js>); |
| CreatePet pet = <jk>new</jk> CreatePet(<js>"Fluffy"</js>, 9.99); |
| String response = store.createPet(pet, UUID.<jsm>randomUUID</jsm>(), <jk>true</jk>); |
| } |
| </p> |
| <p> |
| The call above translates to the following REST call: |
| </p> |
| <p class='bpcode w800'> |
| POST http://localhost:10000/petstore/pets?debug=true HTTP/1.1 |
| Accept: application/json |
| Content-Type: application/json |
| E-Tag: 475588d4-0b27-4f56-9296-cc683251d314 |
| |
| { |
| name: 'Fluffy', |
| price: 9.99 |
| } |
| </p> |