Download - JSR 339 - Java API for RESTful Web Services
![Page 1: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/1.jpg)
JSR 339 Java API for RESTful Web Services
![Page 2: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/2.jpg)
GujavaSC / Adopt a JSR
Membros do GujavaSC
Participação Adopt a JSR
Grupo de estudos
Artigos
Palestras
![Page 3: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/3.jpg)
O que é REST?
![Page 4: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/4.jpg)
É um estilo arquitetural…
![Page 5: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/5.jpg)
Estilo arquitetural???
É um estilo arquitetural…
![Page 6: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/6.jpg)
Enxaimel
Telhado
Madeiras
Tijolos
![Page 7: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/7.jpg)
Estilo
Arquitetural
REST
HTTP
![Page 8: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/8.jpg)
Características
Recursos são identificados por um ID;
Sem estado;
Vincule as coisas;
Múltiplas representações;
Interface uniforme.
![Page 9: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/9.jpg)
Características
Recursos são identificados por um ID;
Sem estado;
Vincule as coisas;
Múltiplas representações;
Interface uniforme.
![Page 10: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/10.jpg)
Interface Uniforme
Verbos HTTP Ação
POST Cria um novo recurso
PUT Atualiza um recurso
DELETE Remove um recurso
GET Retorna um recurso
![Page 11: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/11.jpg)
JAX-RS 1.x
Final Release em 2008
Objetivos
Baseado em POJO
Centralizado em HTTP
Independência de formato
Independência de container
Inclusão no Java EE 6
![Page 12: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/12.jpg)
Exemplo JAX-RS 1.0
@Path("books")
public class BookResource {
@GET
@Path("/year/{year}")
@Produces(MediaType.APPLICATION_JSON)
public Response listByYearAndName(@PathParam("year") Integer year,
@QueryParam("name") String name) {
List<Book> books = getListByYearAndName(year, name);
ResponseBuilder rb = Response.ok(books);
return rb.build();
}
}
http://localhost:8080/rest-example/resources/books/year/2011?name=Book1
![Page 13: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/13.jpg)
Exemplo JAX-RS 1.0
http://localhost:8080/rest-example/resources/books/year/2011?name=Book1
@Path("books")
public class BookResource {
@GET
@Path("/year/{year}")
@Produces(MediaType.APPLICATION_JSON)
public Response listByYearAndName(@PathParam("year") Integer year,
@QueryParam("name") String name) {
List<Book> books = getListByYearAndName(year, name);
ResponseBuilder rb = Response.ok(books);
return rb.build();
}
}
![Page 14: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/14.jpg)
Exemplo JAX-RS 1.0
http://localhost:8080/rest-example/resources/books/year/2011?name=Book1
@Path("books")
public class BookResource {
@GET
@Path("/year/{year}")
@Produces(MediaType.APPLICATION_JSON)
public Response listByYearAndName(@PathParam("year") Integer year,
@QueryParam("name") String name) {
List<Book> books = getListByYearAndName(year, name);
ResponseBuilder rb = Response.ok(books);
return rb.build();
}
}
![Page 15: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/15.jpg)
@Path("books")
public class BookResource {
@GET
@Path("/year/{year}")
@Produces(MediaType.APPLICATION_JSON)
public Response listByYearAndName(@PathParam("year") Integer year,
@QueryParam("name") String name) {
List<Book> books = getListByYearAndName(year, name);
ResponseBuilder rb = Response.ok(books);
return rb.build();
}
}
Exemplo JAX-RS 1.0
http://localhost:8080/rest-example/resources/books/year/2011?name=Book1
![Page 16: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/16.jpg)
Exemplo JAX-RS 1.0
@Path("books")
public class BookResource {
@GET
@Path("/year/{year}")
@Produces(MediaType.APPLICATION_JSON)
public Response listByYearAndName(@PathParam("year") Integer year,
@QueryParam("name") String name) {
List<Book> books = getListByYearAndName(year, name);
ResponseBuilder rb = Response.ok(books);
return rb.build();
}
}
http://localhost:8080/rest-example/resources/books/year/2011?name=Book1
![Page 17: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/17.jpg)
Exemplo JAX-RS 1.0
@Path("books")
public class BookResource {
@GET
@Path("/year/{year}")
@Produces(MediaType.APPLICATION_JSON)
public Response listByYearAndName(@PathParam("year") Integer year,
@QueryParam("name") String name) {
List<Book> books = getListByYearAndName(year, name);
ResponseBuilder rb = Response.ok(books);
return rb.build();
}
}
http://localhost:8080/rest-example/resources/books/year/2011?name=Book1
![Page 18: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/18.jpg)
Exemplo JAX-RS 1.0
@Path("books")
public class BookResource {
@GET
@Path("/year/{year}")
@Produces(MediaType.APPLICATION_JSON)
public Response listByYearAndName(@PathParam("year") Integer year,
@QueryParam("name") String name) {
List<Book> books = getListByYearAndName(year, name);
ResponseBuilder rb = Response.ok(books);
return rb.build();
}
}
http://localhost:8080/rest-example/resources/books/year/2011?name=Book1
![Page 19: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/19.jpg)
Exemplo JAX-RS 1.0
@Path("books")
public class BookResource {
@GET
@Path("/year/{year}")
@Produces(MediaType.APPLICATION_JSON)
public Response listByYearAndName(@PathParam("year") Integer year,
@QueryParam("name") String name) {
List<Book> books = getListByYearAndName(year, name);
ResponseBuilder rb = Response.ok(books);
return rb.build();
}
}
http://localhost:8080/rest-example/resources/books/year/2011?name=Book1
![Page 20: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/20.jpg)
Exemplo JAX-RS 1.0
@Path("books")
public class BookResource {
@GET
@Path("/year/{year}")
@Produces(MediaType.APPLICATION_JSON)
public Response listByYearAndName(@PathParam("year") Integer year,
@QueryParam("name") String name) {
List<Book> books = getListByYearAndName(year, name);
ResponseBuilder rb = Response.ok(books);
return rb.build();
}
}
http://localhost:8080/rest-example/resources/books/year/2011?name=Book1
![Page 21: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/21.jpg)
Exemplo JAX-RS 1.0
@Path("books")
public class BookResource {
@GET
@Path("/year/{year}")
@Produces(MediaType.APPLICATION_JSON)
public List<Book> listByYearAndName(@PathParam("year") Integer year,
@QueryParam("name") String name) {
List<Book> books = getListByYearAndName(year, name);
// ResponseBuilder rb = Response.ok(books);
// return rb.build();
return books;
}
}
http://localhost:8080/rest-example/resources/books/year/2011?name=Book1
![Page 22: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/22.jpg)
JAX-RS 2.0
Final Release em Maio de 2013
Solicitações (JSR 339)
Client API
Asynchronous processing
Filters / Interceptors
Bean Validation
Hypermedia
MVC
![Page 23: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/23.jpg)
Client API
![Page 24: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/24.jpg)
Client API
Baseada na API do Jersey (versão 1.x);
Consumir serviços criados em qualquer linguagem.
![Page 25: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/25.jpg)
Qual é o recurso?
![Page 26: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/26.jpg)
Book!
![Page 27: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/27.jpg)
Como foi exposto?
![Page 28: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/28.jpg)
@Path("books")
public class BookResource {
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response read(@PathParam("id") Integer id) {
return Response.ok(readObject(id)).build();
}
}
http://localhost:8080/rest-example/resources/books/10
![Page 29: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/29.jpg)
Como consultávamos?
![Page 30: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/30.jpg)
@Test
public void deveConterOLivro_javaee6() throws Exception {
URL url = new URL("http://localhost:8080/rest-example/resources/books/10");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
String result = IOUtils.toString(con.getInputStream(), "UTF-8");
con.disconnect();
Book book = new Gson().fromJson(result, Book.class);
assertThat(book, notNullValue());
}
![Page 31: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/31.jpg)
@Test
public void deveConterOLivro_javaee6() throws Exception {
URL url = new URL("http://localhost:8080/rest-example/resources/books/10");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
String result = IOUtils.toString(con.getInputStream(), "UTF-8");
con.disconnect();
Book book = new Gson().fromJson(result, Book.class);
assertThat(book, notNullValue());
}
![Page 32: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/32.jpg)
@Test
public void deveConterOLivro_javaee6() throws Exception {
URL url = new URL("http://localhost:8080/rest-example/resources/books/10");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
String result = IOUtils.toString(con.getInputStream(), "UTF-8");
con.disconnect();
Book book = new Gson().fromJson(result, Book.class);
assertThat(book, notNullValue());
}
![Page 33: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/33.jpg)
@Test
public void deveConterOLivro_javaee6() throws Exception {
URL url = new URL("http://localhost:8080/rest-example/resources/books/10");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
String result = IOUtils.toString(con.getInputStream(), "UTF-8");
con.disconnect();
Book book = new Gson().fromJson(result, Book.class);
assertThat(book, notNullValue());
}
![Page 34: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/34.jpg)
@Test
public void deveConterOLivro_javaee6() throws Exception {
URL url = new URL("http://localhost:8080/rest-example/resources/books/10");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
String result = IOUtils.toString(con.getInputStream(), "UTF-8");
con.disconnect();
Book book = new Gson().fromJson(result, Book.class);
assertThat(book, notNullValue());
}
![Page 35: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/35.jpg)
@Test
public void deveConterOLivro_javaee6() throws Exception {
URL url = new URL("http://localhost:8080/rest-example/resources/books/10");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
String result = IOUtils.toString(con.getInputStream(), "UTF-8");
con.disconnect();
Book book = new Gson().fromJson(result, Book.class);
assertThat(book, notNullValue());
}
![Page 36: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/36.jpg)
@Test
public void deveConterOLivro_javaee6() throws Exception {
URL url = new URL("http://localhost:8080/rest-example/resources/books/10");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
String result = IOUtils.toString(con.getInputStream(), "UTF-8");
con.disconnect();
Book book = new Gson().fromJson(result, Book.class);
assertThat(book, notNullValue());
}
GSON,
Jackson,
Jettison,
XStream
![Page 37: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/37.jpg)
@Test
public void deveConterOLivro_javaee6() throws Exception {
URL url = new URL("http://localhost:8080/rest-example/resources/books/10");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
String result = IOUtils.toString(con.getInputStream(), "UTF-8");
con.disconnect();
Book book = new Gson().fromJson(result, Book.class);
assertThat(book, notNullValue());
}
![Page 38: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/38.jpg)
E agora com a Client API?
![Page 39: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/39.jpg)
@Test
public void deveConterOLivro_javaee7() {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/rest-example/resources");
WebTarget path = target.path("books/{id}");
WebTarget bookId = path.resolveTemplate("id", "10");
Builder invocation = bookId.request();
Book book = invocation.get(Book.class);
client.close();
assertThat(book, notNullValue());
}
![Page 40: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/40.jpg)
@Test
public void deveConterOLivro_javaee7() {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/rest-example/resources");
WebTarget path = target.path("books/{id}");
WebTarget bookId = path.resolveTemplate("id", "10");
Builder invocation = bookId.request();
Book book = invocation.get(Book.class);
client.close();
assertThat(book, notNullValue());
}
![Page 41: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/41.jpg)
@Test
public void deveConterOLivro_javaee7() {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/rest-example/resources");
WebTarget path = target.path("books/{id}");
WebTarget bookId = path.resolveTemplate("id", "10");
Builder invocation = bookId.request();
Book book = invocation.get(Book.class);
client.close();
assertThat(book, notNullValue());
}
![Page 42: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/42.jpg)
@Test
public void deveConterOLivro_javaee7() {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/rest-example/resources");
WebTarget path = target.path("books/{id}");
WebTarget bookId = path.resolveTemplate("id", "10");
Builder invocation = bookId.request();
Book book = invocation.get(Book.class);
client.close();
assertThat(book, notNullValue());
}
![Page 43: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/43.jpg)
@Test
public void deveConterOLivro_javaee7() {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/rest-example/resources");
WebTarget path = target.path("books/{id}");
WebTarget bookId = path.resolveTemplate("id", "10");
Builder invocation = bookId.request();
Book book = invocation.get(Book.class);
client.close();
assertThat(book, notNullValue());
}
![Page 44: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/44.jpg)
@Test
public void deveConterOLivro_javaee7() {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/rest-example/resources");
WebTarget path = target.path("books/{id}");
WebTarget bookId = path.resolveTemplate("id", "10");
Builder invocation = bookId.request();
Book book = invocation.get(Book.class);
client.close();
assertThat(book, notNullValue());
}
![Page 45: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/45.jpg)
@Test
public void deveConterOLivro_javaee7() {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/rest-example/resources");
WebTarget path = target.path("books/{id}");
WebTarget bookId = path.resolveTemplate("id", "10");
Builder invocation = bookId.request();
Book book = invocation.get(Book.class);
client.close();
assertThat(book, notNullValue());
}
![Page 46: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/46.jpg)
@Test
public void deveConterOLivro_javaee7() {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/rest-example/resources");
WebTarget path = target.path("books/{id}");
WebTarget bookId = path.resolveTemplate("id", "10");
Builder invocation = bookId.request();
Book book = invocation.get(Book.class);
client.close();
assertThat(book, notNullValue());
}
![Page 47: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/47.jpg)
@Test
public void deveConterOLivro_javaee7() {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/rest-example/resources");
WebTarget path = target.path("books/{id}");
WebTarget bookId = path.resolveTemplate("id", "10");
Builder invocation = bookId.request();
Book book = invocation.get(Book.class);
client.close();
assertThat(book, notNullValue());
}
![Page 48: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/48.jpg)
...“um pouco” mais fluente...
![Page 49: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/49.jpg)
@Test
public void deveConterOLivro_javaee7() {
Client client = ClientBuilder.newClient();
Book book = client.target("http://localhost:8080/rest-example/resources")
.path("books/{id}")
.resolveTemplate("id", "10")
.request()
.get(Book.class);
client.close();
assertThat(book, notNullValue());
}
![Page 50: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/50.jpg)
...@Delete...
![Page 51: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/51.jpg)
@Test
public void deletarOLivro_javaee7() {
Client client = ClientBuilder.newClient();
Response res = client.target("http://localhost:8080/rest-example/resources")
.path("books/{id}")
.resolveTemplate("id", "10")
.request()
.delete();
response.close(); client.close();
assertEquals(Status.OK.getStatusCode(), res.getStatus());
}
![Page 52: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/52.jpg)
@Test
public void deletarOLivro_javaee7() {
Client client = ClientBuilder.newClient();
Response res = client.target("http://localhost:8080/rest-example/resources")
.path("books/{id}")
.resolveTemplate("id", "10")
.request()
.delete();
response.close(); client.close();
assertEquals(Status.OK.getStatusCode(), res.getStatus());
}
![Page 53: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/53.jpg)
@Test
public void deletarOLivro_javaee7() {
Client client = ClientBuilder.newClient();
Response res = client.target("http://localhost:8080/rest-example/resources")
.path("books/{id}")
.resolveTemplate("id", "10")
.request()
.delete();
response.close(); client.close();
assertEquals(Status.OK.getStatusCode(), res.getStatus());
}
![Page 54: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/54.jpg)
@Test
public void deletarOLivro_javaee7() {
Client client = ClientBuilder.newClient();
Response res = client.target("http://localhost:8080/rest-example/resources")
.path("books/{id}")
.resolveTemplate("id", "10")
.request()
.delete();
response.close(); client.close();
assertEquals(Status.OK.getStatusCode(), res.getStatus());
}
![Page 55: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/55.jpg)
...@Post...
![Page 56: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/56.jpg)
@Test
public void inserirOLivro_javaee7() {
Client client = ClientBuilder.newClient();
Book book = new Book(1, "Android", "Direto das trincheiras");
Response response = client.target("http://localhost:8080/rest-example/resources")
.path("books")
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(book, MediaType.APPLICATION_JSON));
response.close(); client.close();
assertEquals(Status.OK.getStatusCode(), response.getStatus());
}
![Page 57: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/57.jpg)
@Test
public void inserirOLivro_javaee7() {
Client client = ClientBuilder.newClient();
Book book = new Book(1, "Android", "Direto das trincheiras");
Response response = client.target("http://localhost:8080/rest-example/resources")
.path("books")
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(book, MediaType.APPLICATION_JSON));
response.close(); client.close();
assertEquals(Status.OK.getStatusCode(), response.getStatus());
}
![Page 58: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/58.jpg)
@Test
public void inserirOLivro_javaee7() {
Client client = ClientBuilder.newClient();
Book book = new Book(1, "Android", "Direto das trincheiras");
Response response = client.target("http://localhost:8080/rest-example/resources")
.path("books")
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(book, MediaType.APPLICATION_JSON));
response.close(); client.close();
assertEquals(Status.OK.getStatusCode(), response.getStatus());
}
![Page 59: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/59.jpg)
@Test
public void inserirOLivro_javaee7() {
Client client = ClientBuilder.newClient();
Book book = new Book(1, "Android", "Direto das trincheiras");
Response response = client.target("http://localhost:8080/rest-example/resources")
.path("books")
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(book, MediaType.APPLICATION_JSON));
response.close(); client.close();
assertEquals(Status.OK.getStatusCode(), response.getStatus());
}
![Page 60: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/60.jpg)
Como configurar Filters e Interceptors?
![Page 61: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/61.jpg)
@Test
public void inserirOLivroComFiltroDeLog() {
Client client = ClientBuilder.newClient();
client.register(MyLoggingFilter.class);
Book book = new Book(1, "Android", "Direto das trincheiras");
Response response = client.target("http://localhost:8080/rest-example/resources")
.path("books")
.register(MyEntityInterceptor.class)
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(book, MediaType.APPLICATION_JSON));
response.close(); client.close();
assertEquals(Status.OK.getStatusCode(), response.getStatus());
}
![Page 62: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/62.jpg)
@Test
public void inserirOLivroComFiltroDeLog() {
Client client = ClientBuilder.newClient();
client.register(MyLoggingFilter.class);
Book book = new Book(1, "Android", "Direto das trincheiras");
Response response = client.target("http://localhost:8080/rest-example/resources")
.path("books")
.register(MyEntityInterceptor.class)
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(book, MediaType.APPLICATION_JSON));
response.close(); client.close();
assertEquals(Status.OK.getStatusCode(), response.getStatus());
}
![Page 63: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/63.jpg)
@Test
public void inserirOLivroComFiltroDeLog() {
Client client = ClientBuilder.newClient();
client.register(MyLoggingFilter.class);
Book book = new Book(1, "Android", "Direto das trincheiras");
Response response = client.target("http://localhost:8080/rest-example/resources")
.path("books")
.register(MyEntityInterceptor.class)
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(book, MediaType.APPLICATION_JSON));
response.close(); client.close();
assertEquals(Status.OK.getStatusCode(), response.getStatus());
}
![Page 64: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/64.jpg)
@Test
public void inserirOLivroComFiltroDeLog() {
Client client = ClientBuilder.newClient();
client.register(MyLoggingFilter.class);
Book book = new Book(1, "Android", "Direto das trincheiras");
Response response = client.target("http://localhost:8080/rest-example/resources")
.path("books")
.register(MyEntityInterceptor.class)
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(book, MediaType.APPLICATION_JSON));
response.close(); client.close();
assertEquals(Status.OK.getStatusCode(), response.getStatus());
}
![Page 65: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/65.jpg)
AsynchronousProcessing
![Page 66: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/66.jpg)
Que tal uma chamada assíncrona?
![Page 67: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/67.jpg)
@Path("books")
public class BookResource {
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response read(@PathParam("id") Integer id) throws InterruptedException {
Thread.sleep(5000L);
return Response.ok(readObject(id)).build();
}
}
![Page 68: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/68.jpg)
@Path("books")
public class BookResource {
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response read(@PathParam("id") Integer id) throws InterruptedException {
Thread.sleep(5000L);
return Response.ok(readObject(id)).build();
}
}
Simulando um
processamento pesado.
![Page 69: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/69.jpg)
@Test
public void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient()
.target("http://localhost:8080/rest-example/resources")
.path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
System.out.println("Cancelado: " + asyncResponse.isCancelled());
Thread.sleep(2000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
Thread.sleep(5000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
assertThat(asyncResponse.get(), notNullValue());
}
![Page 70: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/70.jpg)
@Test
public void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient()
.target("http://localhost:8080/rest-example/resources")
.path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
System.out.println("Cancelado: " + asyncResponse.isCancelled());
Thread.sleep(2000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
Thread.sleep(5000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
assertThat(asyncResponse.get(), notNullValue());
}
![Page 71: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/71.jpg)
@Test
public void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient()
.target("http://localhost:8080/rest-example/resources")
.path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
System.out.println("Cancelado: " + asyncResponse.isCancelled());
Thread.sleep(2000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
Thread.sleep(5000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
assertThat(asyncResponse.get(), notNullValue());
}
![Page 72: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/72.jpg)
@Test
public void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient()
.target("http://localhost:8080/rest-example/resources")
.path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
System.out.println("Cancelado: " + asyncResponse.isCancelled());
Thread.sleep(2000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
Thread.sleep(5000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
assertThat(asyncResponse.get(), notNullValue());
}
![Page 73: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/73.jpg)
@Test
public void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient()
.target("http://localhost:8080/rest-example/resources")
.path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
System.out.println("Cancelado: " + asyncResponse.isCancelled()); // false
Thread.sleep(2000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
Thread.sleep(5000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
assertThat(asyncResponse.get(), notNullValue());
}
![Page 74: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/74.jpg)
@Test
public void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient()
.target("http://localhost:8080/rest-example/resources")
.path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
System.out.println("Cancelado: " + asyncResponse.isCancelled()); // false
Thread.sleep(2000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
Thread.sleep(5000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
assertThat(asyncResponse.get(), notNullValue());
}
![Page 75: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/75.jpg)
@Test
public void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient()
.target("http://localhost:8080/rest-example/resources")
.path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
System.out.println("Cancelado: " + asyncResponse.isCancelled()); // false
Thread.sleep(2000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
Thread.sleep(5000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
assertThat(asyncResponse.get(), notNullValue());
}
![Page 76: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/76.jpg)
@Test
public void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient()
.target("http://localhost:8080/rest-example/resources")
.path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
System.out.println("Cancelado: " + asyncResponse.isCancelled()); // false
Thread.sleep(2000L);
System.out.println("Finalizado: " + asyncResponse.isDone()); // false
Thread.sleep(5000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
assertThat(asyncResponse.get(), notNullValue());
}
![Page 77: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/77.jpg)
@Test
public void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient()
.target("http://localhost:8080/rest-example/resources")
.path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
System.out.println("Cancelado: " + asyncResponse.isCancelled()); // false
Thread.sleep(2000L);
System.out.println("Finalizado: " + asyncResponse.isDone()); // false
Thread.sleep(5000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
assertThat(asyncResponse.get(), notNullValue());
}
![Page 78: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/78.jpg)
@Test
public void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient()
.target("http://localhost:8080/rest-example/resources")
.path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
System.out.println("Cancelado: " + asyncResponse.isCancelled()); // false
Thread.sleep(2000L);
System.out.println("Finalizado: " + asyncResponse.isDone()); // false
Thread.sleep(5000L);
System.out.println("Finalizado: " + asyncResponse.isDone());
assertThat(asyncResponse.get(), notNullValue());
}
![Page 79: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/79.jpg)
@Test
public void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient()
.target("http://localhost:8080/rest-example/resources")
.path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
System.out.println("Cancelado: " + asyncResponse.isCancelled()); // false
Thread.sleep(2000L);
System.out.println("Finalizado: " + asyncResponse.isDone()); // false
Thread.sleep(5000L);
System.out.println("Finalizado: " + asyncResponse.isDone()); // true
assertThat(asyncResponse.get(), notNullValue());
}
![Page 80: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/80.jpg)
@Test
public void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient()
.target("http://localhost:8080/rest-example/resources")
.path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
System.out.println("Cancelado: " + asyncResponse.isCancelled()); // false
Thread.sleep(2000L);
System.out.println("Finalizado: " + asyncResponse.isDone()); // false
Thread.sleep(5000L);
System.out.println("Finalizado: " + asyncResponse.isDone()); // true
assertThat(asyncResponse.get(), notNullValue());
}
![Page 81: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/81.jpg)
Timeout no método get()JSR 236 - Concurrency Utilities for JavaTM EE
![Page 82: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/82.jpg)
@Testpublic void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient().target("http://localhost:8080/rest-example/resources").path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
assertThat(asyncResponse.get(20L, TimeUnit.SECONDS), notNullValue());}
![Page 83: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/83.jpg)
@Testpublic void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient().target("http://localhost:8080/rest-example/resources").path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
assertThat(asyncResponse.get(20L, TimeUnit.SECONDS), notNullValue());}
![Page 84: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/84.jpg)
@Testpublic void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient().target("http://localhost:8080/rest-example/resources").path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
assertThat(asyncResponse.get(2L, TimeUnit.SECONDS), notNullValue());}
![Page 85: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/85.jpg)
@Testpublic void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient().target("http://localhost:8080/rest-example/resources").path("books/10");
Future<Book> asyncResponse = target.request().async().get(Book.class);
assertThat(asyncResponse.get(2L, TimeUnit.SECONDS), notNullValue());}
![Page 86: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/86.jpg)
Posso enviar um InvocationCallback?
![Page 87: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/87.jpg)
@Testpublic void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient().target("http://localhost:8080/rest-example/resources").path("books/10");
target.request().async().get(new InvocationCallback<Book>() {
public void completed(Book book) {assertThat(book, notNullValue());
}
public void failed(Throwable throwable) {fail();
}
});
}
![Page 88: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/88.jpg)
@Testpublic void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient().target("http://localhost:8080/rest-example/resources").path("books/10");
target.request().async().get(new InvocationCallback<Book>() {
public void completed(Book book) {assertThat(book, notNullValue());
}
public void failed(Throwable throwable) {fail();
}
});
}
![Page 89: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/89.jpg)
@Testpublic void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient().target("http://localhost:8080/rest-example/resources").path("books/10");
target.request().async().get(new InvocationCallback<Book>() {
public void completed(Book book) {assertThat(book, notNullValue());
}
public void failed(Throwable throwable) {fail();
}
});
}
![Page 90: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/90.jpg)
@Testpublic void buscaOLivroDeFormaAssincrona() {
WebTarget target = ClientBuilder.newClient().target("http://localhost:8080/rest-example/resources").path("books/10");
target.request().async().get(new InvocationCallback<Book>() {
public void completed(Book book) {assertThat(book, notNullValue());
}
public void failed(Throwable throwable) {fail();
}
});
}
![Page 91: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/91.jpg)
Assíncrono no servidor......se você quiser!
![Page 92: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/92.jpg)
Primeiro, sem EJB...
![Page 93: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/93.jpg)
@Path("async/books")
public class AsyncBookResource {
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public void longRunningOperation(@Suspended final AsyncResponse asyncResponse) {
Runnable command = new Runnable() {
public void run() {
Thread.sleep(10000L);
asyncResponse.resume(new Book());
}
};
Executors.newSingleThreadExecutor().execute(command);
}
}
![Page 94: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/94.jpg)
@Path("async/books")
public class AsyncBookResource {
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public void longRunningOperation(@Suspended final AsyncResponse asyncResponse) {
Runnable command = new Runnable() {
public void run() {
Thread.sleep(10000L);
asyncResponse.resume(new Book());
}
};
Executors.newSingleThreadExecutor().execute(command);
}
}
![Page 95: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/95.jpg)
@Path("async/books")
public class AsyncBookResource {
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public void longRunningOperation(@Suspended final AsyncResponse asyncResponse) {
Runnable command = new Runnable() {
public void run() {
Thread.sleep(10000L);
asyncResponse.resume(new Book());
}
};
Executors.newSingleThreadExecutor().execute(command);
}
}
![Page 96: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/96.jpg)
@Path("async/books")
public class AsyncBookResource {
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public void longRunningOperation(@Suspended final AsyncResponse asyncResponse) {
Runnable command = new Runnable() {
public void run() {
Thread.sleep(10000L);
asyncResponse.resume(new Book());
}
};
Executors.newSingleThreadExecutor().execute(command); // JSR 236
}
}
![Page 97: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/97.jpg)
@Path("async/books")
public class AsyncBookResource {
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public void longRunningOperation(@Suspended final AsyncResponse asyncResponse) {
Runnable command = new Runnable() {
public void run() {
Thread.sleep(10000L);
asyncResponse.resume(new Book());
}
};
Executors.newSingleThreadExecutor().execute(command); // JSR 236
}
}
![Page 98: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/98.jpg)
@Path("async/books")
public class AsyncBookResource {
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public void longRunningOperation(@Suspended final AsyncResponse asyncResponse) {
Runnable command = new Runnable() {
public void run() {
Thread.sleep(10000L);
asyncResponse.resume(new Book());
}
};
Executors.newSingleThreadExecutor().execute(command); // JSR 236
}
}
![Page 99: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/99.jpg)
E se for com EJB?
![Page 100: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/100.jpg)
@Stateless
@Path("async/books")
public class AsyncBookResource {
@GET
@Path("/")
@Asynchronous
@Produces(MediaType.APPLICATION_JSON)
public void longRunningOperation(@Suspended final AsyncResponse asyncResponse) {
Thread.sleep(10000L);
asyncResponse.resume(new Book());
}
}
EJB 3.1 (Java EE 6)
![Page 101: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/101.jpg)
@Stateless
@Path("async/books")
public class AsyncBookResource {
@GET
@Path("/")
@Asynchronous
@Produces(MediaType.APPLICATION_JSON)
public void longRunningOperation(@Suspended final AsyncResponse asyncResponse) {
Thread.sleep(10000L);
asyncResponse.resume(new Book());
}
}
![Page 102: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/102.jpg)
@Stateless
@Path("async/books")
public class AsyncBookResource {
@GET
@Path("/")
@Asynchronous
@Produces(MediaType.APPLICATION_JSON)
public void longRunningOperation(@Suspended final AsyncResponse asyncResponse) {
Thread.sleep(10000L);
asyncResponse.resume(new Book());
}
}
![Page 103: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/103.jpg)
@Stateless
@Path("async/books")
public class AsyncBookResource {
@GET
@Path("/")
@Asynchronous
@Produces(MediaType.APPLICATION_JSON)
public void longRunningOperation(@Suspended final AsyncResponse asyncResponse) {
Thread.sleep(10000L);
asyncResponse.resume(new Book());
}
}
![Page 104: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/104.jpg)
Filters and Interceptors
![Page 105: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/105.jpg)
O que são os filters?
![Page 106: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/106.jpg)
ClientRequestFilter
ClientResponseFilter
![Page 107: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/107.jpg)
@Provider
public class LoggingFilter implements ClientRequestFilter, ClientResponseFilter {
public void filter(ClientRequestContext requestContext) throws IOException {
log(requestContext);
}
public void filter(ClientRequestContext requestContext, ClientResponseContext
responseContext) throws IOException {
log(responseContext);
}
}
![Page 108: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/108.jpg)
@Provider
public class LoggingFilter implements ClientRequestFilter, ClientResponseFilter {
public void filter(ClientRequestContext requestContext) throws IOException {
log(requestContext);
}
public void filter(ClientRequestContext requestContext, ClientResponseContext
responseContext) throws IOException {
log(responseContext);
}
}
![Page 109: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/109.jpg)
@Provider
public class LoggingFilter implements ClientRequestFilter, ClientResponseFilter {
public void filter(ClientRequestContext requestContext) throws IOException {
log(requestContext);
}
public void filter(ClientRequestContext requestContext, ClientResponseContext
responseContext) throws IOException {
log(responseContext);
}
}
![Page 110: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/110.jpg)
@Provider
public class LoggingFilter implements ClientRequestFilter, ClientResponseFilter {
public void filter(ClientRequestContext requestContext) throws IOException {
log(requestContext);
}
public void filter(ClientRequestContext requestContext, ClientResponseContext
responseContext) throws IOException {
log(responseContext);
}
}
![Page 111: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/111.jpg)
@Provider
public class LoggingFilter implements ClientRequestFilter, ClientResponseFilter {
public void filter(ClientRequestContext requestContext) throws IOException {
log(requestContext);
}
public void filter(ClientRequestContext requestContext, ClientResponseContext
responseContext) throws IOException {
log(responseContext);
}
}
![Page 112: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/112.jpg)
ContainerRequestFilter
ContainerResponserFilter
![Page 113: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/113.jpg)
@Provider
class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter {
public void filter(ContainerRequestContext requestContext) throws IOException {
log(requestContext);
}
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
log(responseContext);
}
}
![Page 114: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/114.jpg)
@Provider
class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter {
public void filter(ContainerRequestContext requestContext) throws IOException {
log(requestContext);
}
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
log(responseContext);
}
}
![Page 115: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/115.jpg)
@Provider
class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter {
public void filter(ContainerRequestContext requestContext) throws IOException {
log(requestContext);
}
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
log(responseContext);
}
}
![Page 116: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/116.jpg)
@Provider
class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter {
public void filter(ContainerRequestContext requestContext) throws IOException {
log(requestContext);
}
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
log(responseContext);
}
}
![Page 117: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/117.jpg)
@Provider
class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter {
public void filter(ContainerRequestContext requestContext) throws IOException {
log(requestContext);
}
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
log(responseContext);
}
}
![Page 118: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/118.jpg)
O que são os interceptors?
![Page 119: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/119.jpg)
ReaderInterceptor
WriterInterceptor
![Page 120: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/120.jpg)
Descompactando a requisição
![Page 121: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/121.jpg)
@Provider
public class GzipReaderInterceptor implements ReaderInterceptor {
Object aroundReadFrom(ReaderInterceptorContext ctx) ... {
if (isGzipped(ctx)) {
InputStream old = ctx.getInputStream();
ctx.setInputStream(new GZIPInputStream(old));
try {
return ctx.proceed();
} finally {
ctx.setInputStream(old);
}
} else {
return ctx.proceed();
}
}
}
![Page 122: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/122.jpg)
@Provider
public class GzipReaderInterceptor implements ReaderInterceptor {
Object aroundReadFrom(ReaderInterceptorContext ctx) ... {
if (isGzipped(ctx)) {
InputStream old = ctx.getInputStream();
ctx.setInputStream(new GZIPInputStream(old));
try {
return ctx.proceed();
} finally {
ctx.setInputStream(old);
}
} else {
return ctx.proceed();
}
}
}
![Page 123: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/123.jpg)
@Provider
public class GzipReaderInterceptor implements ReaderInterceptor {
Object aroundReadFrom(ReaderInterceptorContext ctx) ... {
if (isGzipped(ctx)) {
InputStream old = ctx.getInputStream();
ctx.setInputStream(new GZIPInputStream(old));
try {
return ctx.proceed();
} finally {
ctx.setInputStream(old);
}
} else {
return ctx.proceed();
}
}
}
![Page 124: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/124.jpg)
@Provider
public class GzipReaderInterceptor implements ReaderInterceptor {
Object aroundReadFrom(ReaderInterceptorContext ctx) ... {
if (isGzipped(ctx)) {
InputStream old = ctx.getInputStream();
ctx.setInputStream(new GZIPInputStream(old));
try {
return ctx.proceed();
} finally {
ctx.setInputStream(old);
}
} else {
return ctx.proceed();
}
}
}
![Page 125: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/125.jpg)
Compactando a resposta
![Page 126: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/126.jpg)
@Provider
public class GzipWriteInterceptor implements WriteInterceptor {
void aroundWriteTo(WriterInterceptorContext ctx) ... {
OutputStream old = ctx.getOutputStream();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(old);
ctx.setOutputStream(gzipOutputStream);
updateHeaders(ctx);
try {
ctx.proceed();
} finally {
gzipOutputStream.finish();
ctx.setOutputStream(old);
}
}
}
![Page 127: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/127.jpg)
@Provider
public class GzipWriteInterceptor implements WriteInterceptor {
void aroundWriteTo(WriterInterceptorContext ctx) ... {
OutputStream old = ctx.getOutputStream();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(old);
ctx.setOutputStream(gzipOutputStream);
updateHeaders(ctx);
try {
ctx.proceed();
} finally {
gzipOutputStream.finish();
ctx.setOutputStream(old);
}
}
}
![Page 128: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/128.jpg)
@Provider
public class GzipWriteInterceptor implements WriteInterceptor {
void aroundWriteTo(WriterInterceptorContext ctx) ... {
OutputStream old = ctx.getOutputStream();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(old);
ctx.setOutputStream(gzipOutputStream);
updateHeaders(ctx);
try {
ctx.proceed();
} finally {
gzipOutputStream.finish();
ctx.setOutputStream(old);
}
}
}
![Page 129: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/129.jpg)
@Provider
public class GzipWriteInterceptor implements WriteInterceptor {
void aroundWriteTo(WriterInterceptorContext ctx) ... {
OutputStream old = ctx.getOutputStream();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(old);
ctx.setOutputStream(gzipOutputStream);
updateHeaders(ctx);
try {
ctx.proceed();
} finally {
gzipOutputStream.finish();
ctx.setOutputStream(old);
}
}
}
![Page 130: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/130.jpg)
Mas, qual é a diferençamesmo?
![Page 131: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/131.jpg)
Filters: Acesso ao contexto do request/response.
![Page 132: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/132.jpg)
Exemplo de Filters
Logar origem do request.
Verificar qual o método http utilizado.
Qual a URI.
![Page 133: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/133.jpg)
Interceptors: Acesso ao contexto damensagem.
![Page 134: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/134.jpg)
Exemplo de Interceptors
Compactar o conteúdo da mensagem a ser enviada.
Descompactar o conteúdo da mensagem a ser lida.
![Page 135: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/135.jpg)
Posso logar minhas requisiçõesantes de chegar ao método?
![Page 136: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/136.jpg)
@PreMatching
![Page 137: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/137.jpg)
@Provider@PreMatchingpublic class HttpPreMatchingFilter implements ContainerRequestFilter {
public void filter(ContainerRequestContext requestContext) throws IOException {...
}
}
![Page 138: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/138.jpg)
@Provider@PreMatchingpublic class HttpPreMatchingFilter implements ContainerRequestFilter {
public void filter(ContainerRequestContext requestContext) throws IOException {...
}
}
![Page 139: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/139.jpg)
@Provider@PreMatchingpublic class HttpPreMatchingFilter implements ContainerRequestFilter {
public void filter(ContainerRequestContext requestContext) throws IOException {...
}
}
![Page 140: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/140.jpg)
Mas se eu quiser logar um método específico, é possível?
![Page 141: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/141.jpg)
@NameBinding
![Page 142: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/142.jpg)
@NameBinding@Target({ ElementType.TYPE, ElementType.METHOD })@Retention(value = RetentionPolicy.RUNTIME)public @interface Logged {}
Similar aos qualificadores do
CDI
![Page 143: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/143.jpg)
@Provider @Loggedpublic class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter {
...}
![Page 144: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/144.jpg)
@Path("/")public class MyResourceClass {
@GET@Logged@Path("{name}")@Produces("text/plain")public String hello(@PathParam("name") String name) {
return "Hello " + name;}
}
![Page 145: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/145.jpg)
E a ordem? Posso definir?
![Page 146: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/146.jpg)
@Priority
![Page 147: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/147.jpg)
@Provider@Authenticated@Priority(Priorities.AUTHENTICATION)public class AuthenticationFilter implements ContainerRequestFilter{
...}
![Page 148: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/148.jpg)
Modifier and Type Constant Field Value
public static final int AUTHENTICATION 1000
public static final int AUTHORIZATION 2000
public static final int ENTITY_CODER 3000
public static final int HEADER_DECORATOR 4000
public static final int USER 5000
javax.ws.rs.Priorities
![Page 149: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/149.jpg)
Bean ValidationJSR-349
![Page 150: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/150.jpg)
Como eu aplico?
![Page 151: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/151.jpg)
public class Book {
private Integer id;
@NotNullprivate String name;
private String description;private Integer year;private String genero;
}
![Page 152: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/152.jpg)
@POST@Consumes(MediaType.APPLICATION_JSON)public Response create(@Valid Book book) {
ResponseBuilder rb = Response.ok(createObject(book));return rb.build();
}
![Page 153: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/153.jpg)
@GET@Path("/year/{year}")@Produces(MediaType.APPLICATION_JSON)public Response listByYearAndName(@Max(2015) @PathParam("year") Integer year, @QueryParam("name") String name) {
List<Book> books = getListByYearAndName(year, name);ResponseBuilder rb = Response.ok(books);return rb.build();
}
![Page 154: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/154.jpg)
HATEOAS
Hipermídia como motor do estado do aplicativo
“If the engine of application is not being driven by hypertext, then it cannot be RESTful and
cannot be a REST API” (Roy T. Fielding)
![Page 155: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/155.jpg)
Book Purchase
Related Books
Author
Receipt
/books/1/author
/books/genre/programming
/books/1/purchase
/books/1/purchase/12/receipt
/books/1
Exemplo
![Page 156: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/156.jpg)
Link: <http://gujavasc.org/resources/books/1/purchase>; rel=purchase, …
<book>
<id>1</id>
<year>2013</year>
<name>REST in practice</name>
<genre>programming</genre>
<author>http://gujavasc.org/resources/books/1/author</author>
<related>http://gujavasc.org/resources/books/genre/programming</related>
</book>
Links
Transicionais
Links
Estruturais
![Page 157: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/157.jpg)
Links Transicionais
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response read(@PathParam("id") Integer id, @Context UriInfo uriInfo){
Link purchaseLink = Link.fromPath(uriInfo.getRequestUri().toString() + "/purchase")
.rel("purchase")
.build();
return Response.ok(readObject(id))
.links(purchaseLink)
.link(uriInfo.getRequestUri().toString() + "/genre/programming", "genre")
.build();
}
![Page 158: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/158.jpg)
Links Transicionais
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response read(@PathParam("id") Integer id, @Context UriInfo uriInfo){
Link purchaseLink = Link.fromPath(uriInfo.getRequestUri().toString() + "/purchase")
.rel("purchase")
.build();
return Response.ok(readObject(id))
.links(purchaseLink)
.link(uriInfo.getRequestUri().toString() + "/genre/programming", "genre")
.build();
}
![Page 159: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/159.jpg)
Links Transicionais
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response read(@PathParam("id") Integer id, @Context UriInfo uriInfo){
Link purchaseLink = Link.fromPath(uriInfo.getRequestUri().toString() + "/purchase")
.rel("purchase")
.build();
return Response.ok(readObject(id))
.links(purchaseLink)
.link(uriInfo.getRequestUri().toString() + "/genre/programming", "genre")
.build();
}
![Page 160: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/160.jpg)
Links Transicionais
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response read(@PathParam("id") Integer id, @Context UriInfo uriInfo){
Link purchaseLink = Link.fromPath(uriInfo.getRequestUri().toString() + "/purchase")
.rel("purchase")
.build();
return Response.ok(readObject(id))
.links(purchaseLink)
.link(uriInfo.getRequestUri().toString() + "/genre/programming", "genre")
.build();
}
![Page 161: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/161.jpg)
Links Transicionais
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response read(@PathParam("id") Integer id, @Context UriInfo uriInfo){
Link purchaseLink = Link.fromPath(uriInfo.getRequestUri().toString() + "/purchase")
.rel("purchase")
.build();
return Response.ok(readObject(id))
.links(purchaseLink)
.link(uriInfo.getRequestUri().toString() + "/genre/programming", "genre")
.build();
}
![Page 162: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/162.jpg)
Links Transicionais
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response read(@PathParam("id") Integer id, @Context UriInfo uriInfo){
Link purchaseLink = Link.fromPath(uriInfo.getRequestUri().toString() + "/purchase")
.rel("purchase")
.build();
return Response.ok(readObject(id))
.links(purchaseLink)
.link(uriInfo.getRequestUri().toString() + "/genre/programming", "genre")
.build();
}
![Page 163: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/163.jpg)
Links Transicionais
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response read(@PathParam("id") Integer id, @Context UriInfo uriInfo){
Link purchaseLink = Link.fromPath(uriInfo.getRequestUri().toString() + "/purchase")
.rel("purchase")
.build();
return Response.ok(readObject(id))
.links(purchaseLink)
.link(uriInfo.getRequestUri().toString() + "/genre/programming", "genre")
.build();
}
![Page 164: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/164.jpg)
Links Transicionais
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response read(@PathParam("id") Integer id, @Context UriInfo uriInfo){
Link purchaseLink = Link.fromPath(uriInfo.getRequestUri().toString() + "/purchase")
.rel("purchase")
.build();
return Response.ok(readObject(id))
.links(purchaseLink)
.link(uriInfo.getRequestUri().toString() + "/genre/programming", "genre")
.build();
}
![Page 165: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/165.jpg)
Integração com Java EE
Managed Beans
CDI
EJB
Bean Validation
JSON API
![Page 166: JSR 339 - Java API for RESTful Web Services](https://reader034.vdocuments.site/reader034/viewer/2022052215/55a7b6771a28abc8168b48aa/html5/thumbnails/166.jpg)
OBRIGADO!
Daniel Cunha (Soro) - @dvlc_
Ivan Junckes Filho - @ivanjunckes
Ricardo Longa - @ricardolonga
https://github.com/gujavasc/jaxrs2