Java Reference
In-Depth Information
2. Make sure your PATH is set up to include both the JDK and Maven, as described in
3. Perform the build and run the example by typing
maven install
.
Example ex09_2: Conneg via URL Patterns
Chapter 9
discussed how some clients, particularly browsers, are unable to use the
Accept
header to request different formats from the same URL. To solve this problem, many JAX-
RS implementations allow you to map a filename suffix (
.html
,
.xml
,
.txt
) to a particular me-
dia type. RESTEasy has this capability. We're going to illustrate this using your browser as a
client along with a slightly modified version of
ex09_1
.
The Server Code
A few minor things have changed on the server side. First, we add
getCustomerHtml()
method to our
CustomerResource
class:
@GET
@Path
(
"{id}"
)
@Produces
(
"text/html"
)
public
public
String
getCustomerHtml
(
@PathParam
(
"id"
)
int
int
id
)
{
return
return
"<h1>Customer As HTML</h1><pre>"
+
getCustomer
(
id
).
toString
() +
"</pre>"
;
}
Since you're going to be interacting with this service through your browser, it might be nice
if the example outputs HTML in addition to text, XML, and JSON.
The only other change to the server side is in the configuration for RESTEasy:
src/main/webapp/WEB-INF/web.xml
<web-app>
<context-param>
<context-param>
<param-name>
<param-name>
resteasy.media.type.mappings
</param-name>
</param-name>
<param-value>
<param-value>
html : text/html,
txt : text/plain,
xml : application/xml
</param-value>
</param-value>
</context-param>
</context-param>
...
</web-app>
</web-app>