このコンテンツは選択した言語では利用できません。
13.10. String Based Annotations
13.10.1. Convert String Based @*Param Annotations to Objects リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
JAX-RS
@*Param
annotations, including @PathParam and @FormParam, are represented as strings in a raw HTTP request. These types of injected parameters can be converted to objects if these objects have a valueOf(String) static method or a constructor that takes one String parameter.
RESTEasy provides two proprietary
@Provider
interfaces to handle this conversion for classes that don't have either a valueOf(String)
static method, or a string constructor.
Example 13.11. StringConverter
The StringConverter interface is implemented to provide custom string marshalling. It is registered under the resteasy.providers context-param in the web.xml file. It can also be registered manually by calling the ResteasyProviderFactory.addStringConverter() method.
The example below is a simple example of using StringConverter.
Example 13.12. StringParameterUnmarshaller
The
StringParameterUnmarshaller
interface is sensitive to the annotations placed on the parameter or field you are injecting into. It is created per injector. The setAnnotations() method is called by resteasy to initialize the unmarshaller.
This interface can be added by creating and registering a provider that implements the interface. It can also be bound using a meta-annotation called
org.jboss.resteasy.annotations.StringsParameterUnmarshallerBinder
.
The example below formats a
java.util.Date
based @PathParam.
It defines a new annotation called @DateFormat. The annotation is annotated with the meta-annotation StringParameterUnmarshallerBinder with a reference to the DateFormater classes.
The Service.get() method has a @PathParam parameter that is also annotated with @DateFormat. The application of @DateFormat triggers the binding of the DateFormatter. The DateFormatter will now be run to unmarshal the path parameter into the date paramter of the get() method.