uCosminexus Application Server, Web Service Development Guide
You create root resource classes. This subsection describes how to create a root resource class that contains the following resource methods, sub-resource methods, sub-resource locators and the bean properties, fields, and constructors that are to be injected.
Table 12-3 Root resource class to be created
| Item | Request method identifier | Context path following /root | Remarks | |
|---|---|---|---|---|
| Resource method | resourceMethod1 | @GET | -- | -- |
| resourceMethod7 | @POST | -- |
|
|
| Sub-resource method | subResourceMethod2 | @GET | /getQueryParam | -- |
| subResourceMethod3 | @POST | /getUriInfoAndEntity | Includes the Entity parameter. | |
| subResourceMethod4 | @GET | /getHttpHeaders | Includes the parameters annotated using the Context annotation that injects HttpHeaders. | |
| subResourceMethod5 | @GET | /getMatrixParam | Includes the parameters annotated using the MatrixParam annotation that injects Matrix parameters. | |
| subResourceMethod6 | @GET | /getCookieParam | Includes the parameters annotated using the CookieParam annotation that injects Cookies. | |
| subResourceMethod8 | @GET | /getPathParam | Includes the parameters annotated using the PathParam annotation that injects the path parameter. | |
| subResourceMethod ThrowingException | @GET | /{path:[A-Z][a-z]+}/exception | Throws exceptions. The exception mapping provider RuntimeExceptionMapper maps the thrown exceptions to HTTP entities. | |
| pojoJsonMappingMethod | @POST | /pojoJsonMapping | Processes data in the JSON format. | |
| Sub-resource locator | subResourceMethod9 | -- | /subresourceLocator | Delegates operations to the sub- resource class SubResource. |
| Constructor | -- | -- | Includes the parameters annotated using the Context annotation that injects UriInfo. | |
| Field | -- | -- | Annotated using the Context annotation that injects Request. | |
| bean property | -- | -- | Annotated using the QueryParam annotation that injects query parameters. | |
An example of creating a root resource class is as follows:
package com.sample.resources; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.CookieParam; import javax.ws.rs.FormParam; import javax.ws.rs.MatrixParam; import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.DefaultValue;import javax.ws.rs.Encoded; import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
//sample:root resource class
//this class is accessed by URI "/root"
@Path("/root")
public class Resource {
//injecting to the field of
//the context "javax.ws.rs.core.Request object"
private @Context
Request request;
// injection of a query parameter by using the setter() method
@QueryParam("queryParam")
public void setQueryParam(String queryParam) { this.queryParam = queryParam;
}
//The private field that stores the query parameter injected by //the aforementioned setter()method
private String queryParam;
// injection to the constructor parameter of
//the context "(javax.ws.rs.core.UriInfo object)
public Resource(@Context UriInfo uriInfo) {
this.uriInfo = uriInfo;
}
//Private field that stores the UriInfo object injected by
// the aforementioned constructor private UriInfo uriInfo;
//Sample: Resource Method 1
// returns the context value
// injected to the field "javax.ws.rs.core.Request Object"
// this method processes the HTTP GET request
@GET
public String resourceMethod1() {
String returnString = "RequestMethod: " + request.getMethod();
return returnString;
} //Sample: Sub Resource Method 2
//returns the value of the query parameter injected to the setter()method
//this method processes the HTTP GET request @GET
//this method is accessed by "/root/getQueryParam" of URI
@Path("getQueryParam")
public String subResourceMethod2() {
String returnString = "QueryParameter: " + queryParam;
return returnString;
}
//Sample: Sub Resource Method 3
// // returns the context value "javax.ws.rs.core.UriInfo object"
// injected to the constructor parameter,
// and returns the received HTTP Request entity body // this method processes the HTTP POST request
@POST
//this method is accessed by "/root/getUriInfoAndEntity" of URI
@Path("getUriInfoAndEntity")
public String subResourceMethod3(
// an entity parameter (a parameter that is not annotated) is
// mapped from the HTTP request entity body
String entity) {
String returnString = "UriInfo: " + uriInfo.getPath() + "; Entity: " + entity;
return returnString;
}
//Sample:Sub Resource Method 4
// returns the context value //(javax.ws.rs.core.HttpHeadersobject)
// injected to the sub resource method parameter
// this method processes the HTTP GET request
@GET
// this method is accessed by "/root/getHttpHeaders" of URI
@Path("getHttpHeaders")
public String subResourceMethod4(
//injecting to the subresource method parameter of
//javax.ws.rs.core.HttpHeaders object
@Context HttpHeaders httpHeaders) {
String returnString = "HttpHeaders: " +
httpHeaders.getRequestHeader("header").get(0).toString();
return returnString;
}
// sample: Sub Resource Method 5
// returns the value of the matrix parameter injected to
// the sub resource method parameter
// this method processes the HTTP GET request
@GET
//this method is accessed by "/root/getMatrixPara" of URI
@Path("getMatrixParam")
public String subResourceMethod5(
// the default value is assigned to the Matrix parameter
@DefaultValue("defaultValue")
//injecting the matrix parameter to
// the subresource method
@MatrixParam("matrix") String matrixParam) {
String returnString = "MatrixParam: " + matrixParam;
return returnString;
}
// sample: Sub Resource Method 6
// returns the value of Cookie injected to the sub resource method parameter
//this method processes the HTTP GET request
@GET
//this method is accessed by "/root/getCookieParam"of URI
@Path("getCookieParam")
public String subResourceMethod6(
// disables automatic URI decoding
@Encoded
// injecting to the sub-resource method parameter of the Cookie @CookieParam("cookie") String cookieParam ) {
String returnString = "CookieParam: " + cookieParam;
return returnString;
}
// sample: Resource Method 7
returns the value injected to the resource method parameter
// this method uses the content defined in the MIME media type
Consumes("*/*")
// this method uses the content defined in the MIME media type
//"application/xml"
@Produces("application/xml")
// this method processes the HTTP POST request
@POST
public Response resourceMethod7(
// injecting the form parameter to the resource method parameter
@FormParam("form") String formParam ) {
ResponseBuilder rb = Response.status(200)
.entity("<FormParam>" + formParam + "</FormParam>" )
.type("application/xml");
return rb.build();
} // sample: Sub Resource Method 8
// returns the value of the path parameter injected to the sub resource method parameter
// this method processes the HTTP GET request
@GET
//this method is accessed by
//"/root/getPathParam/{path:[A-Z][a-z]+}" of URI
//"getPathParam/{path:[A-Z][a-z]+}" is the URI template containing variables
//"{path:[A-Z][a-z]+}" uses regular expressions
@Path("getPathParam/{path:[A-Z][a-z]+}")
public String subResourceMethod8(
// injecting to the path parameter
// of the sub resource method parameter
@PathParam("path") String pathParam ) {
String returnString = "PathParam: " + pathParam;
return returnString;
}
// sample: Sub Resource Locator 9
// The sub resource class is accessed by "/root/subresourceLocator" URI
@Path("/subresourceLocator")
public SubResource subResourceMethod9() {
// the sub-resource locator returns a resource class instance
// to process the HTTP request
return new SubResource();
}
// sample:Sub Resource Method 10
// throws an exception
// Exception mapping provider maps
//the thrown exception to the HTTP response
// this method processes the HTTP GET request
@Path("/exception")
//this method is accessed by "/root/exception" URI
@GET
public String subResourceMethodThrowingException() {
throw new RuntimeException();
} //Sample: Sub Resource method 11
// processes data in the JSON format
//This method processes the HTTP POST request
@Path("pojoJsonMapping")
//This method is accessed by "root/PojoJsonMapping" URI
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public CustomType pojoJsonMappingMethod(CustomType record) {
//Confirm the contents of the record object received from //the client
if(!record.getName().equals("Old Record Name")){
throw new RuntimeException();
}
List<Integer> oldGrades = new ArrayList<Integer>();
oldGrades.add(1);
oldGrades.add(2);
oldGrades.add(3);
if(!record.getGrades().equals(oldGrades)){
throw new RuntimeException();
} // Create a new CustomType object to return to the client
CustomType newRecord = new CustomType();
newRecord.setName("New Record Name");
List<Integer> newGrades = new ArrayList<Integer>();
newGrades.add(5);
newGrades.add(6);
newGrades.add(7);
newRecord.setGrades(newGrades);
return newRecord;
} |
You save the created root resource class (Resource.java) in the c:\temp\jaxrs\works\tutorial\server\src\com\sample\resources\ directory in the UTF-8 format.
An example of creating a sub-resource class is as follows. This sub-resource class has a resource method that receives the HTTP GET request. Note that creating a sub-resource class is optional.
package com.sample.resources;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
//Sample: Sub Resource class
public class SubResource {
//This method processes the HTTP GET request
@GET
public String getHandlerForSubResource(
//Injecting header parameters to
//the sub resource method parameters
@HeaderParam("header") String headerParam) {
return "Header: " + headerParam;
}
} |
You save the created sub-resource class (SubResource.java) in the c:\temp\jaxrs\works\tutorial\server\src\com\sample\resources\ directory in the UTF-8 format.
An example of creating an exception mapping provider is as follows. The Exception Mapping Provider maps the runtime exception to the HTTP response that contains the value of "RuntimeException occurs" in the HTTP header and has the status code 501. Note that creating an Exception Mapping Provider is optional.
package com.sample.providers;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.ExceptionMapper;
//sample: exception mapping class
//this class maps RuntimeException to HTTP response
@Provider
public class RuntimeExceptionMapper implements
ExceptionMapper<RuntimeException> {
//this method is called when the application throws
//a runtime exception
public Response toResponse(RuntimeException re) {
//sets "HTTP/1.1 501 Not Implemented" as the HTTP response //status
//sets "header: RuntimeException occurs" in the header of //the HTTP response
return Response.status(501)
.header("header", "RuntimeException occurs").build();
}
}
|
You save the created exception mapping provider (RuntimeExceptionMapper.java) in the c:\temp\jaxrs\works\tutorial\server\src\com\sample\providers\ directory in the UTF-8 format.
An example of creating a POJO to be mapped to the data in the JSON format is as follows. Note that creating a POJO for mapping to the JSON fornat is an optional operation.
package com.sample.resources;
import java.util.List;
public class CustomType {
private String name;
private List<Integer> grade;
public CustomType() {
}
public CustomType (String name, List<Integer> grades){
this.name = name;
this.grade = grades;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Integer> getGrades() {
return grade;
}
public void setGrades(List<Integer> grades) {
this.grade = grades;
}
@Override
public String toString() {
return "Record [Name=" + name + ", Grades=" + grade.toString() + "]";
}
} |
Save the POJO created (CustomType.java) for mapping to JSON in the c:\temp\jaxrs\works\tutorial\server\src\com\sample\resources\directory in the UTF-8 format.
All Rights Reserved. Copyright (C) 2013, Hitachi, Ltd.