blob: 6ce0ddaf23c30fa9dbd58e7e9549b1195e63185f [file] [log] [blame]
<!--
/***************************************************************************************************************************
* 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.
***************************************************************************************************************************/
-->
Configurable Properties
<p>
As shown in previous sections, Juneau serializers and parsers are highly-configurable through properties.
(See {@doc ConfigurableProperties})
</p>
<p>
These properties can be defined for serializers and parsers registered on a REST resource via the following:
</p>
<ul class='javatree'>
<li class='ja'>{@link oajr.annotation.Rest#properties() Rest(properties)}
<li class='jc'>{@link oajr.RestContextBuilder} - Various methods on the context builder.
</ul>
<h5 class='figure'>Example:</h5>
<p class='bpcode w800'>
<jk>import static</jk> org.apache.juneau.BeanContext.*;
<jk>import static</jk> org.apache.juneau.serializer.Serializer.*;
<jk>import static</jk> org.apache.juneau.json.JsonSerializer.*;
<jc>// Servlet with properties applied</jc>
<ja>@Rest</ja>(
properties={
<jc>// Bean properties should be sorted alphabetically.</jc>
<ja>@Property</ja>(name=<jsf>BEAN_sortProperties</jsf>, value=<js>"true"</js>),
<jc>// Nulls should not be serialized</jc>
<ja>@Property</ja>(name=<jsf>SERIALIZER_trimNulls</jsf>, value=<js>"true"</js>),
<jc>// Solidus characters should be escaped in JSON</jc>
<ja>@Property</ja>(name=<jsf>JSON_escapeSolidus</jsf>, value=<js>"true"</js>)
}
)
<jk>public</jk> MyRestServlet <jk>extends</jk> BasicRestServlet {...}
</p>
<p>
The programmatic equivalent to this is:
</p>
<p class='bpcode w800'>
<jc>// Servlet with properties applied</jc>
<ja>@Rest</ja>(...)
<jk>public</jk> MyRestServlet <jk>extends</jk> BasicRestServlet {
<jk>public</jk> MyRestServlet(RestContextBuilder builder) {
builder
.sortProperties(); <jc>// Note: RestContextBuilder extends from BeanContextBuilder</jc>
.set(<jsf>SERIALIZER_trimNulls</jsf>, <jk>true</jk>);
.set(<jsf>JSON_escapeSolidus</jsf>, <jk>true</jk>);
}
}
</p>
<p>
Properties can also be overridden at the Java method level:
</p>
<ul class='javatree'>
<li class='ja'>{@link oajr.annotation.RestMethod#properties() RestMethod(properties)}
<li class='jc'>{@link oajr.RequestProperties}
</ul>
<p class='bpcode w800'>
<jc>// GET method with method-level properties</jc>
<ja>@RestMethod</ja>(
name=<jsf>GET</jsf>, path=<js>"/*"</js>,
properties={
<jc>// Bean properties should be sorted alphabetically.</jc>
<ja>@Property</ja>(name=<jsf>BEAN_sortProperties</jsf>, value=<js>"true"</js>),
<jc>// Nulls should not be serialized</jc>
<ja>@Property</ja>(name=<jsf>SERIALIZER_trimNulls</jsf>, value=<js>"true"</js>),
<jc>// Solidus characters should be escaped in JSON</jc>
<ja>@Property</ja>(name=<jsf>JSON_escapeSolidus</jsf>, value=<js>"true"</js>)
}
<jk>public</jk> Object doGet() {
...
}
</p>
<ul class='seealso'>
<li class='ja'>{@link oajr.annotation.Rest#flags() Rest(flags)} - Shorthand for boolean properties.
<li class='ja'>{@link oajr.annotation.RestMethod#flags() RestMethod(flags)} - Shorthand for boolean properties.
<li class='jc'>{@link oajr.RestContextProperties}
<li class='jc'>{@link oajr.RestMethodProperties}
</ul>