JAX-RS Sample on Content Negotiation
This sample demonstrates how to implement content negotiation in the Application Server. Content negotiation is a mechanism which allows to maintain multiple representations of the same resource, and serve the particular representation relevant to a given request and what the requesting server needs.
Building the sampleÂ
- Open a command line, and navigate to theÂ
<AS_HOME>/samples/Jaxws-Jaxrs/
jaxrs_content_negotiation
 directory. - Run the relevant command to deploy the web app:
Using Maven
Create a WAR file for the sample using the following command:
mvn clean install
Deploy the generated WAR file on WSO2 AS with the related logs on the console:
mvn -Pdeploy
Using Ant
ant
Start the WSO2 Application Server by executing one of the following commands, whereÂ
<PRODUCT_HOME>
 is the directory where you installed the product distribution:OS Command On Windows <PRODUCT_HOME>\bin\wso2server.bat --run
On Linux/Solaris sh <PRODUCT_HOME>/bin/wso2server.sh
Log in to the management console and click List on the Main menu, under Manage/Applications. This opens the Running Applications page. The deployed applications will be listed here.
- You can open the required web application by clicking the Find Services action.
Running the sampleÂ
 A RESTful customer service is provided on URL http://localhost:9763/jaxrs_content_negotiation/services/. Users access this URL to operate on a customer.  The REST server provides the following services:
An HTTP GET request to URL http://localhost:9763/jaxrs_content_negotiation/services/customers/customerservice/customers/123Â with "accept header" set to "application/xml" returns a customer instance in XML format. The XML document returns:
<Customer> <id>123</id> <name>John</name> </Customer>
An HTTP GET request to URL http://localhost:9763/jaxrs_content_negotiation/services/customers/customerservice/customers/123Â with "accept header" set to "application/json" returns a customer instance in JSON format. The JSON document returns:
{"Customer":{"id":"123","name":"John"}}
An HTTP GET request to URL http://localhost:9763/jaxrs_content_negotiation/services/customers/customerservice/customers/123Â without setting "accept header" explicitly, returns a customer instance in XMLÂ format. This is because the accept header will be absent from the request when using HTTP Client. In that case, the CXF will treat the "Accept" content type as "*/*". The XML document returns:
<Customer> <id>123</id> <name>John</name> </Customer>