| <!-- |
| /*************************************************************************************************************************** |
| * 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. |
| ***************************************************************************************************************************/ |
| --> |
| |
| Converters |
| |
| <p> |
| Converters can be thought of as "post-processors" for POJOs before they get passed to the serializers. |
| </p> |
| <p> |
| Converters are associated with resource classes and methods via the following: |
| </p> |
| <ul class='javatree'> |
| <li class='ja'>{@link oajr.annotation.Rest#converters() Rest(converters)} |
| <li class='ja'>{@link oajr.annotation.RestMethod#converters() RestMethod(converters)} |
| <li class='jm'>{@link oajr.RestContextBuilder#converters(Class...)} |
| </ul> |
| <h5 class='figure'>Example:</h5> |
| <p class='bpcode w800'> |
| <jc>// Associate the Traversable converter to all Java REST methods in this servlet</jc> |
| <ja>@Rest</ja>(converters=Traversable.<jk>class</jk>) |
| <jk>public</jk> MyRestServlet <jk>extends</jk> BasicRestServlet { |
| ... |
| } |
| </p> |
| <p> |
| They can also be defined at the method level: |
| </p> |
| <p class='bpcode w800'> |
| <jc>// GET person request handler.</jc> |
| <jc>// Traversable conversion enabled to allow nodes in returned POJO tree to be addressed.</jc> |
| <jc>// Queryable conversion enabled to allow returned POJO to be searched/viewed/sorted.</jc> |
| <ja>@RestMethod</ja>( |
| name=<jsf>GET</jsf>, path=<js>"/people/{id}/*"</js>, |
| converters={Traversable.<jk>class</jk>,Queryable.<jk>class</jk>} |
| ) |
| <jk>public</jk> Person getPerson(<ja>@Path</ja>(<js>"id"</js>) <jk>int</jk> id) { |
| <jk>return</jk> findPerson(id); |
| } |
| </p> |
| <p> |
| The following converter is used to provide support for addressing child nodes in a POJO tree with URL path |
| remainders. |
| In this code, the 3rd parameter is the object that was returned by the Java method. |
| The converter uses the {@link oaj.utils.PojoRest} wrapper class to address nodes in the |
| tree. |
| </p> |
| <p class='bpcode w800'> |
| <jd>/** |
| * Converter for enablement of PojoRest support on response objects returned by a @RestMethod method. |
| * When enabled, objects in a POJO tree returned by the REST method can be addressed through additional URL path information. |
| */</jd> |
| <jk>public class</jk> Traversable <jk>implements</jk> RestConverter { |
| |
| <ja>@Override</ja> <jc>/* RestConverter */</jc> |
| <jk>public</jk> Object convert(RestRequest req, Object o) <jk>throws</jk> RestException { |
| if (o == <jk>null</jk>) |
| <jk>return null</jk>; |
| |
| String pathRemainder = req.getPathMatch().getRemainder(); |
| |
| <jk>if</jk> (pathRemainder != <jk>null</jk>) { |
| <jk>try</jk> { |
| |
| <jc>// Use the PojoRest class to traverse our POJO model.</jc> |
| PojoRest p = <jk>new</jk> PojoRest(o); |
| o = p.get(pathRemainder); |
| |
| } <jk>catch</jk> (PojoRestException e) { |
| <jk>throw new</jk> RestException(e.getStatus(), e); |
| } <jk>catch</jk> (Exception e) { |
| <jk>throw new</jk> RestException(HttpServletResponse.<jsf>SC_INTERNAL_SERVER_ERROR</jsf>, e); |
| } |
| } |
| |
| <jk>return</jk> o; |
| } |
| } |
| </p> |
| <p> |
| Juneau defines the following converters out-of-the-box: |
| </p> |
| <ul class='javatree'> |
| <li class='jic'> |
| {@link oajr.RestConverter} |
| <ul> |
| <li class='jc'> |
| {@link oajr.converters.Queryable} |
| <br>Provides query parameters that can be used to transform the response (i.e. search/view/sort the |
| POJO response before being serialized). |
| <li class='jc'> |
| {@link oajr.converters.Traversable} |
| <br>Allows nodes in the POJO response tree to be individually accessed through additional path info on |
| the request. |
| <li class='jc'> |
| {@link oajr.converters.Introspectable} |
| <br>Allows method calls to be made on the response POJO, and for the result of that method call to be |
| serialized as the response. |
| </ul> |
| </ul> |
| |
| <ul class='seealso'> |
| <li class='jf'>{@link oajr.RestContext#REST_converters} |
| </ul> |