- Notifications
You must be signed in to change notification settings - Fork1
Scalified/rest
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This Library provides extensions forJava API for RESTful Web Services and its implementations
The Library consists of several modules, which can be used separately
- jaxrs -Java API for RESTful Web Services extensions
- jaxrs-resteasy3 -Resteasy version 3.x extensions
- Java SE Development Kit 8 or higher
- JAX-RS API 2.1 implementation
dependencies {implementation"com.scalified:jaxrs:$version"}
dependencies {implementation"com.scalified:jaxrs-resteasy3:$version"}
importcom.scalified.rest.jaxrs.client.JaxRsRestClient;importcom.scalified.rest.jaxrs.client.Request;importcom.scalified.rest.jaxrs.client.RestClient;importjavax.ws.rs.client.Client;importjavax.ws.rs.client.Entity;importjavax.ws.rs.core.GenericType;importjavax.ws.rs.core.MediaType;importjavax.ws.rs.core.Response;importjava.util.Optional;importjava.util.Set;// Creating Rest Client InstanceClientjaxRsClient;// ... client initialization skippedRestClientclient =newJaxRsRestClient(jaxRsClient);// Constructing Request ObjectRequestrequest =Request.builder("http://localhost:8080").path("/application").queryParam("role","admin").accepting(MediaType.APPLICATION_JSON).entity(Entity.json("{\"message\":\"Hello\"}")).header("Authorization","Basic YWRtaW46YWRtaW4=").onSuccess(response ->System.out.println("Success")).onNotFound(response ->System.out.println("Not Found")).onUnsuccessfulResponse(response ->System.out.println("Unsuccessful Response")).onFailure(throwable ->System.out.println("Failure")).build();// Executing HTTP GET RequestsResponseresponse =client.get(request);Optional<String>result =client.get(request,String.class);Optional<Set<String>>results =client.get(request,newGenericType<Set<String>>(){});// Executing HTTP POST RequestsResponseresponse =client.post(request);Optional<String>result =client.post(request,String.class);Optional<Set<String>>results =client.post(request,newGenericType<Set<String>>(){});// Executing HTTP PUT RequestsResponseresponse =client.put(request);Optional<String>result =client.put(request,String.class);Optional<Set<String>>results =client.put(request,newGenericType<Set<String>>(){});// Executing HTTP DELETE RequestsResponseresponse =client.delete(request);Optional<String>result =client.delete(request,String.class);Optional<Set<String>>results =client.delete(request,newGenericType<Set<String>>(){});// Checking Response Is SuccessfullResponseresponse;// ... response initialization skippedbooleansuccessful =RestClient.isSuccessful(response)// Constructing New Response Object From The Given OneResponseresponse;// ... response initialization skippedResponsenewResponse =RestClient.from(response);// HTTP response status code, entity and headers retained
importcom.scalified.rest.jaxrs.extension.ExtendedMediaType;importcom.scalified.rest.jaxrs.extension.ExtendedHttpHeaders;importcom.scalified.rest.jaxrs.extension.ExtendedStatus;importjavax.ws.rs.core.Response;// Using extended HTTP media typesStringapplicationPdfMediaType =ExtendedMediaType.APPLICATION_PDF;// Using extended HTTP headersStringpragmaHeader =ExtendedHttpHeaders.PRAGMA;// Using extended HTTP statusesResponse.StatusTypeunprocessableEntityStatusType =ExtendedStatus.UNPROCESSABLE_ENTITY;
importcom.scalified.rest.jaxrs.cors.CorsFilter;importjavax.ws.rs.core.Feature;importjavax.ws.rs.core.FeatureContext;importjavax.ws.rs.ext.Provider;@ProviderpublicclassCorsFeatureimplementsFeature {@Overridepublicbooleanconfigure(FeatureContextcontext) {CorsFilterfilter =newCorsFilter("*");context.register(filter);returntrue;}}
importcom.scalified.rest.jaxrs.commons.UriUtils;Stringurl =UriUtils.encode("http://localhost:8080/a prämie");asserturl.equals("http%3A%2F%2Flocalhost%3A8080%2Fa%20pr%C3%A4mie");
importcom.scalified.rest.jaxrs.error.ErrorResponse;importjavax.ws.rs.core.Response;importjavax.ws.rs.ext.ExceptionMapper;importjavax.ws.rs.ext.Provider;@ProviderpublicclassDefaultExceptionMapperimplementsExceptionMapper<Exception> {@OverridepublicResponsetoResponse(Exceptione) {ErrorResponsemessage =newErrorResponse(Response.Status.INTERNAL_SERVER_ERROR,e.getMessage());returnResponse.status(Response.Status.INTERNAL_SERVER_ERROR).entity(message).build();}}
importcom.scalified.rest.jaxrs.extension.ExtendedMediaType;importcom.scalified.rest.jaxrs.resteasy.multipart.MultipartUtils;importorg.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;importjavax.ws.rs.Consumes;importjavax.ws.rs.POST;importjavax.ws.rs.Path;importjavax.ws.rs.core.Response;importjava.util.Map;@Path("/")publicclassController {@POST@Path("/files")@Consumes(ExtendedMediaType.MULTIPART_FORM_DATA)publicResponseuploadAttachment(MultipartFormDataInputinput) {// Extracting generic types from MultipartFormDataInputLongsomeValue =MultipartUtils.extractPart(input,"someValue",Long.class);// Extracting String parts from MultipartFormDataInputStringsomeId =MultipartUtils.extractPart(input,"someId");// Extracting files from MultipartFormDataInputMap<String,byte[]>files =MultipartUtils.extractFiles(input);returnResponse.ok() .build(); }}
MIT LicenseCopyright (c) 2018 ScalifiedPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.