jReflectServer - REST code snippets

Below you'll find some useful REST related code snippets. REST-based services of JReflectServer all respond to GET or POST requests.

 

Getting GET or POST parameters

Use the following code to retrieve request parameters:

String value = request.getParameters().get("test");

 

Access raw http-request / -response

Using raw request and response is a perfect method for writing webservices. Use the WebRequest.getHttpRequest() and WebResponse.getHttpResponse() method to access the low level http components. Please refer to the HttpCore tutorial for information.

If you define a custom response, you need to tell jReflect that you'd like to override the default response using WebResponse.setOverrideHttpReponse(true). Example:

HttpResponse httpResponse = response.getResponse();
StringEntity entity = new StringEntity(
   "This is a result string", ContentType.create("text/plain", "UTF-8"));
httpResponse.setEntity(entity);
httpResponse.setStatusCode(HttpStatus.SC_OK);
// Set override flag
response.setOverrideHttpResponse(true);

 

Note: the code above is the implementation of the convenience method for a text response: WebResponse.text(...):

reponse.text(200, "Hello World!");