gx-rest-legacy/README.md

88 lines
2.6 KiB
Markdown
Raw Normal View History

# gxREST
2019-04-06 21:56:02 +02:00
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.
2019-04-06 22:09:37 +02:00
gxRest has the flexibility for different degrees of exploitation:
2019-04-06 21:56:02 +02:00
* it can be entirely adopted both at client and service side with full benefit of its conventions;
2019-04-06 22:09:37 +02:00
* 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.
2019-04-06 21:56:02 +02:00
## Examples of use
Sending a request:
```java
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:
```java
@POST
public Response create(...) {
//Resource successfully created
2019-04-08 04:55:07 +02:00
URI location = ... //URI of the new resource
return GXOutboundSuccessResponse.newCREATEResponse(location)
2019-04-07 21:35:51 +02:00
.withMessage("Resource successfully created.")
.ofType(MediaType.APPLICATION_JSON)
.build();
}
```
Throwing an exception:
```java
@POST
public Response create(...) {
//Oh no, something failed here
2019-04-07 21:35:51 +02:00
GXOutboundErrorResponse.throwException(new MyCustomException("Resource already exists."));
}
```
Parsing a response
```java
2019-04-08 04:55:07 +02:00
// 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);
```
2019-04-06 21:56:02 +02:00
## Modules
* [gxHTTP](https://wiki.gcube-system.org/gcube/GxRest/GxHTTP)
* [gxJRS](https://wiki.gcube-system.org/gcube/GxRest/GxJRS)
## Deployment
2019-05-09 22:16:18 +02:00
Notes about how to deploy this component on an infrastructure or link to wiki doc (if any).
2019-04-06 21:56:02 +02:00
## Documentation
See gxRest on [Wiki](https://wiki.gcube-system.org/gcube/GxRest).
## Built With
* [JAX-RS](https://github.com/eclipse-ee4j/jaxrs-api) - Java™ API for RESTful Web Services
* [Jersey](https://jersey.github.io/) - JAX-RS runtime
* [Maven](https://maven.apache.org/) - Dependency Management
## License
TBP