Request Body Conversion Handler¶
Table of contents
This handler performs conversion process of request body and response body.
The format used for conversion is specified in Consumes and Produces annotations which are configured in the method of resource (action) class that processes the request.
This handler performs the following process.
- Converts the request body to a form that can be accepted by the resource (action) class. For details, see Convert request body to form.
- Converts the processing result of the resource (action) class to response body. For details, see Convert the processing result of resource (action) to response body.
The process flow is as follows.
Module list¶
<dependency>
<groupId>com.nablarch.framework</groupId>
<artifactId>nablarch-fw-jaxrs</artifactId>
</dependency>
Constraints¶
- Configure this handler after the Routing Adapter
- This handler converts the request and response based on the annotation information configured in the method of the resource (action) class. Therefore, this handler should be configured after the Routing Adapter, which specifies the dispatch destination.
Configure the converter that performs the conversion process¶
This handler performs the conversion if request and response using the implementation class BodyConverter that is configured in the bodyConverters property. BodyConverter corresponding to the MIME used in the project should be configured in the bodyConverters property.
An example is shown below.
<component class="nablarch.fw.jaxrs.BodyConvertHandler">
<property name="bodyConverters">
<list>
<!-- Request and response converter for application/xml -->
<component class="nablarch.fw.jaxrs.JaxbBodyConverter" />
<!-- Request and response converter for application/x-www-form-urlencoded -->
<component class="nablarch.fw.jaxrs.FormUrlEncodedConverter" />
</list>
</property>
</component>
Tip
If the MIME used cannot be converted by the converter configured in the bodyConverters property,
a status code (415
) indicating that it is an unsupported media type is returned.
Convert request body to form¶
The format used for the request body conversion is determined by Consumes configured in the method that processes the request.
If MIME different from the MIME configured in Consumes is configured in the Content-Type of the request header,
a status code (415
) indicating that it is an unsupported media type is returned.
An implementation example of the resource (action) method is shown below.
In this example, the request body is being converted to Person
by BodyConverter
corresponding to the application/json
indicated by MediaType.APPLICATION_JSON
.
@Consumes(MediaType.APPLICATION_JSON)
@Valid
public HttpResponse saveJson(Person person) {
UniversalDao.insert(person);
return new HttpResponse();
}
Convert the processing result of resource (action) to response body¶
The format used for the response body conversion is determined by Produces configured in the method that processes the request.
An implementation example of the resource (action) method is shown below.
In this example, the request body is being converted to Person
by BodyConverter
corresponding to the application/json
indicated by MediaType.APPLICATION_JSON
.
GET
@Produces(MediaType.APPLICATION_JSON)
public List<Person> findJson() {
return UniversalDao.findAll(Person.class);
}