public abstract class RestService extends Object implements org.pipservices.commons.run.IOpenable, org.pipservices.commons.config.IConfigurable, org.pipservices.commons.refer.IReferenceable, org.pipservices.commons.refer.IUnreferenceable, IRegisterable
### Configuration parameters ###
### References ###
HttpEndpoint reference
### Example ###
class MyRestService extends RestService {
private IMyController _controller;
...
public MyRestService() {
super();
this._dependencyResolver.put(
"controller",
new Descriptor("mygroup","controller","*","*","1.0")
);
}
public void setReferences(IReferences references) {
super.setReferences(references);
this._controller = (IMyController)this._dependencyResolver.getRequired("controller");
}
public void register() {
...
}
}
MyRestService service = new MyRestService();
service.configure(ConfigParams.fromTuples(
"connection.protocol", "http",
"connection.host", "localhost",
"connection.port", 8080
));
service.setReferences(References.fromTuples(
new Descriptor("mygroup","controller","default","default","1.0"), controller
));
service.open("123");
System.out.println("The REST service is running on port 8080");
| Modifier and Type | Field and Description |
|---|---|
protected String |
_baseRoute
The base route.
|
protected org.pipservices.components.count.CompositeCounters |
_counters
The performance counters.
|
protected org.pipservices.commons.refer.DependencyResolver |
_dependencyResolver
The dependency resolver.
|
protected HttpEndpoint |
_endpoint
The HTTP endpoint that exposes this service.
|
protected org.pipservices.components.log.CompositeLogger |
_logger
The logger.
|
protected com.sun.net.httpserver.HttpServer |
_server |
protected String |
_url |
| Modifier | Constructor and Description |
|---|---|
protected |
RestService() |
| Modifier and Type | Method and Description |
|---|---|
void |
close(String correlationId)
Closes component and frees used resources.
|
void |
configure(org.pipservices.commons.config.ConfigParams config)
Configures component by passing configuration parameters.
|
protected <T> T |
getBodyAsJson(Class<T> type,
javax.ws.rs.container.ContainerRequestContext request)
Gets request body from json string.
|
protected String |
getBodyAsString(javax.ws.rs.container.ContainerRequestContext request)
Gets string value of request body.
|
protected String |
getQueryParameter(javax.ws.rs.container.ContainerRequestContext request,
String name) |
protected org.pipservices.components.count.Timing |
instrument(String correlationId,
String name)
Adds instrumentation to log calls and measure call time.
|
boolean |
isOpen()
Checks if the component is opened.
|
void |
open(String correlationId)
Opens the component.
|
protected void |
registerResource(org.glassfish.jersey.server.model.Resource resource)
Registers resource in this objects Rest service.
|
protected void |
registerRoute(String method,
String route,
org.glassfish.jersey.process.Inflector<javax.ws.rs.container.ContainerRequestContext,javax.ws.rs.core.Response> action)
Registers a route in HTTP endpoint.
|
protected javax.ws.rs.core.Response |
sendCreatedResult(Object result)
Creates a callback function that sends newly created object as JSON.
|
protected javax.ws.rs.core.Response |
sendDeleted(Object result)
Creates a callback function that sends deleted object as JSON.
|
protected javax.ws.rs.core.Response |
sendEmptyResult()
Creates a callback function that sends an empty result with 204 status code.
|
protected javax.ws.rs.core.Response |
sendError(Exception ex)
Sends error serialized as ErrorDescription object and appropriate HTTP status
code.
|
protected javax.ws.rs.core.Response |
sendResult(Object result)
Creates a callback function that sends result as JSON object.
|
void |
setReferences(org.pipservices.commons.refer.IReferences references)
Sets references to dependent components.
|
void |
unsetReferences()
Unsets (clears) previously set references to dependent components.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitregisterprotected String _baseRoute
protected HttpEndpoint _endpoint
protected org.pipservices.commons.refer.DependencyResolver _dependencyResolver
protected org.pipservices.components.log.CompositeLogger _logger
protected org.pipservices.components.count.CompositeCounters _counters
protected com.sun.net.httpserver.HttpServer _server
protected String _url
public void configure(org.pipservices.commons.config.ConfigParams config)
throws org.pipservices.commons.errors.ConfigException
configure in interface org.pipservices.commons.config.IConfigurableconfig - configuration parameters to be set.org.pipservices.commons.errors.ConfigException - when configuration is wrong.public void setReferences(org.pipservices.commons.refer.IReferences references)
throws org.pipservices.commons.refer.ReferenceException,
org.pipservices.commons.errors.ConfigException
setReferences in interface org.pipservices.commons.refer.IReferenceablereferences - references to locate the component dependencies.org.pipservices.commons.refer.ReferenceException - when no found references.org.pipservices.commons.errors.ConfigException - when configuration is wrong.public void unsetReferences()
unsetReferences in interface org.pipservices.commons.refer.IUnreferenceableprotected org.pipservices.components.count.Timing instrument(String correlationId, String name)
correlationId - (optional) transaction id to trace execution through
call chain.name - a method name.public boolean isOpen()
isOpen in interface org.pipservices.commons.run.IOpenablepublic void open(String correlationId) throws org.pipservices.commons.errors.ApplicationException
open in interface org.pipservices.commons.run.IOpenablecorrelationId - (optional) transaction id to trace execution through
call chain.org.pipservices.commons.errors.ApplicationException - when error occured.public void close(String correlationId) throws org.pipservices.commons.errors.ApplicationException
close in interface org.pipservices.commons.run.IClosablecorrelationId - (optional) transaction id to trace execution through
call chain.org.pipservices.commons.errors.ApplicationException - when error occured.protected javax.ws.rs.core.Response sendError(Exception ex)
ex - an error object to be sent.protected javax.ws.rs.core.Response sendResult(Object result)
result - a body object to result.protected javax.ws.rs.core.Response sendEmptyResult()
protected javax.ws.rs.core.Response sendCreatedResult(Object result)
result - a body object to created resultprotected javax.ws.rs.core.Response sendDeleted(Object result)
result - a body object to deleted resultprotected String getQueryParameter(javax.ws.rs.container.ContainerRequestContext request, String name)
protected String getBodyAsString(javax.ws.rs.container.ContainerRequestContext request) throws org.pipservices.commons.errors.ApplicationException
request - HTTP requestorg.pipservices.commons.errors.ApplicationException - when error occured.protected <T> T getBodyAsJson(Class<T> type, javax.ws.rs.container.ContainerRequestContext request) throws org.pipservices.commons.errors.ApplicationException
type - the class type of result object.request - HTTP requestorg.pipservices.commons.errors.ApplicationException - when error occured.protected void registerRoute(String method, String route, org.glassfish.jersey.process.Inflector<javax.ws.rs.container.ContainerRequestContext,javax.ws.rs.core.Response> action)
method - HTTP method: "get", "head", "post", "put", "delete"route - a command route. Base route will be added to this routeaction - an action function that is called when operation is invoked.protected void registerResource(org.glassfish.jersey.server.model.Resource resource)
resource - resource for registrationCopyright © 2018. All rights reserved.