Create, save and deploy the REST Service with the next content:
// simple groovy script
import javax.ws.rs.Path
import javax.ws.rs.POST
import javax.ws.rs.Produces
import javax.ws.rs.Consumes
import javax.ws.rs.PathParam
import javax.ws.rs.HeaderParam
import javax.ws.rs.QueryParam
@Path("/testMediaTypes")
public class TestService {
@POST
@Consumes("application/xml")
@Produces("text/html")
@Path("InnerPath/{pathParam}")
public String post1(@PathParam("pathParam") String pathParam,
@HeaderParam("Test-Header1") String testHeader,
@QueryParam("Test Query Parameter 1") String testQueryParam,
String body) {
return "PathParam 1:" + pathParam + "; Test Query Parameter 1: " + testQueryParam
+ "; Test-Header1: " + testHeader + "; Body: " + body;
}
@POST
@Consumes("application/xml")
@Produces("application/json")
@Path("InnerPath/{pathParam}")
public String post2(@PathParam("pathParam") String pathParam,
@HeaderParam("Test-Header2") String testHeader,
@QueryParam("Test Query Parameter 2") String testQueryParam,
String body) {
return "PathParam 2:" + pathParam + "; Test Query Parameter 2: " + testQueryParam
+ "; Test-Header2: " + testHeader + "; Body: " + body;
}
}
Select
→
from the top menu, or click
on the toolbar.
Select the Path field as "/testMediaTypes", method OPTIONS, then click .
The OPTIONS request is sent. You will see the response in the Output tab. For example:
<application xmlns="http://research.sun.com/wadl/2006/10">
<resources base="http://192.168.0.8:8080/rest">
<resource path="/testMediaTypes">
<method name="OPTIONS">
<response>
<representation mediaType="application/vnd.sun.wadl+xml"/>
</response>
</method>
<resource path="InnerPath/{pathParam}">
<param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="pathParam" style="template" type="xs:string"/>
<method id="post1" name="POST">
<request>
<param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="Test-Header1" style="header" type="xs:string"/>
<param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="Test Query Parameter 1" style="query" type="xs:string"/>
<representation mediaType="application/xml"/>
</request>
<response>
<representation mediaType="text/html"/>
</response>
</method>
<method id="post2" name="POST">
<request>
<param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="Test-Header2" style="header" type="xs:string"/>
<param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="Test Query Parameter 2" style="query" type="xs:string"/>
<representation mediaType="application/xml"/>
</request>
<response>
<representation mediaType="application/json"/>
</response>
</method>
</resource>
</resource>
</resources>
</application>
Open the Launch REST Service... form again and select the another Path field:
"/testMediaTypes/InnerPath/{pathParam}"Select the Response Media Type = "text/html" item.
Enter "/testMediaTypes/InnerPath/value1" in the
Path
field. In the
Query Parameter
tab, set "Test Query Parameter 1"="value2". In the
Header Parameter
tab, set "Test-Header1"="value3". In the
Body
tab, type "example".
Click .
The request is created and then sent. You will see the response in the Output tab:
[OUTPUT] - -Status - - - - - - - -
200 OK
- -Headers- - - - - - - -
Server : Apache-Coyote/1.1
Content-Type : text/html
Transfer-Encoding : chunked
Date : Mon, 05 Jul 2010 09:06:55 GMT
- -Text - - - - - - - - -
PathParam 1:value1; Test Query Parameter 1: value2; Test-Header1: value3; Body: example