Entradas

08. Filters & Interceptors. CDI . Conclusions

Imagen
As Java Brains says we should distinguish several components that can be used both from client and server in order intercept the request or response and modify them. 1 Interceptors, Filters and MessageBody Interceptors manipulate entities ( body ) in input and output streams while filters only manipulate the request or response params like  headers , URIs etc There are 2 kinds of interceptors ReaderInterceptor and WriterInterceptor . To create an interceptor, you should implement one of the 2 interfaces. One of the possible use is to GZip the body for slow communications.There are 2 kinds too of filters Container RequestFilters and ContainerResponseFilters  one for the request and the other for the response. the basic usages are Logging, security.. Filters and interceptors can work both on the client and server side. On the client side, we have these filters: ClientRequestFilter and ClientResponseFilter . We also have MessageBodyReader and MessageBodyWriter for the bo

07. Authentication. Filters. Basic Authentication.

Imagen
Java Brains tutorial shows several ways of authentication. 1. Filters First introduces the concept of filters that have methods to implement from the interfaces: ContainerRequestFilter ContainerResponseFilter In the response, the given example, a parameter is added to the header (" MyHeaderParam " with value " Ximo Dante "). Notice the @Provide r annotation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package org . ximodante . jaxrs . client ; import java.io.IOException ; import javax.ws.rs.container.ContainerRequestContext ; import javax.ws.rs.container.ContainerRequestFilter ; import javax.ws.rs.container.ContainerResponseContext ; import javax.ws.rs.container.ContainerResponseFilter ; import javax.ws.rs.ext.Provider ; @Provider public class RestFilter implements ContainerRequestFilter , ContainerResponseFilter { //01. Method from ContainerRequestFilter interface. Notice it

06. Invocations, Generic Types

1. Invocation Invocation is a class that let you create request and execute it by means of its invoke method. Here is an example of creating an invocation and executing it with some parameters. Note method buildGet(): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 package org . ximodante . jaxrs . client ; import javax.ws.rs.client.Client ; import javax.ws.rs.client.ClientBuilder ; import javax.ws.rs.client.Invocation ; import javax.ws.rs.core.MediaType ; import javax.ws.rs.core.Response ; public class MyInvocation { //01. Invocation generation with parameter year public static Invocation prepareRequest ( int year ) { //01.1 Create a client Client client = ClientBuilder . newClient (); return client // 01.2 Define the URL target . target ( "http://localhost:8080/Jaxrs02/firstapi/" ) //01.3 Define path . path ( "messages" ) //01.4 D

05. REST Clients & POST requests

1. Client code Let's suppose we have a REST server ON that offers its services. Here is a simple client class code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 package org . ximodante . jaxrs . client ; import javax.ws.rs.client.Client ; import javax.ws.rs.client.ClientBuilder ; import javax.ws.rs.core.MediaType ; import javax.ws.rs.core.Response ; public class RestAPIClient01 { public static void main ( String [] args ) { // 01. Create client Client client = ClientBuilder . newClient (); //02. Get a Response calling the client Response response = //03. Tell a URL to call (target) client . target ( "http://localhost:8080/Jaxrs02/messages/1" ) // This produces a WebTarget //04. Get the request. We can specify it type or not // if we specify type : //.request(MediaType.APPLICATION_JSON) // or not: . request () // This pro

04. MessageBodyReaders and MessageBodyWriters. Custom Media Types

Imagen
Java Brains is the inspiration for this blog. 1. MessageBody vs Parameters The message body is the content of the request while the parameters are included in the URL. To read params we need param converters and annotations like @Path, @PathParam, @QueryParam Let's see 3 scenarios PUT, GET and POST 1.a PUT request A PUT request is similar /firstapi /users/ ximo  where " ximo " is a Path Parameter And then is a body data (for instance JSON data) used to update the user  {"data": "...." } 1.b GET request A GET request only has params (Path Params and/or Query Params) /fistapi/ getusers /user? start =20&s ize =10    where getuser is a Path Param and start and size are Query Params No body message is used in a GET 1.c POST request A POST request to create a new user /firstapi/users header-info with header params (usually for security information) Body message  with JSON data (in this case)    {&qu

03. Param annotations, and param converters

Imagen
This blog is based on Java Brains . 1. Resources class. PathParam and QueryParam Let's make these changes Insert {pParam} in the Path annotation of the class @Path("{pParam}/test") Create 2 variables, the first annotated with javax.ws.rs.PathParam and the second one with javax.ws.rs.QueryParam . 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package org . ximodante . jaxrs ; import javax.ws.rs.GET ; import javax.ws.rs.Path ; import javax.ws.rs.PathParam ; import javax.ws.rs.Produces ; import javax.ws.rs.QueryParam ; import javax.ws.rs.core.MediaType ; @Path ( "{pParam}/test02" ) //@Singleton public class MyResource02 { //private int count; @PathParam ( "pParam" ) private String pathParamExample ; @QueryParam ( "qParam" ) private String queryParamExample ; @GET @Produces ( MediaType . TEXT_PLAIN ) public String testMethod () { //count=count +1; r