Java Reference
In-Depth Information
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
reader.close();
return sb.toString();
}
}
The
ProductHtmlFormReader
class implements the
MessageBodyReader<T>
class. Its type
parameter is
List<Product>
because that's what your resource deals with. The reader is
invoked on an incoming POST request containing multi-part form data, as indicated by its
@Consumes
annotation. Once the runtime determines that this is the appropriate handler (using
the
isReadable
method), the
readFrom
method is invoked. That method reads the incoming
request to do something with its data payload. In this example, you use two helper methods
to prepare the form data for creating the product posted by the user to add to the order. The
formToString
method takes the request input stream and wraps it with a buffered reader so
that it's easy to break up into a single string. The resulting string is passed to the
formToMap
method, which parses the string for the key/value pairs that make up the form. Those key/
value pairs represent the user's selections in the form, and match one-to-one the properties of
your
Product
bean. So you use that data to construct a new
Product
, add it to the accumulat-
ing list, and return control to the resource.