gCube eXtensions to REST
Go to file
Luca Frosini eec32da4d7 Merge pull request 'Fixed `getInputStream()` in a `GXInboundResponse` created from a connection, if is not a success response (HTTP code not in `2xx` and `3xx`)' (!1) from mauro.mugnaini/gx-rest:master into master
Reviewed-on: gCubeSystem/gx-rest#1
Reviewed-by: Luca Frosini <luca.frosini@isti.cnr.it>
2024-05-14 15:48:08 +02:00
gxHTTP Removed ide specific files 2023-03-02 12:07:21 +01:00
gxJRS Fixed `getInputStream()` from connection if is not a success response (HTTP code not in `2xx` and `3xx`) 2023-03-22 13:41:47 +01:00
.gitignore Ignored MacOs File 2023-06-21 11:40:00 +02:00
CHANGELOG.md Fixed changelog 2022-07-08 15:15:32 +02:00
LICENSE.md Add CHANGELOG, README and LICENSE files to each module (as requested by the maven-parent). 2020-06-21 16:34:22 -04:00
README.md Switched JSON Management to gcube-jackson 2020-08-06 14:51:24 +02:00
pom.xml Updated maven-parent 2023-10-05 11:34:36 +02:00

README.md

gxREST

The gCube eXtensions to the Rest Protocol (gxRest) is a set of Java libraries designed to provide convenient round-trip interaction between a Restful web application (also known as "service") and its clients.

gxRest has the flexibility for different degrees of exploitation:

  • it can be entirely adopted both at client and service side with full benefit of its conventions;
  • it can be used to send REST requests based only on plain HTTP;
  • it can be used only to return HTTP code/messagess from the service;
  • it can be used only to throw Exceptions from the service to the client.

Examples of use

Sending a request:

GXHTTPRequest request = GXHTTPRequest.newRequest("http://host:port/service/").from("GXRequestTest");
 
//prepare some parameters
String context ="...";
Map<String,String> queryParams = new WeakHashMap<>();
queryParams.put("param name", "param value");
GXInboundResponse response = request.path("context")
	  .queryParams(queryParams).withBody(context).post();

Returning a success response:

 @POST
  public Response create(...) {
 
    //Resource successfully created 
    
    URI location = ... //URI of the new resource
    return GXOutboundSuccessResponse.newCREATEResponse(location)
              .withMessage("Resource successfully created.")
	      .ofType(MediaType.APPLICATION_JSON)
              .build();
  }

Throwing an exception:

@POST
  public Response create(...) {
 
    //Oh no, something failed here
    GXOutboundErrorResponse.throwException(new MyCustomException("Resource already exists."));
    
  }   

Parsing a response

// invoke the remote method and get a response.
GXInboundResponse response = ...;
if (response.hasException()) 
    throw response.getException();
//assuming a created (200) code was expected 
if (response.hasCREATEDCode()) {
		System.out.println("Resource successfully created!");
}
//access content as string
String value = response.getStreamedContentAsString();
//unmasharll content as json
Context returnedContext = response.tryConvertStreamedContentFromJson(Context.class);

Modules

Deployment

Notes about how to deploy this component on an infrastructure or link to wiki doc (if any).

Documentation

See gxRest on Wiki.

Built With

  • JAX-RS - Java™ API for RESTful Web Services
  • Jersey - JAX-RS runtime
  • Maven - Dependency Management

License

TBP