| <!-- |
| /*************************************************************************************************************************** |
| * 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. |
| ***************************************************************************************************************************/ |
| --> |
| |
| Serializers |
| |
| <p> |
| REST resources use the {@link oaj.serializer.Serializer} API for defining serializers for |
| serializing response POJOs. |
| </p> |
| <p> |
| The servlet will pick which serializer to use by matching the request <l>Accept</l> header with the |
| media types defined through the {@link oaj.serializer.Serializer#getMediaTypeRanges()} method. |
| </p> |
| <p> |
| Serializers can be associated with REST servlets in the following ways: |
| </p> |
| <ul class='javatree'> |
| <li class='ja'> |
| {@link oajr.annotation.Rest#serializers() Rest(serializers)} |
| - Annotation on resource Java class. |
| <li class='ja'> |
| {@link oajr.annotation.RestMethod#serializers() RestMethod(serializers)} |
| - Annotation on resource Java method. |
| <li class='jf'> |
| {@link oajr.RestContext#REST_serializers} |
| - Programmatic. |
| </ul> |
| <p> |
| The following are all equivalent ways of defining serializers used by a resource: |
| </p> |
| <p class='bpcode w800'> |
| <jc>// Option #1 - Defined via annotation.</jc> |
| <ja>@Rest</ja>(serializers={JsonSerializer.<jk>class</jk>, XmlSerializer.<jk>class</jk>}) |
| <jk>public class</jk> MyResource { |
| |
| <jc>// Option #2 - Defined via builder passed in through resource constructor.</jc> |
| <jk>public</jk> MyResource(RestContextBuilder builder) <jk>throws</jk> Exception { |
| |
| <jc>// Using method on builder.</jc> |
| builder.serializers(JsonSerializer.<jk>class</jk>, XmlSerializer.<jk>class</jk>); |
| |
| <jc>// Same, but use pre-instantiated parsers.</jc> |
| builder.serializers(JsonSerializer.<jsf>DEFAULT</jsf>, XmlSerializer.<jsf>DEFAULT</jsf>); |
| |
| <jc>// Same, but using property.</jc> |
| builder.set(<jsf>REST_serializers</jsf>, JsonSerializer.<jk>class</jk>, XmlSerializer.<jk>class</jk>); |
| } |
| |
| <jc>// Option #3 - Defined via builder passed in through init method.</jc> |
| <ja>@RestHook</ja>(<jsf>INIT</jsf>) |
| <jk>public void</jk> init(RestContextBuilder builder) <jk>throws</jk> Exception { |
| builder.serializers(JsonSerializer.<jk>class</jk>, XmlSerializer.<jk>class</jk>); |
| } |
| |
| <jc>// Override at the method level.</jc> |
| <ja>@RestMethod</ja>(serializers={HtmlSerializer.<jk>class</jk>}) |
| <jk>public</jk> MyPojo myMethod() { |
| <jc>// Return a POJO to be serialized.</jc> |
| <jk>return new</jk> MyPojo(); |
| } |
| } |
| </p> |
| |
| <ul class='seealso'> |
| <li class='jf'>{@link oajr.RestContext#REST_serializers} |
| </ul> |