| <!-- |
| /*************************************************************************************************************************** |
| * 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. |
| ***************************************************************************************************************************/ |
| --> |
| |
| Parsers |
| |
| <p> |
| REST resources use the {@link oaj.parser.Parser} API for defining parsers for parsing request |
| body content and converting them into POJOs. |
| </p> |
| <p> |
| The servlet will pick which parser to use by matching the request <l>Content-Type</l> header with the |
| media types defined through the {@link oaj.parser.Parser#getMediaTypes()} method. |
| </p> |
| <p> |
| Parsers can be associated with REST servlets in the following ways: |
| </p> |
| <ul> |
| <li class='ja'> |
| {@link oajr.annotation.RestResource#parsers() RestResource(parsers)} |
| - Annotation on resource Java class. |
| <li class='ja'> |
| {@link oajr.annotation.RestMethod#parsers() RestMethod(parsers)} |
| - Annotation on resource Java method. |
| <li class='jm'> |
| {@link oajr.RestContextBuilder#parsers(Class[])} |
| - Programmatic. |
| </ul> |
| <p> |
| The following are all equivalent ways of defining parsers used by a resource: |
| </p> |
| <p class='bpcode w800'> |
| <jc>// Option #1 - Defined via annotation.</jc> |
| <ja>@RestResource</ja>(parsers={JsonParser.<jk>class</jk>, XmlParser.<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.parsers(JsonParser.<jk>class</jk>, XmlParser.<jk>class</jk>); |
| |
| <jc>// Same, but use pre-instantiated parsers.</jc> |
| builder.parsers(JsonParser.<jsf>DEFAULT</jsf>, XmlParser.<jsf>DEFAULT</jsf>); |
| |
| <jc>// Same, but using property.</jc> |
| builder.set(<jsf>REST_parsers</jsf>, JsonParser.<jk>class</jk>, XmlParser.<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.parsers(JsonParser.<jk>class</jk>, XmlParser.<jk>class</jk>); |
| } |
| |
| <jc>// Override at the method level.</jc> |
| <ja>@RestMethod</ja>(parsers={HtmlParser.<jk>class</jk>}) |
| <jk>public</jk> Object myMethod(<ja>@Body</ja> MyPojo myPojo) { |
| <jc>// Do something with your parsed POJO.</jc> |
| } |
| } |
| </p> |
| <h5 class='section'>See Also:</h5> |
| <ul class='doctree'> |
| <li class='jf'>{@link oajr.RestContext#REST_parsers} |
| </ul> |