SlideShare a Scribd company logo
1 of 9
Download to read offline
JEE KURZUS 6. HÉT
SETENY.JANOS@TIGRA.HU
1
Mi a REST?
REpresentational State Transfer

Representational - a mögötte lévő
struktúrát igyekszik megjeleníteni

Leképezi az URL-ek struktúrájára, így
azonosítja az erőforrásokat

HTTP protokollra épülő, állapotmentes és
alkalmas elosztott rendszerek
megvalósítására
2
Tipikus REST API
http://example.com/books/

GET: könyvek listáját adja vissza

POST: új könyvet szúr a listába, az azonosítót a szerver adja

DELETE: törli a teljes listát

PUT: lecseréli a teljes listát

http://example.com/books/xyz

GET: egy konkrét könyvet ad vissza, melynek xyz az azonosítója

POST: az xyz könyvet listának tekinti, és alárendel egy új elemet (pl.
fejezetet)

DELETE: törli az xyz könyvet

PUT: módosítja az xyz könyvet, vagy újat hoz létre xyz azonosítóval
3
RESTful Web Service
JAX-RS egy ilyen megvalósítás

Többféle reprezentáció (JSON, XML, JPEG, …

lásd @Produces és MediaType osztály

Stateless EJB, és CDI bean lehet JAX-RS resource is és kaphat @Path annotációt

Amit lehet végezni az egyes erőforrásokkal public metódusok segítségével:

PUT - létrehozás 

A teljes lista létrehozása/lecserélése

Olvasás

@GET List<Message> getMessages()

Update

@POST Response addMessage(String msg) 

Törlés

@DELETE @Path(“{msgNum}")

void deleteMessage(@PathParam("msgNum") int msgNum)

4
@Path
@Path("users/{userId:d+}")

@GET

public Response getUser(@PathParam("userId") int id){

return Response.status(200).entity(userDAO.find(id));

}
A végső útvonal hierarchikusan épül fel, az alábbi módon:
applicationPath / classPath / methodPath

Lásd: JaxRsApplication / MessageBoardRootResource
és MessageBoardResourceBean / @GET, @POST és
@DELETE -el jelölt metódusok

{ } -el adjuk meg azokat a részeket, amiket a @PathParam-
okban fugunk használni

Regex is használható, lásd: d+
JAX-RS Annotációk
@Path

URI részlet ahol elérhető a resource

@PUT, @GET, @POST, @DELETE, (@HEAD)

create, read, update, delete, (metaadat lekérés)

@PathParam

az URI részét képező paraméterek, pl: /users/123

@QueryParam

a URI query paraméter, pl: /users?id=123 

@Produces, @Consumes

A kimenő/bejövő tartalom típusa (MIME Type, pl: JSON, XML,
stb…). Osztály és metódus szinten is megadható
6
JAX-RS tutorial-ok
https://docs.oracle.com/javaee/7/
tutorial/jaxrs.htm

http://www.mkyong.com/webservices/
jax-rs/restfull-java-client-with-java-net-
url/
7
Arquillian test JAX-RS-hez
@RunWith(Arquillian.class)

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

public class MessageBoardTest {

@ArquillianResource

private URL base;



@Before

public void setupClass() throws MalformedURLException {

Client client = ClientBuilder.newClient();

target = client.target(
URI.create(new URL(base, “app/messages").toExternalForm())
);

}



@Test

public void test2GetList() {

String r = target.request().get(String.class);

assertEquals(
"[{"uniqueId":3},{"uniqueId":2},{"uniqueId":1},{"uniqueId":0}]"
,r
);

}

}
A createDeployment metódus a következő slide-on van
@Deployment(testable = false)

public static WebArchive createDeployment() {

WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")

.addClass(Message.class)

.addClass(MessageListWriter.class)

.addClass(MessageWriter.class)

.addClass(NotFoundException.class)

.addClass(NotFoundExceptionMapper.class)

.addClass(ResponseFilter.class)

.addClass(ValidCharacterInterceptor.class)

.addClass(JaxRsApplication.class)

.addClass(MessageBoardResourceBean.class)

.addClass(MessageBoardRootResource.class)

.addClass(MessageHolderSingletonBean.class);



System.out.println(war.toString(true));



return war;

}
Arquillian test JAX-RS-hez

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Jee kurzus 6. het

  • 1. JEE KURZUS 6. HÉT SETENY.JANOS@TIGRA.HU 1
  • 2. Mi a REST? REpresentational State Transfer Representational - a mögötte lévő struktúrát igyekszik megjeleníteni Leképezi az URL-ek struktúrájára, így azonosítja az erőforrásokat HTTP protokollra épülő, állapotmentes és alkalmas elosztott rendszerek megvalósítására 2
  • 3. Tipikus REST API http://example.com/books/ GET: könyvek listáját adja vissza POST: új könyvet szúr a listába, az azonosítót a szerver adja DELETE: törli a teljes listát PUT: lecseréli a teljes listát http://example.com/books/xyz GET: egy konkrét könyvet ad vissza, melynek xyz az azonosítója POST: az xyz könyvet listának tekinti, és alárendel egy új elemet (pl. fejezetet) DELETE: törli az xyz könyvet PUT: módosítja az xyz könyvet, vagy újat hoz létre xyz azonosítóval 3
  • 4. RESTful Web Service JAX-RS egy ilyen megvalósítás Többféle reprezentáció (JSON, XML, JPEG, …
 lásd @Produces és MediaType osztály Stateless EJB, és CDI bean lehet JAX-RS resource is és kaphat @Path annotációt Amit lehet végezni az egyes erőforrásokkal public metódusok segítségével: PUT - létrehozás 
 A teljes lista létrehozása/lecserélése Olvasás
 @GET List<Message> getMessages() Update
 @POST Response addMessage(String msg) Törlés
 @DELETE @Path(“{msgNum}")
 void deleteMessage(@PathParam("msgNum") int msgNum)
 4
  • 5. @Path @Path("users/{userId:d+}")
 @GET
 public Response getUser(@PathParam("userId") int id){
 return Response.status(200).entity(userDAO.find(id));
 } A végső útvonal hierarchikusan épül fel, az alábbi módon: applicationPath / classPath / methodPath Lásd: JaxRsApplication / MessageBoardRootResource és MessageBoardResourceBean / @GET, @POST és @DELETE -el jelölt metódusok { } -el adjuk meg azokat a részeket, amiket a @PathParam- okban fugunk használni Regex is használható, lásd: d+
  • 6. JAX-RS Annotációk @Path URI részlet ahol elérhető a resource @PUT, @GET, @POST, @DELETE, (@HEAD) create, read, update, delete, (metaadat lekérés) @PathParam az URI részét képező paraméterek, pl: /users/123 @QueryParam a URI query paraméter, pl: /users?id=123 @Produces, @Consumes A kimenő/bejövő tartalom típusa (MIME Type, pl: JSON, XML, stb…). Osztály és metódus szinten is megadható 6
  • 8. Arquillian test JAX-RS-hez @RunWith(Arquillian.class)
 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
 public class MessageBoardTest {
 @ArquillianResource
 private URL base;
 
 @Before
 public void setupClass() throws MalformedURLException {
 Client client = ClientBuilder.newClient();
 target = client.target( URI.create(new URL(base, “app/messages").toExternalForm()) );
 }
 
 @Test
 public void test2GetList() {
 String r = target.request().get(String.class);
 assertEquals( "[{"uniqueId":3},{"uniqueId":2},{"uniqueId":1},{"uniqueId":0}]" ,r );
 }
 } A createDeployment metódus a következő slide-on van
  • 9. @Deployment(testable = false)
 public static WebArchive createDeployment() {
 WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
 .addClass(Message.class)
 .addClass(MessageListWriter.class)
 .addClass(MessageWriter.class)
 .addClass(NotFoundException.class)
 .addClass(NotFoundExceptionMapper.class)
 .addClass(ResponseFilter.class)
 .addClass(ValidCharacterInterceptor.class)
 .addClass(JaxRsApplication.class)
 .addClass(MessageBoardResourceBean.class)
 .addClass(MessageBoardRootResource.class)
 .addClass(MessageHolderSingletonBean.class);
 
 System.out.println(war.toString(true));
 
 return war;
 } Arquillian test JAX-RS-hez