Java Reference
In-Depth Information
}
}
For JAX-RS resource methods that return a
String
or an array of characters, you must spe-
cify the
@Produces
annotation so that JAX-RS knows what media to use to set the
Content-
Type
header.
The JAX-RS specification does require that implementations be sensitive to the character set
specified by the
Content-Type
when creating an injected
String
. For example, here's a cli-
ent HTTP POST request that is sending some text data to our service:
POST
/
data
Content
-
Type:
application
/
xml
;
charset
=
UTF
-
8
<
customer
>...</
customer
>
The
Content-Type
of the request is
application/xml
, but it is also stating the character en-
coding is
UTF-8
. JAX-RS implementations will make sure that the created Java
String
is
encoded as
UTF-8
as well.
MultivaluedMap<String, String> and Form Input
HTML forms are a common way to post data to web servers. Form data is encoded as the
the
@FormParam
annotation to inject individual form parameters from the request. You can
also inject a
MultivaluedMap<String, String>
that represents all the form data sent with
the request. For example:
@Path
(
"/"
)
public
public class
class
MyService
MyService
{
@POST
@Consumes
(
"application/x-www-form-urlencoded"
)
@Produces
(
"application/x-www-form-urlencoded"
)
public
public
MultivaluedMap
<
String
,
String
>
post
(
MultivaluedMap
<
String
,
String
>
form
) {
return
return
form
;
}
}
Here, our
post()
method accepts POST requests and receives a
MultivaluedMap<String,
String>
containing all our form data. You may also return a
MultivaluedMap
of form data
as your response. We do this in our example.