Documentos de Académico
Documentos de Profesional
Documentos de Cultura
REST
@BORILLO
YO
{ n a m e:' R i c a r d oB o r i l l o ' , c o m p a n y:' U n i v e r s i t a tJ a u m eI ' , m a i l:' b o r i l l o @ u j i . e s ' , s o c i a l:{ t w i t t e r:' @ b o r i l l o ' , b l o g:' x m l u t i l s . c o m ' , l i n k e d i n:' l i n k e d i n . c o m / i n / b o r i l l o ' } }
YO
NDICE
HTTP y REST Jersey JAX-RS Testing: Objetivos y alternativas Mejora de la expresividad Documentacin de los servicios
HTTP Y REST
CARACTERSTICAS DE REST:
CARACTERSTICAS DE REST:
CARACTERSTICAS DE REST:
ORIENTADO A RECURSOS
Lista todos los coches o recupera uno:
G E T/ c a r s G E T/ c a r s / 1 2 3 4 A A W
REST != RPC
JERSEY JAX-RS
QU ES?
Jersey es la implementacin Java de referencia del estndar JAX-RS para la definicin de servicios REST:
h t t p s : / / j e r s e y . d e v . j a v a . n e t /
QU ES?
Mapear peticiones HTTP a cdigo Java @GET / @POST / @PUT / @DELETE
@ P a t h ( " u s e r s " ) p u b l i cc l a s sU s e r s R e s o u r c e { @ G E T p u b l i cL i s t < U s e r >g e t U s e r s ( ){ . . . } }
G E T/ u s e r s
QU ES?
Mapear parmetros de URL a parmetros de entrada a los mtodos @PathParam / @QueryParam
@ G E T @ P a t h ( " / u s e r s / { u s e r I d } " ) p u b l i cU s e rg e t U s e r ( @ P a t h P a r a m ( " u s e r I d " )S t r i n gu s e r I d , @ Q u e r y P a r a m ( " d e b u g " )@ D e f a u l t V a l u e ( " 5 " )S t r i n gd e b u g ) { }
G E T/ u s e r s / 1 4 2 1 ? d e b u g = S
QU ES?
Declaracin del formato de los contenidos recibidos o emitidos @Consumes / @Produces
@ G E T @ P r o d u c e s ( M e d i a T y p e . A P P L I C A T I O N _ X M L ) p u b l i cL i s t < U s e r >g e t U s e r s ( ){ } @ P U T @ C o n s u m e s ( M e d i a T y p e . A P P L I C A T I O N _ J S O N ) p u b l i cv o i du p d a t e U s e r ( U s e ru s e r ){ }
CDIGO DE EJEMPLO:
https://github.com/borillo/template-jersey-spring-jpa
OBJETIVOS BSICOS
TESTING:
APROXIMACIN INICIAL
TESTING:
REST SHELL
Consola sencilla de utilizar y con completado Compatible con HATEOAS (soporta discover) Fcil interaccin con servicios REST Carga y guardado de peticiones y respuestas Configuracin del contexto: Cabeceras, auth, etc Disponible en: GitHub REST-shell
Acceso a recursos:
>g e tr e s o u r c ep a r a m s" {p a r a m 1:' v a l u e '} " >p o s tr e s o u r c ed a t a" {p a r a m 1:' v a l u e '} "
>p o s tf r o md a t a . j s o n
>f o l l o wp e o p l e
h t t p : / / l o c a l h o s t : 8 0 8 0 / p e r s o n : >l i s t r e l h r e f = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = p e o p l e . P e r s o n h t t p : / / l o c a l h o s t : 8 0 8 0 / p e r s o n / 1 p e o p l e . s e a r c h h t t p : / / l o c a l h o s t : 8 0 8 0 / p e r s o n / s e a r c h
HERRAMIENTAS DE AUTOMATIZACIN
TESTING:
SOAPUI
SOAPUI
SOAPUI
SOAPUI
SOAPUI
SOAPUI
SOAPUI
SOAPUI
TESTING:
DEFINICIN DE UN TEST
Cdigo necesario para arrancar el contenedor Java:
p u b l i cc l a s sU s e r s R e s o u r c e T e s te x t e n d sJ e r s e y T e s t{ p r i v a t eW e b R e s o u r c er e s o u r c e ; p u b l i cU s e r s R e s o u r c e T e s t ( ){ s u p e r ( n e wW e b A p p D e s c r i p t o r . B u i l d e r ( " c o m . d e c h a r l a s . s e r v i c e s " ) . c o n t e x t P a r a m ( " w e b A p p R o o t K e y " ," j e r s e y m a v e n . r o o t " ) . s e r v l e t C l a s s ( S e r v l e t C o n t a i n e r . c l a s s ) . b u i l d ( ) ) ; t h i s . r e s o u r c e=r e s o u r c e ( ) ; } @ O v e r r i d e p r o t e c t e dT e s t C o n t a i n e r F a c t o r yg e t T e s t C o n t a i n e r F a c t o r y ( ){ r e t u r nn e wG r i z z l y W e b T e s t C o n t a i n e r F a c t o r y ( ) ; } }
DEFINICIN DE UN TEST
Definicin de los tests:
p u b l i cc l a s sU s e r s R e s o u r c e T e s te x t e n d sJ e r s e y T e s t{ / /M t o d o sd ed e f i n i c i nd el as l i d ea n t e r i o r @ T e s t p u b l i cv o i dd e l e t e U s e r ( )t h r o w sE x c e p t i o n{ C l i e n t R e s p o n s er e s p o n s e=r e s o u r c e . p a t h ( " u s e r s / 1 " ) . a c c e p t ( " a p p l i c a t i o n / j s o n " ) . d e l e t e ( C l i e n t R e s p o n s e . c l a s s ) ; A s s e r t . a s s e r t E q u a l s ( S t a t u s . N O _ C O N T E N T . g e t S t a t u s C o d e ( ) , r e s p o n s e . g e t S t a t u s ( ) ) ; } }
CDIGO DE EJEMPLO:
https://github.com/borillo/template-jersey-spring-jpa
OBJETIVOS
Las aserciones en jUnit no resultan nada semnticas:
A s s e r t . a s s e r t E q u a l s ( S t a t u s . N O _ C O N T E N T . g e t S t a t u s C o d e ( ) , r e s p o n s e . g e t S t a t u s ( ) ) ;
Vamos a ver como mejorarlas y as conseguir: Aserciones ms fciles de leer Menos duplicacin de cdigo en las pruebas Mejora de la semntica Facilidad de comprobacin de los resultados
HAMCREST
REST-ASSURED
DEFINICIN
Testing and validating REST services in Java is harder than in dynamic languages such as Ruby and Groovy. REST Assured brings the simplicity of using these languages into the Java domain.
CARACTERSTICAS PRINCIPALES:
Integracin HTTP total: Cookies, auth, headers, ... Expresivo DSL para realizar las comprobaciones JsonPath y XmlPath para validar informacin Matchers Hamcrest para las validaciones Custom parsers por content type Dispone de un StubServer Logging Mucho ms!! Consulta la doc oficial :)
CDIGO DE EJEMPLO:
https://github.com/borillo/template-rest-assured
SWAGGER
QU ES SWAGGER?
Swagger is a specification and complete framework implementation for describing, producing, consuming, and visualizing RESTful web services. The overarching goal of Swagger is to enable client and documentation systems to update at the same pace as the server. With Swagger, deploying managing, and using powerful APIs has never been easier.
SWAGGER:
DOCUMENTACIN GENERADA
Accedemos al ndice de servicios documentados:
c u r lX G E Th t t p : / / l o c a l h o s t : 8 0 8 0 / a p i d o c s . j s o n { a p i V e r s i o n :" 1 . 0 " , s w a g g e r V e r s i o n :" 1 . 0 " , b a s e P a t h :" h t t p : / / l o c a l h o s t : 8 0 8 0 " , a p i s :[ { p a t h :" / a p i d o c s . { f o r m a t } / p e t " , d e s c r i p t i o n :" O p e r a t i o n sa b o u tp e t s " } ] }
DOCUMENTACIN GENERADA
Y luego a la descripcin de un servicio:
c u r lX G E Th t t p : / / l o c a l h o s t : 8 0 8 0 / a p i d o c s . j s o n / p e t { a p i V e r s i o n :" 1 . 0 " , s w a g g e r V e r s i o n :" 1 . 0 " , b a s e P a t h :" h t t p : / / l o c a l h o s t : 8 0 8 0 " , r e s o u r c e P a t h :" / p e t " , a p i s :[ { p a t h :" / p e t . { f o r m a t } / { p e t I d } " , d e s c r i p t i o n :" O p e r a t i o n sa b o u tp e t s " , o p e r a t i o n s :[{ p a r a m e t e r s :[ { n a m e :" p e t I d " , . . .
CDIGO DE EJEMPLO:
https://github.com/borillo/template-jersey-swagger
SWAGGER:
CONFIGURACIN DE SWAGGER-UI
Copiamos swagger-ui al proyecto y lo inicializamos:
v a rd o c s _ h a n d l e r=e x p r e s s . s t a t i c ( _ _ d i r n a m e+' / s w a g g e r u i / ' ) ; a p p . g e t ( / ^ \ / d o c s ( \ / . * ) ? $ / ,f u n c t i o n ( r e q ,r e s ,n e x t ){ i f( r e q . u r l= = =' / d o c s ' ){ r e s . w r i t e H e a d ( 3 0 2 ,{' L o c a t i o n ':r e q . u r l+' / '} ) ; r e s . e n d ( ) ; r e t u r n ; } r e q . u r l=r e q . u r l . s u b s t r ( ' / d o c s ' . l e n g t h ) ; r e t u r nd o c s _ h a n d l e r ( r e q ,r e s ,n e x t ) ; } ) ;
CDIGO DE EJEMPLO:
https://github.com/borillo/template-nodejs-swagger
PREGUNTAS?