From c14e7ad1a17b34a60b74a4a4b63e8850567365d6 Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sat, 30 Jan 2016 17:48:56 +0100 Subject: [PATCH 01/84] RestEasy Tutorial, CRUD Services example --- RestEasy Example/pom.xml | 91 +++ .../src/main/java/com/baeldung/Movie.java | 535 ++++++++++++++++++ .../baeldung/client/ServicesInterface.java | 44 ++ .../server/service/MovieCrudService.java | 94 +++ .../server/service/RestEasyServices.java | 40 ++ .../src/main/resources/schema1.xsd | 29 + .../main/webapp/WEB-INF/classes/logback.xml | 3 + .../WEB-INF/jboss-deployment-structure.xml | 16 + .../src/main/webapp/WEB-INF/jboss-web.xml | 4 + .../src/main/webapp/WEB-INF/web.xml | 51 ++ .../com/baeldung/server/RestEasyClient.java | 50 ++ .../resources/server/movies/transformer.json | 22 + 12 files changed, 979 insertions(+) create mode 100644 RestEasy Example/pom.xml create mode 100644 RestEasy Example/src/main/java/com/baeldung/Movie.java create mode 100644 RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java create mode 100644 RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java create mode 100644 RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java create mode 100644 RestEasy Example/src/main/resources/schema1.xsd create mode 100644 RestEasy Example/src/main/webapp/WEB-INF/classes/logback.xml create mode 100644 RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml create mode 100644 RestEasy Example/src/main/webapp/WEB-INF/jboss-web.xml create mode 100644 RestEasy Example/src/main/webapp/WEB-INF/web.xml create mode 100644 RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java create mode 100644 RestEasy Example/src/test/resources/server/movies/transformer.json diff --git a/RestEasy Example/pom.xml b/RestEasy Example/pom.xml new file mode 100644 index 0000000000..b16c5e8267 --- /dev/null +++ b/RestEasy Example/pom.xml @@ -0,0 +1,91 @@ + + + 4.0.0 + + com.baeldung + resteasy-tutorial + 1.0 + war + + + + jboss + http://repository.jboss.org/nexus/content/groups/public/ + + + + + 3.0.14.Final + runtime + + + + RestEasyTutorial + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + + org.jboss.resteasy + jaxrs-api + 3.0.12.Final + ${resteasy.scope} + + + + org.jboss.resteasy + resteasy-servlet-initializer + ${resteasy.version} + ${resteasy.scope} + + + jboss-jaxrs-api_2.0_spec + org.jboss.spec.javax.ws.rs + + + + + + org.jboss.resteasy + resteasy-client + ${resteasy.version} + ${resteasy.scope} + + + + + javax.ws.rs + javax.ws.rs-api + 2.0.1 + + + + org.jboss.resteasy + resteasy-jackson-provider + ${resteasy.version} + ${resteasy.scope} + + + + org.jboss.resteasy + resteasy-jaxb-provider + ${resteasy.version} + ${resteasy.scope} + + + + + + \ No newline at end of file diff --git a/RestEasy Example/src/main/java/com/baeldung/Movie.java b/RestEasy Example/src/main/java/com/baeldung/Movie.java new file mode 100644 index 0000000000..c0041d2e95 --- /dev/null +++ b/RestEasy Example/src/main/java/com/baeldung/Movie.java @@ -0,0 +1,535 @@ + +package com.baeldung; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlType; + + +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "movie", propOrder = { + "actors", + "awards", + "country", + "director", + "genre", + "imdbID", + "imdbRating", + "imdbVotes", + "language", + "metascore", + "plot", + "poster", + "rated", + "released", + "response", + "runtime", + "title", + "type", + "writer", + "year" +}) +public class Movie { + + protected String actors; + protected String awards; + protected String country; + protected String director; + protected String genre; + protected String imdbID; + protected String imdbRating; + protected String imdbVotes; + protected String language; + protected String metascore; + protected String plot; + protected String poster; + protected String rated; + protected String released; + protected String response; + protected String runtime; + protected String title; + protected String type; + protected String writer; + protected String year; + + /** + * Recupera il valore della propriet� actors. + * + * @return + * possible object is + * {@link String } + * + */ + public String getActors() { + return actors; + } + + /** + * Imposta il valore della propriet� actors. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setActors(String value) { + this.actors = value; + } + + /** + * Recupera il valore della propriet� awards. + * + * @return + * possible object is + * {@link String } + * + */ + public String getAwards() { + return awards; + } + + /** + * Imposta il valore della propriet� awards. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setAwards(String value) { + this.awards = value; + } + + /** + * Recupera il valore della propriet� country. + * + * @return + * possible object is + * {@link String } + * + */ + public String getCountry() { + return country; + } + + /** + * Imposta il valore della propriet� country. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setCountry(String value) { + this.country = value; + } + + /** + * Recupera il valore della propriet� director. + * + * @return + * possible object is + * {@link String } + * + */ + public String getDirector() { + return director; + } + + /** + * Imposta il valore della propriet� director. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setDirector(String value) { + this.director = value; + } + + /** + * Recupera il valore della propriet� genre. + * + * @return + * possible object is + * {@link String } + * + */ + public String getGenre() { + return genre; + } + + /** + * Imposta il valore della propriet� genre. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setGenre(String value) { + this.genre = value; + } + + /** + * Recupera il valore della propriet� imdbID. + * + * @return + * possible object is + * {@link String } + * + */ + public String getImdbID() { + return imdbID; + } + + /** + * Imposta il valore della propriet� imdbID. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setImdbID(String value) { + this.imdbID = value; + } + + /** + * Recupera il valore della propriet� imdbRating. + * + * @return + * possible object is + * {@link String } + * + */ + public String getImdbRating() { + return imdbRating; + } + + /** + * Imposta il valore della propriet� imdbRating. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setImdbRating(String value) { + this.imdbRating = value; + } + + /** + * Recupera il valore della propriet� imdbVotes. + * + * @return + * possible object is + * {@link String } + * + */ + public String getImdbVotes() { + return imdbVotes; + } + + /** + * Imposta il valore della propriet� imdbVotes. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setImdbVotes(String value) { + this.imdbVotes = value; + } + + /** + * Recupera il valore della propriet� language. + * + * @return + * possible object is + * {@link String } + * + */ + public String getLanguage() { + return language; + } + + /** + * Imposta il valore della propriet� language. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setLanguage(String value) { + this.language = value; + } + + /** + * Recupera il valore della propriet� metascore. + * + * @return + * possible object is + * {@link String } + * + */ + public String getMetascore() { + return metascore; + } + + /** + * Imposta il valore della propriet� metascore. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setMetascore(String value) { + this.metascore = value; + } + + /** + * Recupera il valore della propriet� plot. + * + * @return + * possible object is + * {@link String } + * + */ + public String getPlot() { + return plot; + } + + /** + * Imposta il valore della propriet� plot. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setPlot(String value) { + this.plot = value; + } + + /** + * Recupera il valore della propriet� poster. + * + * @return + * possible object is + * {@link String } + * + */ + public String getPoster() { + return poster; + } + + /** + * Imposta il valore della propriet� poster. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setPoster(String value) { + this.poster = value; + } + + /** + * Recupera il valore della propriet� rated. + * + * @return + * possible object is + * {@link String } + * + */ + public String getRated() { + return rated; + } + + /** + * Imposta il valore della propriet� rated. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setRated(String value) { + this.rated = value; + } + + /** + * Recupera il valore della propriet� released. + * + * @return + * possible object is + * {@link String } + * + */ + public String getReleased() { + return released; + } + + /** + * Imposta il valore della propriet� released. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setReleased(String value) { + this.released = value; + } + + /** + * Recupera il valore della propriet� response. + * + * @return + * possible object is + * {@link String } + * + */ + public String getResponse() { + return response; + } + + /** + * Imposta il valore della propriet� response. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setResponse(String value) { + this.response = value; + } + + /** + * Recupera il valore della propriet� runtime. + * + * @return + * possible object is + * {@link String } + * + */ + public String getRuntime() { + return runtime; + } + + /** + * Imposta il valore della propriet� runtime. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setRuntime(String value) { + this.runtime = value; + } + + /** + * Recupera il valore della propriet� title. + * + * @return + * possible object is + * {@link String } + * + */ + public String getTitle() { + return title; + } + + /** + * Imposta il valore della propriet� title. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setTitle(String value) { + this.title = value; + } + + /** + * Recupera il valore della propriet� type. + * + * @return + * possible object is + * {@link String } + * + */ + public String getType() { + return type; + } + + /** + * Imposta il valore della propriet� type. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setType(String value) { + this.type = value; + } + + /** + * Recupera il valore della propriet� writer. + * + * @return + * possible object is + * {@link String } + * + */ + public String getWriter() { + return writer; + } + + /** + * Imposta il valore della propriet� writer. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setWriter(String value) { + this.writer = value; + } + + /** + * Recupera il valore della propriet� year. + * + * @return + * possible object is + * {@link String } + * + */ + public String getYear() { + return year; + } + + /** + * Imposta il valore della propriet� year. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setYear(String value) { + this.year = value; + } + +} diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java new file mode 100644 index 0000000000..53e88961be --- /dev/null +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -0,0 +1,44 @@ +package com.baeldung.client; + +import com.baeldung.Movie; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + + +public interface ServicesInterface { + + + @GET + @Path("/getinfo") + @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + Movie movieByImdbID(@QueryParam("imdbID") String imdbID); + + + @POST + @Path("/addmovie") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + Response addMovie(Movie movie); + + + @PUT + @Path("/updatemovie") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + Response updateMovie(Movie movie); + + + @DELETE + @Path("/deletemovie") + Response deleteMovie(@QueryParam("imdbID") String imdbID); + + + @GET + @Path("/listmovies") + @Produces({"application/json"}) + List listMovies(); + +} diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java new file mode 100644 index 0000000000..d1973e7037 --- /dev/null +++ b/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java @@ -0,0 +1,94 @@ +package com.baeldung.server.service; + +import com.baeldung.Movie; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.Provider; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + + +@Path("/movies") +public class MovieCrudService { + + + private Map inventory = new HashMap(); + + @GET + @Path("/getinfo") + @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){ + + System.out.println("*** Calling getinfo ***"); + + Movie movie=new Movie(); + movie.setImdbID(imdbID); + return movie; + } + + @POST + @Path("/addmovie") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + public Response addMovie(Movie movie){ + + System.out.println("*** Calling addMovie ***"); + + if (null!=inventory.get(movie.getImdbID())){ + return Response.status(Response.Status.NOT_MODIFIED) + .entity("Movie is Already in the database.").build(); + } + inventory.put(movie.getImdbID(),movie); + + return Response.status(Response.Status.CREATED).build(); + } + + + @PUT + @Path("/updatemovie") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + public Response updateMovie(Movie movie){ + + System.out.println("*** Calling updateMovie ***"); + + if (null!=inventory.get(movie.getImdbID())){ + return Response.status(Response.Status.NOT_MODIFIED) + .entity("Movie is not in the database.\nUnable to Update").build(); + } + inventory.put(movie.getImdbID(),movie); + return Response.status(Response.Status.OK).build(); + + } + + + @DELETE + @Path("/deletemovie") + public Response deleteMovie(@QueryParam("imdbID") String imdbID){ + + System.out.println("*** Calling deleteMovie ***"); + + if (null==inventory.get(imdbID)){ + return Response.status(Response.Status.NOT_FOUND) + .entity("Movie is not in the database.\nUnable to Delete").build(); + } + + inventory.remove(imdbID); + return Response.status(Response.Status.OK).build(); + } + + @GET + @Path("/listmovies") + @Produces({"application/json"}) + public List listMovies(){ + + return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new)); + + } + + + +} diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java b/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java new file mode 100644 index 0000000000..16b6200ad1 --- /dev/null +++ b/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java @@ -0,0 +1,40 @@ +package com.baeldung.server.service; + + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * Created by Admin on 29/01/2016. + */ + + + +@ApplicationPath("/rest") +public class RestEasyServices extends Application { + + private Set singletons = new HashSet(); + + public RestEasyServices() { + singletons.add(new MovieCrudService()); + } + + @Override + public Set getSingletons() { + return singletons; + } + + @Override + public Set> getClasses() { + return super.getClasses(); + } + + @Override + public Map getProperties() { + return super.getProperties(); + } +} diff --git a/RestEasy Example/src/main/resources/schema1.xsd b/RestEasy Example/src/main/resources/schema1.xsd new file mode 100644 index 0000000000..0d74b7c366 --- /dev/null +++ b/RestEasy Example/src/main/resources/schema1.xsd @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/RestEasy Example/src/main/webapp/WEB-INF/classes/logback.xml b/RestEasy Example/src/main/webapp/WEB-INF/classes/logback.xml new file mode 100644 index 0000000000..d94e9f71ab --- /dev/null +++ b/RestEasy Example/src/main/webapp/WEB-INF/classes/logback.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml b/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml new file mode 100644 index 0000000000..84d75934a7 --- /dev/null +++ b/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RestEasy Example/src/main/webapp/WEB-INF/jboss-web.xml b/RestEasy Example/src/main/webapp/WEB-INF/jboss-web.xml new file mode 100644 index 0000000000..694bb71332 --- /dev/null +++ b/RestEasy Example/src/main/webapp/WEB-INF/jboss-web.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/RestEasy Example/src/main/webapp/WEB-INF/web.xml b/RestEasy Example/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..c66d3b56ae --- /dev/null +++ b/RestEasy Example/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,51 @@ + + + + RestEasy Example + + + + org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap + + + + RestEasy Example + + + webAppRootKey + RestEasyExample + + + + + + resteasy.servlet.mapping.prefix + /rest + + + + + resteasy-servlet + + org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher + + + javax.ws.rs.Application + com.baeldung.server.service.RestEasyServices + + + + + resteasy-servlet + /rest/* + + + + + index.html + + + + \ No newline at end of file diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java new file mode 100644 index 0000000000..e711233979 --- /dev/null +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java @@ -0,0 +1,50 @@ +package com.baeldung.server; + +import com.baeldung.Movie; +import com.baeldung.client.ServicesInterface; +import org.jboss.resteasy.client.jaxrs.ResteasyClient; +import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; +import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; + +import java.util.List; + +public class RestEasyClient { + + public static void main(String[] args) { + + Movie st = new Movie(); + st.setImdbID("12345"); + + /* + * Alternatively you can use this simple String to send + * instead of using a Student instance + * + * String jsonString = "{\"id\":12,\"firstName\":\"Catain\",\"lastName\":\"Hook\",\"age\":10}"; + */ + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target("http://localhost:8080/RestEasyTutorial/rest/movies/listmovies"); + + ServicesInterface simple = target.proxy(ServicesInterface.class); + final List movies = simple.listMovies(); + + /* + if (response.getStatus() != 200) { + throw new RuntimeException("Failed : HTTP error code : " + + response.getStatus()); + } + + System.out.println("Server response : \n"); + System.out.println(response.readEntity(String.class)); + + response.close(); +*/ + } catch (Exception e) { + + e.printStackTrace(); + + } + } + +} \ No newline at end of file diff --git a/RestEasy Example/src/test/resources/server/movies/transformer.json b/RestEasy Example/src/test/resources/server/movies/transformer.json new file mode 100644 index 0000000000..2154868265 --- /dev/null +++ b/RestEasy Example/src/test/resources/server/movies/transformer.json @@ -0,0 +1,22 @@ +{ + "title": "Transformers", + "year": "2007", + "rated": "PG-13", + "released": "03 Jul 2007", + "runtime": "144 min", + "genre": "Action, Adventure, Sci-Fi", + "director": "Michael Bay", + "writer": "Roberto Orci (screenplay), Alex Kurtzman (screenplay), John Rogers (story), Roberto Orci (story), Alex Kurtzman (story)", + "actors": "Shia LaBeouf, Megan Fox, Josh Duhamel, Tyrese Gibson", + "plot": "A long time ago, far away on the planet of Cybertron, a war is being waged between the noble Autobots (led by the wise Optimus Prime) and the devious Decepticons (commanded by the dreaded Megatron) for control over the Allspark, a mystical talisman that would grant unlimited power to whoever possesses it. The Autobots managed to smuggle the Allspark off the planet, but Megatron blasts off in search of it. He eventually tracks it to the planet of Earth (circa 1850), but his reckless desire for power sends him right into the Arctic Ocean, and the sheer cold forces him into a paralyzed state. His body is later found by Captain Archibald Witwicky, but before going into a comatose state Megatron uses the last of his energy to engrave into the Captain's glasses a map showing the location of the Allspark, and to send a transmission to Cybertron. Megatron is then carried away aboard the Captain's ship. A century later, Captain Witwicky's grandson Sam Witwicky (nicknamed Spike by his friends) buys his first car. To his shock, he discovers it to be Bumblebee, an Autobot in disguise who is to protect Spike, who possesses the Captain's glasses and the map engraved on them. But Bumblebee is not the only Transformer to have arrived on Earth - in the desert of Qatar, the Decepticons Blackout and Scorponok attack a U.S. military base, causing the Pentagon to send their special Sector Seven agents to capture all \"specimens of this alien race.\" Spike and his girlfriend Mikaela find themselves in the midst of a grand battle between the Autobots and the Decepticons, stretching from Hoover Dam all the way to Los Angeles. Meanwhile, deep inside Hoover Dam, the cryogenically stored body of Megatron awakens.", + "language": "English, Spanish", + "country": "USA", + "awards": "Nominated for 3 Oscars. Another 18 wins & 40 nominations.", + "poster": "http://ia.media-imdb.com/images/M/MV5BMTQwNjU5MzUzNl5BMl5BanBnXkFtZTYwMzc1MTI3._V1_SX300.jpg", + "metascore": "61", + "imdbRating": "7.1", + "imdbVotes": "492,225", + "imdbID": "tt0418279", + "Type": "movie", + "response": "True" +} \ No newline at end of file From a85c9cbda573fc25ac4d0f133eaf6ed779611ad6 Mon Sep 17 00:00:00 2001 From: Giuseppe Bueti Date: Sat, 30 Jan 2016 20:39:28 +0100 Subject: [PATCH 02/84] Added testCase for List All Movies and Add a Movie --- RestEasy Example/pom.xml | 29 +++++ .../baeldung/client/ServicesInterface.java | 6 +- .../java/com/baeldung/{ => model}/Movie.java | 45 +++++++- .../{service => }/MovieCrudService.java | 5 +- .../{service => }/RestEasyServices.java | 2 +- .../com/baeldung/server/RestEasyClient.java | 104 ++++++++++++++---- .../com/baeldung/server/movies/batman.json | 22 ++++ .../baeldung}/server/movies/transformer.json | 2 +- 8 files changed, 184 insertions(+), 31 deletions(-) rename RestEasy Example/src/main/java/com/baeldung/{ => model}/Movie.java (86%) rename RestEasy Example/src/main/java/com/baeldung/server/{service => }/MovieCrudService.java (96%) rename RestEasy Example/src/main/java/com/baeldung/server/{service => }/RestEasyServices.java (95%) create mode 100644 RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json rename RestEasy Example/src/test/resources/{ => com/baeldung}/server/movies/transformer.json (99%) diff --git a/RestEasy Example/pom.xml b/RestEasy Example/pom.xml index b16c5e8267..8dabfc863b 100644 --- a/RestEasy Example/pom.xml +++ b/RestEasy Example/pom.xml @@ -85,6 +85,35 @@ ${resteasy.scope} + + + + junit + junit + 4.4 + + + + commons-io + commons-io + 2.4 + + + + com.fasterxml.jackson.core + jackson-core + 2.7.0 + + + + com.fasterxml.jackson.core + jackson-annotations + 2.7.0 + + + + + diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java index 53e88961be..2585c32438 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -1,15 +1,13 @@ package com.baeldung.client; -import com.baeldung.Movie; +import com.baeldung.model.Movie; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; - +@Path("/movies") public interface ServicesInterface { diff --git a/RestEasy Example/src/main/java/com/baeldung/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java similarity index 86% rename from RestEasy Example/src/main/java/com/baeldung/Movie.java rename to RestEasy Example/src/main/java/com/baeldung/model/Movie.java index c0041d2e95..052ba081c1 100644 --- a/RestEasy Example/src/main/java/com/baeldung/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -1,5 +1,5 @@ -package com.baeldung; +package com.baeldung.model; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; @@ -532,4 +532,47 @@ public class Movie { this.year = value; } + @Override + public String toString() { + return "Movie{" + + "actors='" + actors + '\'' + + ", awards='" + awards + '\'' + + ", country='" + country + '\'' + + ", director='" + director + '\'' + + ", genre='" + genre + '\'' + + ", imdbID='" + imdbID + '\'' + + ", imdbRating='" + imdbRating + '\'' + + ", imdbVotes='" + imdbVotes + '\'' + + ", language='" + language + '\'' + + ", metascore='" + metascore + '\'' + + ", poster='" + poster + '\'' + + ", rated='" + rated + '\'' + + ", released='" + released + '\'' + + ", response='" + response + '\'' + + ", runtime='" + runtime + '\'' + + ", title='" + title + '\'' + + ", type='" + type + '\'' + + ", writer='" + writer + '\'' + + ", year='" + year + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Movie movie = (Movie) o; + + if (imdbID != null ? !imdbID.equals(movie.imdbID) : movie.imdbID != null) return false; + return title != null ? title.equals(movie.title) : movie.title == null; + + } + + @Override + public int hashCode() { + int result = imdbID != null ? imdbID.hashCode() : 0; + result = 31 * result + (title != null ? title.hashCode() : 0); + return result; + } } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java similarity index 96% rename from RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java rename to RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index d1973e7037..60e0121966 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -1,11 +1,10 @@ -package com.baeldung.server.service; +package com.baeldung.server; -import com.baeldung.Movie; +import com.baeldung.model.Movie; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import javax.ws.rs.ext.Provider; import java.util.ArrayList; import java.util.HashMap; import java.util.List; diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java b/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java similarity index 95% rename from RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java rename to RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java index 16b6200ad1..8c57d2c9b4 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java @@ -1,4 +1,4 @@ -package com.baeldung.server.service; +package com.baeldung.server; import javax.ws.rs.ApplicationPath; diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java index e711233979..c77f494862 100644 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java @@ -1,49 +1,111 @@ package com.baeldung.server; -import com.baeldung.Movie; +import com.baeldung.model.Movie; import com.baeldung.client.ServicesInterface; +import org.apache.commons.io.IOUtils; +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; import org.jboss.resteasy.client.jaxrs.ResteasyClient; import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import javax.naming.NamingException; +import javax.ws.rs.core.Link; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.List; +import java.util.Locale; public class RestEasyClient { - public static void main(String[] args) { - Movie st = new Movie(); - st.setImdbID("12345"); + Movie transformerMovie=null; + Movie batmanMovie=null; + ObjectMapper jsonMapper=null; - /* - * Alternatively you can use this simple String to send - * instead of using a Student instance - * - * String jsonString = "{\"id\":12,\"firstName\":\"Catain\",\"lastName\":\"Hook\",\"age\":10}"; - */ + @BeforeClass + public static void loadMovieInventory(){ + + + + } + + @Before + public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { + + + jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); + jsonMapper.setDateFormat(sdf); + + try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/transformer.json")) { + String transformerMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + transformerMovie = jsonMapper.readValue(transformerMovieAsString, Movie.class); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/batman.json")) { + String batmanMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + } + + @Test + public void testListAllMovies() { try { ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target("http://localhost:8080/RestEasyTutorial/rest/movies/listmovies"); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + final List movies = simple.listMovies(); + System.out.println(movies); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + + @Test + public void testAddMovie() { + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); ServicesInterface simple = target.proxy(ServicesInterface.class); - final List movies = simple.listMovies(); + final Response moviesResponse = simple.addMovie(batmanMovie); - /* - if (response.getStatus() != 200) { + if (moviesResponse.getStatus() != 201) { + System.out.println(moviesResponse.readEntity(String.class)); throw new RuntimeException("Failed : HTTP error code : " - + response.getStatus()); + + moviesResponse.getStatus()); } - System.out.println("Server response : \n"); - System.out.println(response.readEntity(String.class)); + moviesResponse.close(); - response.close(); -*/ } catch (Exception e) { - e.printStackTrace(); - } } diff --git a/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json b/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json new file mode 100644 index 0000000000..28061d5bf9 --- /dev/null +++ b/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json @@ -0,0 +1,22 @@ +{ + "title": "Batman", + "year": "1989", + "rated": "PG-13", + "released": "23 Jun 1989", + "runtime": "126 min", + "genre": "Action, Adventure", + "director": "Tim Burton", + "writer": "Bob Kane (Batman characters), Sam Hamm (story), Sam Hamm (screenplay), Warren Skaaren (screenplay)", + "actors": "Michael Keaton, Jack Nicholson, Kim Basinger, Robert Wuhl", + "plot": "The Dark Knight of Gotham City begins his war on crime with his first major enemy being the clownishly homicidal Joker.", + "language": "English, French", + "country": "USA, UK", + "awards": "Won 1 Oscar. Another 9 wins & 22 nominations.", + "poster": "http://ia.media-imdb.com/images/M/MV5BMTYwNjAyODIyMF5BMl5BanBnXkFtZTYwNDMwMDk2._V1_SX300.jpg", + "metascore": "66", + "imdbRating": "7.6", + "imdbVotes": "256,000", + "imdbID": "tt0096895", + "type": "movie", + "response": "True" +} \ No newline at end of file diff --git a/RestEasy Example/src/test/resources/server/movies/transformer.json b/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json similarity index 99% rename from RestEasy Example/src/test/resources/server/movies/transformer.json rename to RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json index 2154868265..a3b033a8ba 100644 --- a/RestEasy Example/src/test/resources/server/movies/transformer.json +++ b/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json @@ -17,6 +17,6 @@ "imdbRating": "7.1", "imdbVotes": "492,225", "imdbID": "tt0418279", - "Type": "movie", + "type": "movie", "response": "True" } \ No newline at end of file From a2a026a503167a5e3a0fb2f3e8856686ce1428cc Mon Sep 17 00:00:00 2001 From: Giuseppe Bueti Date: Sun, 31 Jan 2016 11:05:11 +0100 Subject: [PATCH 03/84] Added testCase for all Services --- RestEasy Example/pom.xml | 15 -- .../baeldung/client/ServicesInterface.java | 10 +- .../com/baeldung/server/MovieCrudService.java | 15 +- .../src/main/webapp/WEB-INF/web.xml | 2 +- .../com/baeldung/server/RestEasyClient.java | 112 ----------- .../baeldung/server/RestEasyClientTest.java | 189 ++++++++++++++++++ 6 files changed, 206 insertions(+), 137 deletions(-) delete mode 100644 RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java create mode 100644 RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java diff --git a/RestEasy Example/pom.xml b/RestEasy Example/pom.xml index 8dabfc863b..6935238d91 100644 --- a/RestEasy Example/pom.xml +++ b/RestEasy Example/pom.xml @@ -99,21 +99,6 @@ 2.4 - - com.fasterxml.jackson.core - jackson-core - 2.7.0 - - - - com.fasterxml.jackson.core - jackson-annotations - 2.7.0 - - - - - diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java index 2585c32438..7efed546d8 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -17,6 +17,12 @@ public interface ServicesInterface { Movie movieByImdbID(@QueryParam("imdbID") String imdbID); + @GET + @Path("/listmovies") + @Produces({"application/json"}) + List listMovies(); + + @POST @Path("/addmovie") @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @@ -34,9 +40,5 @@ public interface ServicesInterface { Response deleteMovie(@QueryParam("imdbID") String imdbID); - @GET - @Path("/listmovies") - @Produces({"application/json"}) - List listMovies(); } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index 60e0121966..18366e2faa 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -18,18 +18,21 @@ public class MovieCrudService { private Map inventory = new HashMap(); + @GET @Path("/getinfo") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){ - System.out.println("*** Calling getinfo ***"); + System.out.println("*** Calling getinfo for a given ImdbID***"); + + if(inventory.containsKey(imdbID)){ + return inventory.get(imdbID); + }else return null; - Movie movie=new Movie(); - movie.setImdbID(imdbID); - return movie; } + @POST @Path("/addmovie") @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @@ -41,6 +44,7 @@ public class MovieCrudService { return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is Already in the database.").build(); } + inventory.put(movie.getImdbID(),movie); return Response.status(Response.Status.CREATED).build(); @@ -54,7 +58,7 @@ public class MovieCrudService { System.out.println("*** Calling updateMovie ***"); - if (null!=inventory.get(movie.getImdbID())){ + if (null==inventory.get(movie.getImdbID())){ return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is not in the database.\nUnable to Update").build(); } @@ -79,6 +83,7 @@ public class MovieCrudService { return Response.status(Response.Status.OK).build(); } + @GET @Path("/listmovies") @Produces({"application/json"}) diff --git a/RestEasy Example/src/main/webapp/WEB-INF/web.xml b/RestEasy Example/src/main/webapp/WEB-INF/web.xml index c66d3b56ae..ab3bc1aa83 100644 --- a/RestEasy Example/src/main/webapp/WEB-INF/web.xml +++ b/RestEasy Example/src/main/webapp/WEB-INF/web.xml @@ -33,7 +33,7 @@ javax.ws.rs.Application - com.baeldung.server.service.RestEasyServices + com.baeldung.server.RestEasyServices diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java deleted file mode 100644 index c77f494862..0000000000 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.baeldung.server; - -import com.baeldung.model.Movie; -import com.baeldung.client.ServicesInterface; -import org.apache.commons.io.IOUtils; -import org.codehaus.jackson.map.DeserializationConfig; -import org.codehaus.jackson.map.ObjectMapper; -import org.jboss.resteasy.client.jaxrs.ResteasyClient; -import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; -import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import javax.naming.NamingException; -import javax.ws.rs.core.Link; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriBuilder; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileReader; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.List; -import java.util.Locale; - -public class RestEasyClient { - - - Movie transformerMovie=null; - Movie batmanMovie=null; - ObjectMapper jsonMapper=null; - - @BeforeClass - public static void loadMovieInventory(){ - - - - } - - @Before - public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { - - - jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); - jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); - SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); - jsonMapper.setDateFormat(sdf); - - try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/transformer.json")) { - String transformerMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); - transformerMovie = jsonMapper.readValue(transformerMovieAsString, Movie.class); - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException("Test is going to die ...", e); - } - - try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/batman.json")) { - String batmanMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); - batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); - - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException("Test is going to die ...", e); - } - - } - - @Test - public void testListAllMovies() { - - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); - - final List movies = simple.listMovies(); - System.out.println(movies); - - } catch (Exception e) { - e.printStackTrace(); - } - } - - - - @Test - public void testAddMovie() { - - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - - ServicesInterface simple = target.proxy(ServicesInterface.class); - final Response moviesResponse = simple.addMovie(batmanMovie); - - if (moviesResponse.getStatus() != 201) { - System.out.println(moviesResponse.readEntity(String.class)); - throw new RuntimeException("Failed : HTTP error code : " - + moviesResponse.getStatus()); - } - - moviesResponse.close(); - - } catch (Exception e) { - e.printStackTrace(); - } - } - -} \ No newline at end of file diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java new file mode 100644 index 0000000000..fb4205bcd7 --- /dev/null +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java @@ -0,0 +1,189 @@ +package com.baeldung.server; + +import com.baeldung.model.Movie; +import com.baeldung.client.ServicesInterface; +import org.apache.commons.io.IOUtils; +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; +import org.jboss.resteasy.client.jaxrs.ResteasyClient; +import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; +import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import javax.naming.NamingException; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; +import java.util.List; +import java.util.Locale; + +public class RestEasyClientTest { + + + Movie transformerMovie=null; + Movie batmanMovie=null; + ObjectMapper jsonMapper=null; + + @BeforeClass + public static void loadMovieInventory(){ + + + + } + + @Before + public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { + + + jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); + jsonMapper.setDateFormat(sdf); + + try (InputStream inputStream = new RestEasyClientTest().getClass().getResourceAsStream("./movies/transformer.json")) { + String transformerMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + transformerMovie = jsonMapper.readValue(transformerMovieAsString, Movie.class); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + try (InputStream inputStream = new RestEasyClientTest().getClass().getResourceAsStream("./movies/batman.json")) { + String batmanMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + } + + + @Test + public void testListAllMovies() { + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(transformerMovie); + moviesResponse.close(); + moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + + final List movies = simple.listMovies(); + System.out.println(movies); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testMovieByImdbID() { + + String transformerImdbId="tt0418279"; + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(transformerMovie); + moviesResponse.close(); + + final Movie movies = simple.movieByImdbID(transformerImdbId); + System.out.println(movies); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testAddMovie() { + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = simple.addMovie(transformerMovie); + + if (moviesResponse.getStatus() != Response.Status.CREATED.getStatusCode()) { + //System.out.println(moviesResponse.readEntity(String.class)); + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); + } + moviesResponse.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testDeleteMovie() { + + String transformerImdbId="tt0418279"; + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = simple.deleteMovie(transformerImdbId); + moviesResponse.close(); + + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + System.out.println(moviesResponse.readEntity(String.class)); + throw new RuntimeException("Failed : HTTP error code : " + moviesResponse.getStatus()); + } + + moviesResponse.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testUpdateMovie() { + + try { + + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + batmanMovie.setImdbVotes("300,000"); + moviesResponse = simple.updateMovie(batmanMovie); + + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + //System.out.println(moviesResponse.readEntity(String.class)); + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); + } + + moviesResponse.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file From 2fff2739690a712e5d87263c3f1c1c192f51ae9f Mon Sep 17 00:00:00 2001 From: oborkovskyi Date: Mon, 1 Feb 2016 16:12:47 +0200 Subject: [PATCH 04/84] Added guava19 test project --- guava19/pom.xml | 46 +++++++++ .../java/com/baeldung/guava/entity/User.java | 36 +++++++ .../com/baeldung/guava/CharMatcherTest.java | 33 +++++++ .../baeldung/guava/GuavaMiscUtilsTest.java | 95 +++++++++++++++++++ .../java/com/baeldung/guava/HashingTest.java | 34 +++++++ .../com/baeldung/guava/TypeTokenTest.java | 45 +++++++++ 6 files changed, 289 insertions(+) create mode 100644 guava19/pom.xml create mode 100644 guava19/src/main/java/com/baeldung/guava/entity/User.java create mode 100644 guava19/src/test/java/com/baeldung/guava/CharMatcherTest.java create mode 100644 guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java create mode 100644 guava19/src/test/java/com/baeldung/guava/HashingTest.java create mode 100644 guava19/src/test/java/com/baeldung/guava/TypeTokenTest.java diff --git a/guava19/pom.xml b/guava19/pom.xml new file mode 100644 index 0000000000..97c1b0ea4e --- /dev/null +++ b/guava19/pom.xml @@ -0,0 +1,46 @@ + + + 4.0.0 + + com.baeldung + guava + 1.0-SNAPSHOT + + + + com.google.guava + guava + 19.0 + + + junit + junit + 4.12 + + + org.hamcrest + hamcrest-all + 1.3 + + + + + + maven-compiler-plugin + 3.3 + + true + true + 1.8 + 1.8 + UTF-8 + true + true + + + + + + \ No newline at end of file diff --git a/guava19/src/main/java/com/baeldung/guava/entity/User.java b/guava19/src/main/java/com/baeldung/guava/entity/User.java new file mode 100644 index 0000000000..be673edb10 --- /dev/null +++ b/guava19/src/main/java/com/baeldung/guava/entity/User.java @@ -0,0 +1,36 @@ +package com.baeldung.guava.entity; + +import com.google.common.base.MoreObjects; + +public class User{ + private long id; + private String name; + private int age; + + public User(long id, String name, int age) { + this.id = id; + this.name = name; + this.age = age; + } + + public long getId() { + return id; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(User.class) + .add("id", id) + .add("name", name) + .add("age", age) + .toString(); + } +} \ No newline at end of file diff --git a/guava19/src/test/java/com/baeldung/guava/CharMatcherTest.java b/guava19/src/test/java/com/baeldung/guava/CharMatcherTest.java new file mode 100644 index 0000000000..f890d9abc1 --- /dev/null +++ b/guava19/src/test/java/com/baeldung/guava/CharMatcherTest.java @@ -0,0 +1,33 @@ +package com.baeldung.guava; + +import com.google.common.base.CharMatcher; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class CharMatcherTest { + @Test + public void whenMatchingLetterOrString_ShouldReturnTrueForCorrectString() throws Exception { + String inputString = "someString789"; + boolean result = CharMatcher.javaLetterOrDigit().matchesAllOf(inputString); + + assertTrue(result); + } + + @Test + public void whenCollapsingString_ShouldReturnStringWithDashesInsteadOfWhitespaces() throws Exception { + String inputPhoneNumber = "8 123 456 123"; + String result = CharMatcher.whitespace().collapseFrom(inputPhoneNumber, '-'); + + assertEquals("8-123-456-123", result); + } + + @Test + public void whenCountingDigitsInString_ShouldReturnActualCountOfDigits() throws Exception { + String inputPhoneNumber = "8 123 456 123"; + int result = CharMatcher.digit().countIn(inputPhoneNumber); + + assertEquals(10, result); + } +} diff --git a/guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java b/guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java new file mode 100644 index 0000000000..4569019e60 --- /dev/null +++ b/guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java @@ -0,0 +1,95 @@ +package com.baeldung.guava; + +import com.baeldung.guava.entity.User; +import com.google.common.base.CharMatcher; +import com.google.common.base.Throwables; +import com.google.common.collect.*; +import com.google.common.hash.HashCode; +import com.google.common.hash.HashFunction; +import com.google.common.hash.Hashing; +import com.google.common.reflect.TypeToken; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; +import static org.hamcrest.core.AnyOf.anyOf; +import static org.junit.Assert.*; + +public class GuavaMiscUtilsTest { + + @Test + public void whenGettingLazyStackTrace_ListShouldBeReturned() throws Exception { + IllegalArgumentException e = new IllegalArgumentException("Some argument is incorrect"); + + List stackTraceElements = Throwables.lazyStackTrace(e); + + assertEquals(27, stackTraceElements.size()); + } + + @Test + public void multisetShouldCountHitsOfMultipleDuplicateObjects() throws Exception { + List userNames = Arrays.asList("David", "Eugene", "Alex", "Alex", "David", "David", "David"); + + Multiset userNamesMultiset = HashMultiset.create(userNames); + + assertEquals(7, userNamesMultiset.size()); + assertEquals(4, userNamesMultiset.count("David")); + assertEquals(2, userNamesMultiset.count("Alex")); + assertEquals(1, userNamesMultiset.count("Eugene")); + assertThat(userNamesMultiset.elementSet(), anyOf(containsInAnyOrder("Alex", "David", "Eugene"))); + } + + @Test + public void whenAddingNewConnectedRange_RangesShouldBeMerged() throws Exception { + RangeSet rangeSet = TreeRangeSet.create(); + + rangeSet.add(Range.closed(1, 10)); + rangeSet.add(Range.closed(5, 15)); + rangeSet.add(Range.closedOpen(10, 17)); + + assertTrue(rangeSet.encloses(Range.closedOpen(1, 17))); + assertTrue(rangeSet.encloses(Range.closed(2, 3))); + assertTrue(rangeSet.contains(15)); + assertFalse(rangeSet.contains(17)); + assertEquals(1, rangeSet.asDescendingSetOfRanges().size()); + } + + @Test + public void cartesianProductShouldReturnAllPossibleCombinations() throws Exception { + List first = Lists.newArrayList("value1", "value2"); + List second = Lists.newArrayList("value3", "value4"); + + List> cartesianProduct = Lists.cartesianProduct(first, second); + + List pair1 = Lists.newArrayList("value2", "value3"); + List pair2 = Lists.newArrayList("value2", "value4"); + List pair3 = Lists.newArrayList("value1", "value3"); + List pair4 = Lists.newArrayList("value1", "value4"); + + assertThat(cartesianProduct, anyOf(containsInAnyOrder(pair1, pair2, pair3, pair4))); + } + + @Test + public void multisetShouldRemoveOccurrencesOfSpecifiedObjects() throws Exception { + Multiset multisetToModify = HashMultiset.create(); + Multiset occurrencesToRemove = HashMultiset.create(); + + multisetToModify.add("John"); + multisetToModify.add("Max"); + multisetToModify.add("Alex"); + + occurrencesToRemove.add("Alex"); + occurrencesToRemove.add("John"); + + Multisets.removeOccurrences(multisetToModify, occurrencesToRemove); + + assertEquals(1, multisetToModify.size()); + assertTrue(multisetToModify.contains("Max")); + assertFalse(multisetToModify.contains("John")); + assertFalse(multisetToModify.contains("Alex")); + } +} \ No newline at end of file diff --git a/guava19/src/test/java/com/baeldung/guava/HashingTest.java b/guava19/src/test/java/com/baeldung/guava/HashingTest.java new file mode 100644 index 0000000000..9b4acde9f7 --- /dev/null +++ b/guava19/src/test/java/com/baeldung/guava/HashingTest.java @@ -0,0 +1,34 @@ +package com.baeldung.guava; + +import com.google.common.hash.HashCode; +import com.google.common.hash.HashFunction; +import com.google.common.hash.Hashing; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class HashingTest { + @Test + public void whenHashingInSha384_hashFunctionShouldBeReturned() throws Exception { + int inputData = 15; + + HashFunction hashFunction = Hashing.sha384(); + HashCode hashCode = hashFunction.hashInt(inputData); + + assertEquals("0904b6277381dcfbdddd6b6c66e4e3e8f83d4690718d8e6f272c891f24773a12feaf8c449fa6e42240a621b2b5e3cda8", + hashCode.toString()); + } + + @Test + public void whenConcatenatingHashFunction_concatenatedHashShouldBeReturned() throws Exception { + int inputData = 15; + + HashFunction hashFunction = Hashing.concatenating(Hashing.crc32(), Hashing.crc32()); + HashFunction crc32Function = Hashing.crc32(); + + HashCode hashCode = hashFunction.hashInt(inputData); + HashCode crc32HashCode = crc32Function.hashInt(inputData); + + assertEquals(crc32HashCode.toString() + crc32HashCode.toString(), hashCode.toString()); + } +} diff --git a/guava19/src/test/java/com/baeldung/guava/TypeTokenTest.java b/guava19/src/test/java/com/baeldung/guava/TypeTokenTest.java new file mode 100644 index 0000000000..1923451f20 --- /dev/null +++ b/guava19/src/test/java/com/baeldung/guava/TypeTokenTest.java @@ -0,0 +1,45 @@ +package com.baeldung.guava; + +import com.google.common.reflect.TypeToken; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class TypeTokenTest { + @Test + public void whenCheckingIsAssignableFrom_shouldReturnTrueEvenIfGenericIsSpecified() throws Exception { + ArrayList stringList = new ArrayList<>(); + ArrayList intList = new ArrayList<>(); + boolean isAssignableFrom = stringList.getClass().isAssignableFrom(intList.getClass()); + + assertTrue(isAssignableFrom); + } + + @Test + public void whenCheckingIsSupertypeOf_shouldReturnFalseIfGenericIsSpecified() throws Exception { + TypeToken> listString = new TypeToken>() { + }; + TypeToken> integerString = new TypeToken>() { + }; + + boolean isSupertypeOf = listString.isSupertypeOf(integerString); + + assertFalse(isSupertypeOf); + } + + @Test + public void whenCheckingIsSubtypeOf_shouldReturnTrueIfClassIsExtendedFrom() throws Exception { + TypeToken> stringList = new TypeToken>() { + }; + TypeToken list = new TypeToken() { + }; + + boolean isSubtypeOf = stringList.isSubtypeOf(list); + + assertTrue(isSubtypeOf); + } +} From 0d45b71098d3dc53b0abe2ae9af500fc1a58c410 Mon Sep 17 00:00:00 2001 From: Kevin Gilmore Date: Sat, 6 Feb 2016 11:32:12 -0600 Subject: [PATCH 05/84] API for RAML modularization article --- .../api-before-modularization.raml | 119 ++++++++++++++++++ raml/modularization/api-with-libraries.raml | 50 ++++++++ .../api-with-typed-fragments.raml | 74 +++++++++++ raml/modularization/api.raml | 47 +++++++ raml/modularization/examples/Bar.json | 6 + raml/modularization/examples/Bars.json | 19 +++ raml/modularization/examples/Error.json | 4 + raml/modularization/examples/Foo.json | 4 + raml/modularization/examples/Foos.json | 16 +++ .../extensions/en_US/additionalResources.raml | 16 +++ raml/modularization/libraries/dataTypes.raml | 19 +++ .../libraries/resourceTypes.raml | 32 +++++ .../libraries/securitySchemes.raml | 20 +++ raml/modularization/libraries/traits.raml | 33 +++++ .../overlays/es_ES/additionalResources.raml | 13 ++ .../overlays/es_ES/documentationItems.raml | 23 ++++ .../resourceTypes/collection.raml | 12 ++ raml/modularization/resourceTypes/item.raml | 14 +++ raml/modularization/traits/hasNotFound.raml | 8 ++ .../modularization/traits/hasRequestItem.raml | 5 + .../traits/hasResponseCollection.raml | 8 ++ .../traits/hasResponseItem.raml | 8 ++ raml/modularization/types/Bar.raml | 7 ++ raml/modularization/types/Error.raml | 5 + raml/modularization/types/Foo.raml | 6 + 25 files changed, 568 insertions(+) create mode 100644 raml/modularization/api-before-modularization.raml create mode 100644 raml/modularization/api-with-libraries.raml create mode 100644 raml/modularization/api-with-typed-fragments.raml create mode 100644 raml/modularization/api.raml create mode 100644 raml/modularization/examples/Bar.json create mode 100644 raml/modularization/examples/Bars.json create mode 100644 raml/modularization/examples/Error.json create mode 100644 raml/modularization/examples/Foo.json create mode 100644 raml/modularization/examples/Foos.json create mode 100644 raml/modularization/extensions/en_US/additionalResources.raml create mode 100644 raml/modularization/libraries/dataTypes.raml create mode 100644 raml/modularization/libraries/resourceTypes.raml create mode 100644 raml/modularization/libraries/securitySchemes.raml create mode 100644 raml/modularization/libraries/traits.raml create mode 100644 raml/modularization/overlays/es_ES/additionalResources.raml create mode 100644 raml/modularization/overlays/es_ES/documentationItems.raml create mode 100644 raml/modularization/resourceTypes/collection.raml create mode 100644 raml/modularization/resourceTypes/item.raml create mode 100644 raml/modularization/traits/hasNotFound.raml create mode 100644 raml/modularization/traits/hasRequestItem.raml create mode 100644 raml/modularization/traits/hasResponseCollection.raml create mode 100644 raml/modularization/traits/hasResponseItem.raml create mode 100644 raml/modularization/types/Bar.raml create mode 100644 raml/modularization/types/Error.raml create mode 100644 raml/modularization/types/Foo.raml diff --git a/raml/modularization/api-before-modularization.raml b/raml/modularization/api-before-modularization.raml new file mode 100644 index 0000000000..b580c33983 --- /dev/null +++ b/raml/modularization/api-before-modularization.raml @@ -0,0 +1,119 @@ +#%RAML 1.0 +title: API for REST Services used in the RAML tutorials on Baeldung.com +documentation: + - title: Overview + - content: | + This document defines the interface for the REST services + used in the popular RAML Tutorial series at Baeldung.com. + - title: Disclaimer: + - content: | + All names used in this definition are purely fictional. + Any similarities between the names used in this tutorial and those of real persons, whether living or dead, are merely coincidental. + - title: Copyright + - content: Copyright 2016 by Baeldung.com. All rights reserved. +version: v1 +protocols: [ HTTPS ] +baseUri: http://rest-api.baeldung.com/api/{version} +mediaType: application/json +securedBy: basicAuth +securitySchemes: + - basicAuth: + description: Each request must contain the headers necessary for + basic authentication + type: Basic Authentication + describedBy: + headers: + Authorization: + description: | + Used to send the Base64 encoded "username:password" + credentials + type: string + responses: + 401: + description: | + Unauthorized. Either the provided username and password + combination is invalid, or the user is not allowed to + access the content provided by the requested URL. +types: + Foo: !include types/Foo.raml + Bar: !include types/Bar.raml + Error: !include types/Error.raml +resourceTypes: + - collection: + usage: Use this resourceType to represent a collection of items + description: A collection of <> + get: + description: | + Get all <>, + optionally filtered + is: [ hasResponseCollection ] + post: + description: | + Create a new <> + is: [ hasRequestItem ] + - item: + usage: Use this resourceType to represent any single item + description: A single <> + get: + description: Get a <> by <> + is: [ hasResponseItem, hasNotFound ] + put: + description: Update a <> by <> + is: [ hasRequestItem, hasResponseItem, hasNotFound ] + delete: + description: Delete a <> by <> + is: [ hasNotFound ] + responses: + 204: +traits: + - hasRequestItem: + body: + application/json: + type: <> + - hasResponseItem: + responses: + 200: + body: + application/json: + type: <> + example: !include examples/<>.json + - hasResponseCollection: + responses: + 200: + body: + application/json: + type: <>[] + example: !include examples/<>.json + - hasNotFound: + responses: + 404: + body: + application/json: + type: Error + example: !include examples/Error.json +/foos: + type: collection + typeName: Foo + get: + queryParameters: + name?: string + ownerName?: string + /{fooId}: + type: item + typeName: Foo + /name/{name}: + get: + description: List all foos with a certain name + typeName: Foo + is: [ hasResponseCollection ] +/bars: + type: collection + typeName: Bar + /{barId}: + type: item + typeName: Bar + /fooId/{fooId}: + get: + description: Get all bars for the matching fooId + typeName: Bar + is: [ hasResponseCollection ] \ No newline at end of file diff --git a/raml/modularization/api-with-libraries.raml b/raml/modularization/api-with-libraries.raml new file mode 100644 index 0000000000..b3081e843a --- /dev/null +++ b/raml/modularization/api-with-libraries.raml @@ -0,0 +1,50 @@ +#%RAML 1.0 +title: API for REST Services used in the RAML tutorials on Baeldung.com +documentation: + - title: Overview + - content: | + This document defines the interface for the REST services + used in the popular RAML Tutorial series at Baeldung.com. + - title: Disclaimer: + - content: | + All names used in this definition are purely fictional. + Any similarities between the names used in this tutorial and those of real persons, whether living or dead, are merely coincidental. + - title: Copyright + - content: Copyright 2016 by Baeldung.com. All rights reserved. +uses: + mySecuritySchemes: !include libraries/security.raml + myDataTypes: !include libraries/dataTypes.raml + myResourceTypes: !include libraries/resourceTypes.raml + myTraits: !include libraries/traits.raml +version: v1 +protocols: [ HTTPS ] +baseUri: http://rest-api.baeldung.com/api/{version} +mediaType: application/json +securedBy: [ mySecuritySchemes.basicAuth ] +/foos: + type: myResourceTypes.collection + typeName: myDataTypes.Foo + get: + queryParameters: + name?: string + ownerName?: string + /{fooId}: + type: myResourceTypes.item + typeName: myDataTypes.Foo + /name/{name}: + get: + description: List all foos with a certain name + typeName: myDataTypes.Foo + is: [ myTraits.hasResponseCollection ] +/bars: + type: myResourceTypes.collection + typeName: myDataTypes.Bar + /{barId}: + type: myResourceTypes.item + typeName: myDataTypes.Bar + /fooId/{fooId}: + get: + description: Get all bars for the matching fooId + type: myResourceTypes.item + typeName: myDataTypes.Bar + is: [ myTraits.hasResponseCollection ] \ No newline at end of file diff --git a/raml/modularization/api-with-typed-fragments.raml b/raml/modularization/api-with-typed-fragments.raml new file mode 100644 index 0000000000..2bb4e317c1 --- /dev/null +++ b/raml/modularization/api-with-typed-fragments.raml @@ -0,0 +1,74 @@ +#%RAML 1.0 +title: API for REST Services used in the RAML tutorials on Baeldung.com +documentation: + - title: Overview + - content: | + This document defines the interface for the REST services + used in the popular RAML Tutorial series at Baeldung.com. + - title: Disclaimer: + - content: | + All names used in this definition are purely fictional. + Any similarities between the names used in this tutorial and those of real persons, whether living or dead, are merely coincidental. + - title: Copyright + - content: Copyright 2016 by Baeldung.com. All rights reserved. +version: v1 +protocols: [ HTTPS ] +baseUri: http://rest-api.baeldung.com/api/{version} +mediaType: application/json +securedBy: [ basicAuth ] +securitySchemes: + - basicAuth: + description: Each request must contain the headers necessary for + basic authentication + type: Basic Authentication + describedBy: + headers: + Authorization: + description: | + Used to send the Base64 encoded "username:password" + credentials + type: string + responses: + 401: + description: | + Unauthorized. Either the provided username and password + combination is invalid, or the user is not allowed to + access the content provided by the requested URL. +types: + Foo: !include types/Foo.raml + Bar: !include types/Bar.raml + Error: !include types/Error.raml +resourceTypes: + - collection: !include resourceTypes/collection.raml + - item: !include resourceTypes/item.raml +traits: + - hasRequestItem: !include traits/hasRequestItem.raml + - hasResponseItem: !include traits/hasResponseItem.raml + - hasResponseCollection: !include traits/hasResponseCollection.raml + - hasNotFound: !include traits/hasNotFound.raml +/foos: + type: collection + typeName: Foo + get: + queryParameters: + name?: string + ownerName?: string + /{fooId}: + type: item + typeName: Foo + /name/{name}: + get: + description: List all foos with a certain name + typeName: Foo + is: [ hasResponseCollection ] +/bars: + type: collection + typeName: Bar + /{barId}: + type: item + typeName: Bar + /fooId/{fooId}: + get: + description: Get all bars for the matching fooId + typeName: Bar + is: [ hasResponseCollection ] \ No newline at end of file diff --git a/raml/modularization/api.raml b/raml/modularization/api.raml new file mode 100644 index 0000000000..184027cd26 --- /dev/null +++ b/raml/modularization/api.raml @@ -0,0 +1,47 @@ +#%RAML 1.0 +title: Baeldung Foo REST Services API +uses: + security: !include libraries/security.raml +version: v1 +protocols: [ HTTPS ] +baseUri: http://rest-api.baeldung.com/api/{version} +mediaType: application/json +securedBy: [ security.basicAuth ] +types: + Foo: !include types/Foo.raml + Bar: !include types/Bar.raml + Error: !include types/Error.raml +resourceTypes: + - collection: !include resourceTypes/collection.raml + - item: !include resourceTypes/item.raml +traits: + - hasRequestItem: !include traits/hasRequestItem.raml + - hasResponseItem: !include traits/hasResponseItem.raml + - hasResponseCollection: !include traits/hasResponseCollection.raml + - hasNotFound: !include traits/hasNotFound.raml +/foos: + type: collection + typeName: Foo + get: + queryParameters: + name?: string + ownerName?: string + /{fooId}: + type: item + typeName: Foo + /name/{name}: + get: + description: List all foos with a certain name + typeName: Foo + is: [ hasResponseCollection ] +/bars: + type: collection + typeName: Bar + /{barId}: + type: item + typeName: Bar + /fooId/{fooId}: + get: + description: Get all bars for the matching fooId + typeName: Bar + is: [ hasResponseCollection ] \ No newline at end of file diff --git a/raml/modularization/examples/Bar.json b/raml/modularization/examples/Bar.json new file mode 100644 index 0000000000..0ee1b34edb --- /dev/null +++ b/raml/modularization/examples/Bar.json @@ -0,0 +1,6 @@ +{ + "id" : 1, + "name" : "First Bar", + "city" : "Austin", + "fooId" : 2 +} \ No newline at end of file diff --git a/raml/modularization/examples/Bars.json b/raml/modularization/examples/Bars.json new file mode 100644 index 0000000000..89ea875432 --- /dev/null +++ b/raml/modularization/examples/Bars.json @@ -0,0 +1,19 @@ +[ + { + "id" : 1, + "name" : "First Bar", + "city" : "Austin", + "fooId" : 2 + }, + { + "id" : 2, + "name" : "Second Bar", + "city" : "Dallas", + "fooId" : 1 + }, + { + "id" : 3, + "name" : "Third Bar", + "fooId" : 2 + } +] \ No newline at end of file diff --git a/raml/modularization/examples/Error.json b/raml/modularization/examples/Error.json new file mode 100644 index 0000000000..dca56da7c2 --- /dev/null +++ b/raml/modularization/examples/Error.json @@ -0,0 +1,4 @@ +{ + "message" : "Not found", + "code" : 1001 +} \ No newline at end of file diff --git a/raml/modularization/examples/Foo.json b/raml/modularization/examples/Foo.json new file mode 100644 index 0000000000..1b1b8c891e --- /dev/null +++ b/raml/modularization/examples/Foo.json @@ -0,0 +1,4 @@ +{ + "id" : 1, + "name" : "First Foo" +} \ No newline at end of file diff --git a/raml/modularization/examples/Foos.json b/raml/modularization/examples/Foos.json new file mode 100644 index 0000000000..74f64689f0 --- /dev/null +++ b/raml/modularization/examples/Foos.json @@ -0,0 +1,16 @@ +[ + { + "id" : 1, + "name" : "First Foo", + "ownerName" : "Jack Robinson" + }, + { + "id" : 2, + "name" : "Second Foo" + }, + { + "id" : 3, + "name" : "Third Foo", + "ownerName" : "Chuck Norris" + } +] \ No newline at end of file diff --git a/raml/modularization/extensions/en_US/additionalResources.raml b/raml/modularization/extensions/en_US/additionalResources.raml new file mode 100644 index 0000000000..20c6851f23 --- /dev/null +++ b/raml/modularization/extensions/en_US/additionalResources.raml @@ -0,0 +1,16 @@ +#%RAML 1.0 Extension +# File located at: +# /extensions/en_US/additionalResources.raml +masterRef: /api.raml +usage: This extension defines additional resources for version 2 of the API. +version: v2 +/foos: + /bar/{barId}: + get: + description: | + Get the foo that is related to the bar having barId = {barId} + typeName: Foo + queryParameters: + barId?: integer + typeName: Foo + is: [ hasResponseItem ] diff --git a/raml/modularization/libraries/dataTypes.raml b/raml/modularization/libraries/dataTypes.raml new file mode 100644 index 0000000000..8a240e62dc --- /dev/null +++ b/raml/modularization/libraries/dataTypes.raml @@ -0,0 +1,19 @@ +#%RAML 1.0 Library +# This is the file /libraries/dataTypes.raml +usage: This library defines the data types for the API +types: + Foo: + properties: + id: integer + name: string + ownerName?: string + Bar: + properties: + id: integer + name: string + city?: string + fooId: integer + Error: + properties: + code: integer + message: string diff --git a/raml/modularization/libraries/resourceTypes.raml b/raml/modularization/libraries/resourceTypes.raml new file mode 100644 index 0000000000..681ff710d6 --- /dev/null +++ b/raml/modularization/libraries/resourceTypes.raml @@ -0,0 +1,32 @@ +#%RAML 1.0 Library +# This is the file /libraries/resourceTypes.raml +usage: This library defines the resource types for the API +uses: + myTraits: !include traits.raml +resourceTypes: + collection: + usage: Use this resourceType to represent a collection of items + description: A collection of <> + get: + description: | + Get all <>, + optionally filtered + is: [ myTraits.hasResponseCollection ] + post: + description: | + Create a new <> + is: [ myTraits.hasRequestItem ] + item: + usage: Use this resourceType to represent any single item + description: A single <> + get: + description: Get a <> by <> + is: [ myTraits.hasResponseItem, myTraits.hasNotFound ] + put: + description: Update a <> by <> + is: [ myTraits.hasRequestItem, myTraits.hasResponseItem, myTraits.hasNotFound ] + delete: + description: Delete a <> by <> + is: [ myTraits.hasNotFound ] + responses: + 204: diff --git a/raml/modularization/libraries/securitySchemes.raml b/raml/modularization/libraries/securitySchemes.raml new file mode 100644 index 0000000000..621c6ac975 --- /dev/null +++ b/raml/modularization/libraries/securitySchemes.raml @@ -0,0 +1,20 @@ +#%RAML 1.0 Library +# This is the file /libraries/securitySchemes.raml +securitySchemes: + - basicAuth: + description: Each request must contain the headers necessary for + basic authentication + type: Basic Authentication + describedBy: + headers: + Authorization: + description: | + Used to send the Base64 encoded "username:password" + credentials + type: string + responses: + 401: + description: | + Unauthorized. Either the provided username and password + combination is invalid, or the user is not allowed to + access the content provided by the requested URL. diff --git a/raml/modularization/libraries/traits.raml b/raml/modularization/libraries/traits.raml new file mode 100644 index 0000000000..c101d94c02 --- /dev/null +++ b/raml/modularization/libraries/traits.raml @@ -0,0 +1,33 @@ +#%RAML 1.0 Library +# This is the file /libraries/traits.raml +usage: This library defines some basic traits +traits: + hasRequestItem: + usage: Use this trait for resources whose request body is a single item + body: + application/json: + type: <> + hasResponseItem: + usage: Use this trait for resources whose response body is a single item + responses: + 200: + body: + application/json: + type: <> + example: !include /examples/<>.json + hasResponseCollection: + usage: Use this trait for resources whose response body is a collection of items + responses: + 200: + body: + application/json: + type: <>[] + example: !include /examples/<>.json + hasNotFound: + usage: Use this trait for resources that could respond with a 404 status + responses: + 404: + body: + application/json: + type: Error + example: !include /examples/Error.json diff --git a/raml/modularization/overlays/es_ES/additionalResources.raml b/raml/modularization/overlays/es_ES/additionalResources.raml new file mode 100644 index 0000000000..e8748fd726 --- /dev/null +++ b/raml/modularization/overlays/es_ES/additionalResources.raml @@ -0,0 +1,13 @@ +#%RAML 1.0 Overlay +# Archivo situado en: +# /overlays/es_ES/additionalResources.raml +masterRef: /api.raml +usage: | + Se trata de un español demasiado que describe los recursos adicionales + para la versión 2 del API. +version: v2 +/foos: + /bar/{barId}: + get: + description: | + Obtener el foo que se relaciona con el bar tomando barId = {barId} diff --git a/raml/modularization/overlays/es_ES/documentationItems.raml b/raml/modularization/overlays/es_ES/documentationItems.raml new file mode 100644 index 0000000000..dc6ca3eaef --- /dev/null +++ b/raml/modularization/overlays/es_ES/documentationItems.raml @@ -0,0 +1,23 @@ +#%RAML 1.0 Overlay +# File located at (archivo situado en): +# /overlays/es_ES/documentationItems.raml +masterRef: /api.raml +usage: | + To provide user documentation and other descriptive text in Spanish + (Para proporcionar la documentación del usuario y otro texto descriptivo en español) +title: API para servicios REST utilizados en los tutoriales RAML en Baeldung.com +documentation: + - title: Descripción general + - content: | + Este documento define la interfaz para los servicios REST + utilizados en la popular serie de RAML Tutorial en Baeldung.com + - title: Renuncia + - content: | + Todos los nombres usados ​​en esta definición son pura ficción. + Cualquier similitud entre los nombres utilizados en este tutorial + y los de las personas reales, ya sea vivo o muerto, + no son más que coincidenta. + + - title: Derechos de autor + - content: | + Derechos de autor 2016 por Baeldung.com. Todos los derechos reservados. diff --git a/raml/modularization/resourceTypes/collection.raml b/raml/modularization/resourceTypes/collection.raml new file mode 100644 index 0000000000..0cab417f14 --- /dev/null +++ b/raml/modularization/resourceTypes/collection.raml @@ -0,0 +1,12 @@ +#%RAML 1.0 ResourceType +usage: Use this resourceType to represent a collection of items +description: A collection of <> +get: + description: | + Get all <>, + optionally filtered + is: [ hasResponseCollection ] +post: + description: | + Create a new <> + is: [ hasRequestItem ] diff --git a/raml/modularization/resourceTypes/item.raml b/raml/modularization/resourceTypes/item.raml new file mode 100644 index 0000000000..59f057ca98 --- /dev/null +++ b/raml/modularization/resourceTypes/item.raml @@ -0,0 +1,14 @@ +#%RAML 1.0 ResourceType +usage: Use this resourceType to represent any single item +description: A single <> +get: + description: Get a <> by <> + is: [ hasResponseItem, hasNotFound ] +put: + description: Update a <> by <> + is: [ hasRequestItem, hasResponseItem, hasNotFound ] +delete: + description: Delete a <> by <> + is: [ hasNotFound ] + responses: + 204: diff --git a/raml/modularization/traits/hasNotFound.raml b/raml/modularization/traits/hasNotFound.raml new file mode 100644 index 0000000000..8d2d940c03 --- /dev/null +++ b/raml/modularization/traits/hasNotFound.raml @@ -0,0 +1,8 @@ +#%RAML 1.0 Trait +usage: Use this trait for resources that could respond with a 404 status +responses: + 404: + body: + application/json: + type: Error + example: !include /examples/Error.json diff --git a/raml/modularization/traits/hasRequestItem.raml b/raml/modularization/traits/hasRequestItem.raml new file mode 100644 index 0000000000..06281781c0 --- /dev/null +++ b/raml/modularization/traits/hasRequestItem.raml @@ -0,0 +1,5 @@ +#%RAML 1.0 Trait +usage: Use this trait for resources whose request body is a single item +body: + application/json: + type: <> diff --git a/raml/modularization/traits/hasResponseCollection.raml b/raml/modularization/traits/hasResponseCollection.raml new file mode 100644 index 0000000000..47dc1c2d5f --- /dev/null +++ b/raml/modularization/traits/hasResponseCollection.raml @@ -0,0 +1,8 @@ +#%RAML 1.0 Trait +usage: Use this trait for resources whose response body is a collection of items +responses: + 200: + body: + application/json: + type: <>[] + example: !include /examples/<>.json diff --git a/raml/modularization/traits/hasResponseItem.raml b/raml/modularization/traits/hasResponseItem.raml new file mode 100644 index 0000000000..94d3ba0756 --- /dev/null +++ b/raml/modularization/traits/hasResponseItem.raml @@ -0,0 +1,8 @@ +#%RAML 1.0 Trait +usage: Use this trait for resources whose response body is a single item +responses: + 200: + body: + application/json: + type: <> + example: !include /examples/<>.json diff --git a/raml/modularization/types/Bar.raml b/raml/modularization/types/Bar.raml new file mode 100644 index 0000000000..92255a75fe --- /dev/null +++ b/raml/modularization/types/Bar.raml @@ -0,0 +1,7 @@ +#%RAML 1.0 DataType + +properties: + id: integer + name: string + city?: string + fooId: integer diff --git a/raml/modularization/types/Error.raml b/raml/modularization/types/Error.raml new file mode 100644 index 0000000000..8d54b5f181 --- /dev/null +++ b/raml/modularization/types/Error.raml @@ -0,0 +1,5 @@ +#%RAML 1.0 DataType + + properties: + code: integer + message: string diff --git a/raml/modularization/types/Foo.raml b/raml/modularization/types/Foo.raml new file mode 100644 index 0000000000..1702865e05 --- /dev/null +++ b/raml/modularization/types/Foo.raml @@ -0,0 +1,6 @@ +#%RAML 1.0 DataType + +properties: + id: integer + name: string + ownerName?: string From 80b3e457f2eb16c2082381cf198c0be7c3b096a3 Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sun, 7 Feb 2016 20:29:30 +0100 Subject: [PATCH 06/84] Minor Bugfix --- .../baeldung/client/ServicesInterface.java | 4 +- .../main/java/com/baeldung/model/Movie.java | 22 +-- .../com/baeldung/server/MovieCrudService.java | 12 +- .../src/main/webapp/WEB-INF/web.xml | 37 +---- .../java/baeldung/client/RestEasyClient.java | 7 + .../baeldung/server/RestEasyClientTest.java | 149 +++++++----------- 6 files changed, 81 insertions(+), 150 deletions(-) create mode 100644 RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java index 7efed546d8..749cabc757 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -14,7 +14,7 @@ public interface ServicesInterface { @GET @Path("/getinfo") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - Movie movieByImdbID(@QueryParam("imdbID") String imdbID); + Movie movieByImdbId(@QueryParam("imdbId") String imdbId); @GET @@ -37,7 +37,7 @@ public interface ServicesInterface { @DELETE @Path("/deletemovie") - Response deleteMovie(@QueryParam("imdbID") String imdbID); + Response deleteMovie(@QueryParam("imdbId") String imdbID); diff --git a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java index 052ba081c1..a2b2bd5250 100644 --- a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -13,7 +13,7 @@ import javax.xml.bind.annotation.XmlType; "country", "director", "genre", - "imdbID", + "imdbId", "imdbRating", "imdbVotes", "language", @@ -36,7 +36,7 @@ public class Movie { protected String country; protected String director; protected String genre; - protected String imdbID; + protected String imdbId; protected String imdbRating; protected String imdbVotes; protected String language; @@ -173,27 +173,27 @@ public class Movie { } /** - * Recupera il valore della propriet� imdbID. + * Recupera il valore della propriet� imdbId. * * @return * possible object is * {@link String } * */ - public String getImdbID() { - return imdbID; + public String getImdbId() { + return imdbId; } /** - * Imposta il valore della propriet� imdbID. + * Imposta il valore della propriet� imdbId. * * @param value * allowed object is * {@link String } * */ - public void setImdbID(String value) { - this.imdbID = value; + public void setImdbId(String value) { + this.imdbId = value; } /** @@ -540,7 +540,7 @@ public class Movie { ", country='" + country + '\'' + ", director='" + director + '\'' + ", genre='" + genre + '\'' + - ", imdbID='" + imdbID + '\'' + + ", imdbId='" + imdbId + '\'' + ", imdbRating='" + imdbRating + '\'' + ", imdbVotes='" + imdbVotes + '\'' + ", language='" + language + '\'' + @@ -564,14 +564,14 @@ public class Movie { Movie movie = (Movie) o; - if (imdbID != null ? !imdbID.equals(movie.imdbID) : movie.imdbID != null) return false; + if (imdbId != null ? !imdbId.equals(movie.imdbId) : movie.imdbId != null) return false; return title != null ? title.equals(movie.title) : movie.title == null; } @Override public int hashCode() { - int result = imdbID != null ? imdbID.hashCode() : 0; + int result = imdbId != null ? imdbId.hashCode() : 0; result = 31 * result + (title != null ? title.hashCode() : 0); return result; } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index 18366e2faa..29226aa0e0 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -22,7 +22,7 @@ public class MovieCrudService { @GET @Path("/getinfo") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){ + public Movie movieByImdbID(@QueryParam("imdbId") String imdbID){ System.out.println("*** Calling getinfo for a given ImdbID***"); @@ -40,12 +40,12 @@ public class MovieCrudService { System.out.println("*** Calling addMovie ***"); - if (null!=inventory.get(movie.getImdbID())){ + if (null!=inventory.get(movie.getImdbId())){ return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is Already in the database.").build(); } - inventory.put(movie.getImdbID(),movie); + inventory.put(movie.getImdbId(),movie); return Response.status(Response.Status.CREATED).build(); } @@ -58,11 +58,11 @@ public class MovieCrudService { System.out.println("*** Calling updateMovie ***"); - if (null==inventory.get(movie.getImdbID())){ + if (null==inventory.get(movie.getImdbId())){ return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is not in the database.\nUnable to Update").build(); } - inventory.put(movie.getImdbID(),movie); + inventory.put(movie.getImdbId(),movie); return Response.status(Response.Status.OK).build(); } @@ -70,7 +70,7 @@ public class MovieCrudService { @DELETE @Path("/deletemovie") - public Response deleteMovie(@QueryParam("imdbID") String imdbID){ + public Response deleteMovie(@QueryParam("imdbId") String imdbID){ System.out.println("*** Calling deleteMovie ***"); diff --git a/RestEasy Example/src/main/webapp/WEB-INF/web.xml b/RestEasy Example/src/main/webapp/WEB-INF/web.xml index ab3bc1aa83..f70fdf7975 100644 --- a/RestEasy Example/src/main/webapp/WEB-INF/web.xml +++ b/RestEasy Example/src/main/webapp/WEB-INF/web.xml @@ -5,47 +5,12 @@ RestEasy Example - - - org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap - - - - RestEasy Example - - - webAppRootKey - RestEasyExample - - - - + resteasy.servlet.mapping.prefix /rest - - resteasy-servlet - - org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher - - - javax.ws.rs.Application - com.baeldung.server.RestEasyServices - - - - - resteasy-servlet - /rest/* - - - - - index.html - - \ No newline at end of file diff --git a/RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java b/RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java new file mode 100644 index 0000000000..b474b3d4f8 --- /dev/null +++ b/RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java @@ -0,0 +1,7 @@ +package baeldung.client; + +/** + * Created by Admin on 29/01/2016. + */ +public class RestEasyClient { +} diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java index fb4205bcd7..b6a2e2a0c1 100644 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java @@ -1,7 +1,7 @@ package com.baeldung.server; -import com.baeldung.model.Movie; import com.baeldung.client.ServicesInterface; +import com.baeldung.model.Movie; import org.apache.commons.io.IOUtils; import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; @@ -9,7 +9,6 @@ import org.jboss.resteasy.client.jaxrs.ResteasyClient; import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import javax.naming.NamingException; @@ -23,22 +22,13 @@ import java.util.Locale; public class RestEasyClientTest { - Movie transformerMovie=null; Movie batmanMovie=null; ObjectMapper jsonMapper=null; - @BeforeClass - public static void loadMovieInventory(){ - - - - } - @Before public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { - jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); @@ -57,133 +47,102 @@ public class RestEasyClientTest { batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); } catch (Exception e) { - e.printStackTrace(); throw new RuntimeException("Test is going to die ...", e); } - } - @Test public void testListAllMovies() { - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); - Response moviesResponse = simple.addMovie(transformerMovie); - moviesResponse.close(); - moviesResponse = simple.addMovie(batmanMovie); - moviesResponse.close(); + Response moviesResponse = simple.addMovie(transformerMovie); + moviesResponse.close(); + moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); - final List movies = simple.listMovies(); - System.out.println(movies); - - } catch (Exception e) { - e.printStackTrace(); - } + List movies = simple.listMovies(); + System.out.println(movies); } - @Test - public void testMovieByImdbID() { + public void testMovieByImdbId() { String transformerImdbId="tt0418279"; - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); - Response moviesResponse = simple.addMovie(transformerMovie); - moviesResponse.close(); + Response moviesResponse = simple.addMovie(transformerMovie); + moviesResponse.close(); - final Movie movies = simple.movieByImdbID(transformerImdbId); - System.out.println(movies); - - } catch (Exception e) { - e.printStackTrace(); - } + Movie movies = simple.movieByImdbId(transformerImdbId); + System.out.println(movies); } @Test public void testAddMovie() { - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); - Response moviesResponse = simple.addMovie(batmanMovie); - moviesResponse.close(); - moviesResponse = simple.addMovie(transformerMovie); + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = simple.addMovie(transformerMovie); - if (moviesResponse.getStatus() != Response.Status.CREATED.getStatusCode()) { - //System.out.println(moviesResponse.readEntity(String.class)); - System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); - } - moviesResponse.close(); - - } catch (Exception e) { - e.printStackTrace(); + if (moviesResponse.getStatus() != Response.Status.CREATED.getStatusCode()) { + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); } + + moviesResponse.close(); + System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); } @Test - public void testDeleteMovie() { + public void testDeleteMovi1e() { - String transformerImdbId="tt0418279"; + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = simple.deleteMovie(batmanMovie.getImdbId()); - Response moviesResponse = simple.addMovie(batmanMovie); - moviesResponse.close(); - moviesResponse = simple.deleteMovie(transformerImdbId); - moviesResponse.close(); - - if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { - System.out.println(moviesResponse.readEntity(String.class)); - throw new RuntimeException("Failed : HTTP error code : " + moviesResponse.getStatus()); - } - - moviesResponse.close(); - - } catch (Exception e) { - e.printStackTrace(); + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + System.out.println(moviesResponse.readEntity(String.class)); + throw new RuntimeException("Failed : HTTP error code : " + moviesResponse.getStatus()); } + + moviesResponse.close(); + System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); } @Test public void testUpdateMovie() { - try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + batmanMovie.setImdbVotes("300,000"); + moviesResponse = simple.updateMovie(batmanMovie); - Response moviesResponse = simple.addMovie(batmanMovie); - moviesResponse.close(); - batmanMovie.setImdbVotes("300,000"); - moviesResponse = simple.updateMovie(batmanMovie); - - if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { - //System.out.println(moviesResponse.readEntity(String.class)); - System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); - } - - moviesResponse.close(); - - } catch (Exception e) { - e.printStackTrace(); + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); } + + moviesResponse.close(); + System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); } } \ No newline at end of file From a0c22cc22514d0190d14d51e6bcc06ad5eed8836 Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sat, 30 Jan 2016 17:48:56 +0100 Subject: [PATCH 07/84] RestEasy Tutorial, CRUD Services example --- RestEasy Example/pom.xml | 91 +++ .../src/main/java/com/baeldung/Movie.java | 535 ++++++++++++++++++ .../baeldung/client/ServicesInterface.java | 44 ++ .../server/service/MovieCrudService.java | 94 +++ .../server/service/RestEasyServices.java | 40 ++ .../src/main/resources/schema1.xsd | 29 + .../main/webapp/WEB-INF/classes/logback.xml | 3 + .../WEB-INF/jboss-deployment-structure.xml | 16 + .../src/main/webapp/WEB-INF/jboss-web.xml | 4 + .../src/main/webapp/WEB-INF/web.xml | 51 ++ .../com/baeldung/server/RestEasyClient.java | 50 ++ .../resources/server/movies/transformer.json | 22 + 12 files changed, 979 insertions(+) create mode 100644 RestEasy Example/pom.xml create mode 100644 RestEasy Example/src/main/java/com/baeldung/Movie.java create mode 100644 RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java create mode 100644 RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java create mode 100644 RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java create mode 100644 RestEasy Example/src/main/resources/schema1.xsd create mode 100644 RestEasy Example/src/main/webapp/WEB-INF/classes/logback.xml create mode 100644 RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml create mode 100644 RestEasy Example/src/main/webapp/WEB-INF/jboss-web.xml create mode 100644 RestEasy Example/src/main/webapp/WEB-INF/web.xml create mode 100644 RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java create mode 100644 RestEasy Example/src/test/resources/server/movies/transformer.json diff --git a/RestEasy Example/pom.xml b/RestEasy Example/pom.xml new file mode 100644 index 0000000000..b16c5e8267 --- /dev/null +++ b/RestEasy Example/pom.xml @@ -0,0 +1,91 @@ + + + 4.0.0 + + com.baeldung + resteasy-tutorial + 1.0 + war + + + + jboss + http://repository.jboss.org/nexus/content/groups/public/ + + + + + 3.0.14.Final + runtime + + + + RestEasyTutorial + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + + org.jboss.resteasy + jaxrs-api + 3.0.12.Final + ${resteasy.scope} + + + + org.jboss.resteasy + resteasy-servlet-initializer + ${resteasy.version} + ${resteasy.scope} + + + jboss-jaxrs-api_2.0_spec + org.jboss.spec.javax.ws.rs + + + + + + org.jboss.resteasy + resteasy-client + ${resteasy.version} + ${resteasy.scope} + + + + + javax.ws.rs + javax.ws.rs-api + 2.0.1 + + + + org.jboss.resteasy + resteasy-jackson-provider + ${resteasy.version} + ${resteasy.scope} + + + + org.jboss.resteasy + resteasy-jaxb-provider + ${resteasy.version} + ${resteasy.scope} + + + + + + \ No newline at end of file diff --git a/RestEasy Example/src/main/java/com/baeldung/Movie.java b/RestEasy Example/src/main/java/com/baeldung/Movie.java new file mode 100644 index 0000000000..c0041d2e95 --- /dev/null +++ b/RestEasy Example/src/main/java/com/baeldung/Movie.java @@ -0,0 +1,535 @@ + +package com.baeldung; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlType; + + +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "movie", propOrder = { + "actors", + "awards", + "country", + "director", + "genre", + "imdbID", + "imdbRating", + "imdbVotes", + "language", + "metascore", + "plot", + "poster", + "rated", + "released", + "response", + "runtime", + "title", + "type", + "writer", + "year" +}) +public class Movie { + + protected String actors; + protected String awards; + protected String country; + protected String director; + protected String genre; + protected String imdbID; + protected String imdbRating; + protected String imdbVotes; + protected String language; + protected String metascore; + protected String plot; + protected String poster; + protected String rated; + protected String released; + protected String response; + protected String runtime; + protected String title; + protected String type; + protected String writer; + protected String year; + + /** + * Recupera il valore della propriet� actors. + * + * @return + * possible object is + * {@link String } + * + */ + public String getActors() { + return actors; + } + + /** + * Imposta il valore della propriet� actors. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setActors(String value) { + this.actors = value; + } + + /** + * Recupera il valore della propriet� awards. + * + * @return + * possible object is + * {@link String } + * + */ + public String getAwards() { + return awards; + } + + /** + * Imposta il valore della propriet� awards. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setAwards(String value) { + this.awards = value; + } + + /** + * Recupera il valore della propriet� country. + * + * @return + * possible object is + * {@link String } + * + */ + public String getCountry() { + return country; + } + + /** + * Imposta il valore della propriet� country. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setCountry(String value) { + this.country = value; + } + + /** + * Recupera il valore della propriet� director. + * + * @return + * possible object is + * {@link String } + * + */ + public String getDirector() { + return director; + } + + /** + * Imposta il valore della propriet� director. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setDirector(String value) { + this.director = value; + } + + /** + * Recupera il valore della propriet� genre. + * + * @return + * possible object is + * {@link String } + * + */ + public String getGenre() { + return genre; + } + + /** + * Imposta il valore della propriet� genre. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setGenre(String value) { + this.genre = value; + } + + /** + * Recupera il valore della propriet� imdbID. + * + * @return + * possible object is + * {@link String } + * + */ + public String getImdbID() { + return imdbID; + } + + /** + * Imposta il valore della propriet� imdbID. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setImdbID(String value) { + this.imdbID = value; + } + + /** + * Recupera il valore della propriet� imdbRating. + * + * @return + * possible object is + * {@link String } + * + */ + public String getImdbRating() { + return imdbRating; + } + + /** + * Imposta il valore della propriet� imdbRating. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setImdbRating(String value) { + this.imdbRating = value; + } + + /** + * Recupera il valore della propriet� imdbVotes. + * + * @return + * possible object is + * {@link String } + * + */ + public String getImdbVotes() { + return imdbVotes; + } + + /** + * Imposta il valore della propriet� imdbVotes. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setImdbVotes(String value) { + this.imdbVotes = value; + } + + /** + * Recupera il valore della propriet� language. + * + * @return + * possible object is + * {@link String } + * + */ + public String getLanguage() { + return language; + } + + /** + * Imposta il valore della propriet� language. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setLanguage(String value) { + this.language = value; + } + + /** + * Recupera il valore della propriet� metascore. + * + * @return + * possible object is + * {@link String } + * + */ + public String getMetascore() { + return metascore; + } + + /** + * Imposta il valore della propriet� metascore. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setMetascore(String value) { + this.metascore = value; + } + + /** + * Recupera il valore della propriet� plot. + * + * @return + * possible object is + * {@link String } + * + */ + public String getPlot() { + return plot; + } + + /** + * Imposta il valore della propriet� plot. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setPlot(String value) { + this.plot = value; + } + + /** + * Recupera il valore della propriet� poster. + * + * @return + * possible object is + * {@link String } + * + */ + public String getPoster() { + return poster; + } + + /** + * Imposta il valore della propriet� poster. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setPoster(String value) { + this.poster = value; + } + + /** + * Recupera il valore della propriet� rated. + * + * @return + * possible object is + * {@link String } + * + */ + public String getRated() { + return rated; + } + + /** + * Imposta il valore della propriet� rated. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setRated(String value) { + this.rated = value; + } + + /** + * Recupera il valore della propriet� released. + * + * @return + * possible object is + * {@link String } + * + */ + public String getReleased() { + return released; + } + + /** + * Imposta il valore della propriet� released. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setReleased(String value) { + this.released = value; + } + + /** + * Recupera il valore della propriet� response. + * + * @return + * possible object is + * {@link String } + * + */ + public String getResponse() { + return response; + } + + /** + * Imposta il valore della propriet� response. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setResponse(String value) { + this.response = value; + } + + /** + * Recupera il valore della propriet� runtime. + * + * @return + * possible object is + * {@link String } + * + */ + public String getRuntime() { + return runtime; + } + + /** + * Imposta il valore della propriet� runtime. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setRuntime(String value) { + this.runtime = value; + } + + /** + * Recupera il valore della propriet� title. + * + * @return + * possible object is + * {@link String } + * + */ + public String getTitle() { + return title; + } + + /** + * Imposta il valore della propriet� title. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setTitle(String value) { + this.title = value; + } + + /** + * Recupera il valore della propriet� type. + * + * @return + * possible object is + * {@link String } + * + */ + public String getType() { + return type; + } + + /** + * Imposta il valore della propriet� type. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setType(String value) { + this.type = value; + } + + /** + * Recupera il valore della propriet� writer. + * + * @return + * possible object is + * {@link String } + * + */ + public String getWriter() { + return writer; + } + + /** + * Imposta il valore della propriet� writer. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setWriter(String value) { + this.writer = value; + } + + /** + * Recupera il valore della propriet� year. + * + * @return + * possible object is + * {@link String } + * + */ + public String getYear() { + return year; + } + + /** + * Imposta il valore della propriet� year. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setYear(String value) { + this.year = value; + } + +} diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java new file mode 100644 index 0000000000..53e88961be --- /dev/null +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -0,0 +1,44 @@ +package com.baeldung.client; + +import com.baeldung.Movie; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + + +public interface ServicesInterface { + + + @GET + @Path("/getinfo") + @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + Movie movieByImdbID(@QueryParam("imdbID") String imdbID); + + + @POST + @Path("/addmovie") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + Response addMovie(Movie movie); + + + @PUT + @Path("/updatemovie") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + Response updateMovie(Movie movie); + + + @DELETE + @Path("/deletemovie") + Response deleteMovie(@QueryParam("imdbID") String imdbID); + + + @GET + @Path("/listmovies") + @Produces({"application/json"}) + List listMovies(); + +} diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java new file mode 100644 index 0000000000..d1973e7037 --- /dev/null +++ b/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java @@ -0,0 +1,94 @@ +package com.baeldung.server.service; + +import com.baeldung.Movie; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.Provider; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + + +@Path("/movies") +public class MovieCrudService { + + + private Map inventory = new HashMap(); + + @GET + @Path("/getinfo") + @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){ + + System.out.println("*** Calling getinfo ***"); + + Movie movie=new Movie(); + movie.setImdbID(imdbID); + return movie; + } + + @POST + @Path("/addmovie") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + public Response addMovie(Movie movie){ + + System.out.println("*** Calling addMovie ***"); + + if (null!=inventory.get(movie.getImdbID())){ + return Response.status(Response.Status.NOT_MODIFIED) + .entity("Movie is Already in the database.").build(); + } + inventory.put(movie.getImdbID(),movie); + + return Response.status(Response.Status.CREATED).build(); + } + + + @PUT + @Path("/updatemovie") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + public Response updateMovie(Movie movie){ + + System.out.println("*** Calling updateMovie ***"); + + if (null!=inventory.get(movie.getImdbID())){ + return Response.status(Response.Status.NOT_MODIFIED) + .entity("Movie is not in the database.\nUnable to Update").build(); + } + inventory.put(movie.getImdbID(),movie); + return Response.status(Response.Status.OK).build(); + + } + + + @DELETE + @Path("/deletemovie") + public Response deleteMovie(@QueryParam("imdbID") String imdbID){ + + System.out.println("*** Calling deleteMovie ***"); + + if (null==inventory.get(imdbID)){ + return Response.status(Response.Status.NOT_FOUND) + .entity("Movie is not in the database.\nUnable to Delete").build(); + } + + inventory.remove(imdbID); + return Response.status(Response.Status.OK).build(); + } + + @GET + @Path("/listmovies") + @Produces({"application/json"}) + public List listMovies(){ + + return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new)); + + } + + + +} diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java b/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java new file mode 100644 index 0000000000..16b6200ad1 --- /dev/null +++ b/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java @@ -0,0 +1,40 @@ +package com.baeldung.server.service; + + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * Created by Admin on 29/01/2016. + */ + + + +@ApplicationPath("/rest") +public class RestEasyServices extends Application { + + private Set singletons = new HashSet(); + + public RestEasyServices() { + singletons.add(new MovieCrudService()); + } + + @Override + public Set getSingletons() { + return singletons; + } + + @Override + public Set> getClasses() { + return super.getClasses(); + } + + @Override + public Map getProperties() { + return super.getProperties(); + } +} diff --git a/RestEasy Example/src/main/resources/schema1.xsd b/RestEasy Example/src/main/resources/schema1.xsd new file mode 100644 index 0000000000..0d74b7c366 --- /dev/null +++ b/RestEasy Example/src/main/resources/schema1.xsd @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/RestEasy Example/src/main/webapp/WEB-INF/classes/logback.xml b/RestEasy Example/src/main/webapp/WEB-INF/classes/logback.xml new file mode 100644 index 0000000000..d94e9f71ab --- /dev/null +++ b/RestEasy Example/src/main/webapp/WEB-INF/classes/logback.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml b/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml new file mode 100644 index 0000000000..84d75934a7 --- /dev/null +++ b/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RestEasy Example/src/main/webapp/WEB-INF/jboss-web.xml b/RestEasy Example/src/main/webapp/WEB-INF/jboss-web.xml new file mode 100644 index 0000000000..694bb71332 --- /dev/null +++ b/RestEasy Example/src/main/webapp/WEB-INF/jboss-web.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/RestEasy Example/src/main/webapp/WEB-INF/web.xml b/RestEasy Example/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..c66d3b56ae --- /dev/null +++ b/RestEasy Example/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,51 @@ + + + + RestEasy Example + + + + org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap + + + + RestEasy Example + + + webAppRootKey + RestEasyExample + + + + + + resteasy.servlet.mapping.prefix + /rest + + + + + resteasy-servlet + + org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher + + + javax.ws.rs.Application + com.baeldung.server.service.RestEasyServices + + + + + resteasy-servlet + /rest/* + + + + + index.html + + + + \ No newline at end of file diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java new file mode 100644 index 0000000000..e711233979 --- /dev/null +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java @@ -0,0 +1,50 @@ +package com.baeldung.server; + +import com.baeldung.Movie; +import com.baeldung.client.ServicesInterface; +import org.jboss.resteasy.client.jaxrs.ResteasyClient; +import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; +import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; + +import java.util.List; + +public class RestEasyClient { + + public static void main(String[] args) { + + Movie st = new Movie(); + st.setImdbID("12345"); + + /* + * Alternatively you can use this simple String to send + * instead of using a Student instance + * + * String jsonString = "{\"id\":12,\"firstName\":\"Catain\",\"lastName\":\"Hook\",\"age\":10}"; + */ + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target("http://localhost:8080/RestEasyTutorial/rest/movies/listmovies"); + + ServicesInterface simple = target.proxy(ServicesInterface.class); + final List movies = simple.listMovies(); + + /* + if (response.getStatus() != 200) { + throw new RuntimeException("Failed : HTTP error code : " + + response.getStatus()); + } + + System.out.println("Server response : \n"); + System.out.println(response.readEntity(String.class)); + + response.close(); +*/ + } catch (Exception e) { + + e.printStackTrace(); + + } + } + +} \ No newline at end of file diff --git a/RestEasy Example/src/test/resources/server/movies/transformer.json b/RestEasy Example/src/test/resources/server/movies/transformer.json new file mode 100644 index 0000000000..2154868265 --- /dev/null +++ b/RestEasy Example/src/test/resources/server/movies/transformer.json @@ -0,0 +1,22 @@ +{ + "title": "Transformers", + "year": "2007", + "rated": "PG-13", + "released": "03 Jul 2007", + "runtime": "144 min", + "genre": "Action, Adventure, Sci-Fi", + "director": "Michael Bay", + "writer": "Roberto Orci (screenplay), Alex Kurtzman (screenplay), John Rogers (story), Roberto Orci (story), Alex Kurtzman (story)", + "actors": "Shia LaBeouf, Megan Fox, Josh Duhamel, Tyrese Gibson", + "plot": "A long time ago, far away on the planet of Cybertron, a war is being waged between the noble Autobots (led by the wise Optimus Prime) and the devious Decepticons (commanded by the dreaded Megatron) for control over the Allspark, a mystical talisman that would grant unlimited power to whoever possesses it. The Autobots managed to smuggle the Allspark off the planet, but Megatron blasts off in search of it. He eventually tracks it to the planet of Earth (circa 1850), but his reckless desire for power sends him right into the Arctic Ocean, and the sheer cold forces him into a paralyzed state. His body is later found by Captain Archibald Witwicky, but before going into a comatose state Megatron uses the last of his energy to engrave into the Captain's glasses a map showing the location of the Allspark, and to send a transmission to Cybertron. Megatron is then carried away aboard the Captain's ship. A century later, Captain Witwicky's grandson Sam Witwicky (nicknamed Spike by his friends) buys his first car. To his shock, he discovers it to be Bumblebee, an Autobot in disguise who is to protect Spike, who possesses the Captain's glasses and the map engraved on them. But Bumblebee is not the only Transformer to have arrived on Earth - in the desert of Qatar, the Decepticons Blackout and Scorponok attack a U.S. military base, causing the Pentagon to send their special Sector Seven agents to capture all \"specimens of this alien race.\" Spike and his girlfriend Mikaela find themselves in the midst of a grand battle between the Autobots and the Decepticons, stretching from Hoover Dam all the way to Los Angeles. Meanwhile, deep inside Hoover Dam, the cryogenically stored body of Megatron awakens.", + "language": "English, Spanish", + "country": "USA", + "awards": "Nominated for 3 Oscars. Another 18 wins & 40 nominations.", + "poster": "http://ia.media-imdb.com/images/M/MV5BMTQwNjU5MzUzNl5BMl5BanBnXkFtZTYwMzc1MTI3._V1_SX300.jpg", + "metascore": "61", + "imdbRating": "7.1", + "imdbVotes": "492,225", + "imdbID": "tt0418279", + "Type": "movie", + "response": "True" +} \ No newline at end of file From 72a74b8096cd535da6ff3c96d7cc05d305c5e1a9 Mon Sep 17 00:00:00 2001 From: Giuseppe Bueti Date: Sat, 30 Jan 2016 20:39:28 +0100 Subject: [PATCH 08/84] Added testCase for List All Movies and Add a Movie --- RestEasy Example/pom.xml | 29 +++++ .../baeldung/client/ServicesInterface.java | 6 +- .../java/com/baeldung/{ => model}/Movie.java | 45 +++++++- .../{service => }/MovieCrudService.java | 5 +- .../{service => }/RestEasyServices.java | 2 +- .../com/baeldung/server/RestEasyClient.java | 104 ++++++++++++++---- .../com/baeldung/server/movies/batman.json | 22 ++++ .../baeldung}/server/movies/transformer.json | 2 +- 8 files changed, 184 insertions(+), 31 deletions(-) rename RestEasy Example/src/main/java/com/baeldung/{ => model}/Movie.java (86%) rename RestEasy Example/src/main/java/com/baeldung/server/{service => }/MovieCrudService.java (96%) rename RestEasy Example/src/main/java/com/baeldung/server/{service => }/RestEasyServices.java (95%) create mode 100644 RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json rename RestEasy Example/src/test/resources/{ => com/baeldung}/server/movies/transformer.json (99%) diff --git a/RestEasy Example/pom.xml b/RestEasy Example/pom.xml index b16c5e8267..8dabfc863b 100644 --- a/RestEasy Example/pom.xml +++ b/RestEasy Example/pom.xml @@ -85,6 +85,35 @@ ${resteasy.scope} + + + + junit + junit + 4.4 + + + + commons-io + commons-io + 2.4 + + + + com.fasterxml.jackson.core + jackson-core + 2.7.0 + + + + com.fasterxml.jackson.core + jackson-annotations + 2.7.0 + + + + + diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java index 53e88961be..2585c32438 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -1,15 +1,13 @@ package com.baeldung.client; -import com.baeldung.Movie; +import com.baeldung.model.Movie; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; - +@Path("/movies") public interface ServicesInterface { diff --git a/RestEasy Example/src/main/java/com/baeldung/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java similarity index 86% rename from RestEasy Example/src/main/java/com/baeldung/Movie.java rename to RestEasy Example/src/main/java/com/baeldung/model/Movie.java index c0041d2e95..052ba081c1 100644 --- a/RestEasy Example/src/main/java/com/baeldung/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -1,5 +1,5 @@ -package com.baeldung; +package com.baeldung.model; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; @@ -532,4 +532,47 @@ public class Movie { this.year = value; } + @Override + public String toString() { + return "Movie{" + + "actors='" + actors + '\'' + + ", awards='" + awards + '\'' + + ", country='" + country + '\'' + + ", director='" + director + '\'' + + ", genre='" + genre + '\'' + + ", imdbID='" + imdbID + '\'' + + ", imdbRating='" + imdbRating + '\'' + + ", imdbVotes='" + imdbVotes + '\'' + + ", language='" + language + '\'' + + ", metascore='" + metascore + '\'' + + ", poster='" + poster + '\'' + + ", rated='" + rated + '\'' + + ", released='" + released + '\'' + + ", response='" + response + '\'' + + ", runtime='" + runtime + '\'' + + ", title='" + title + '\'' + + ", type='" + type + '\'' + + ", writer='" + writer + '\'' + + ", year='" + year + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Movie movie = (Movie) o; + + if (imdbID != null ? !imdbID.equals(movie.imdbID) : movie.imdbID != null) return false; + return title != null ? title.equals(movie.title) : movie.title == null; + + } + + @Override + public int hashCode() { + int result = imdbID != null ? imdbID.hashCode() : 0; + result = 31 * result + (title != null ? title.hashCode() : 0); + return result; + } } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java similarity index 96% rename from RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java rename to RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index d1973e7037..60e0121966 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -1,11 +1,10 @@ -package com.baeldung.server.service; +package com.baeldung.server; -import com.baeldung.Movie; +import com.baeldung.model.Movie; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import javax.ws.rs.ext.Provider; import java.util.ArrayList; import java.util.HashMap; import java.util.List; diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java b/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java similarity index 95% rename from RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java rename to RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java index 16b6200ad1..8c57d2c9b4 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java @@ -1,4 +1,4 @@ -package com.baeldung.server.service; +package com.baeldung.server; import javax.ws.rs.ApplicationPath; diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java index e711233979..c77f494862 100644 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java @@ -1,49 +1,111 @@ package com.baeldung.server; -import com.baeldung.Movie; +import com.baeldung.model.Movie; import com.baeldung.client.ServicesInterface; +import org.apache.commons.io.IOUtils; +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; import org.jboss.resteasy.client.jaxrs.ResteasyClient; import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import javax.naming.NamingException; +import javax.ws.rs.core.Link; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.List; +import java.util.Locale; public class RestEasyClient { - public static void main(String[] args) { - Movie st = new Movie(); - st.setImdbID("12345"); + Movie transformerMovie=null; + Movie batmanMovie=null; + ObjectMapper jsonMapper=null; - /* - * Alternatively you can use this simple String to send - * instead of using a Student instance - * - * String jsonString = "{\"id\":12,\"firstName\":\"Catain\",\"lastName\":\"Hook\",\"age\":10}"; - */ + @BeforeClass + public static void loadMovieInventory(){ + + + + } + + @Before + public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { + + + jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); + jsonMapper.setDateFormat(sdf); + + try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/transformer.json")) { + String transformerMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + transformerMovie = jsonMapper.readValue(transformerMovieAsString, Movie.class); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/batman.json")) { + String batmanMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + } + + @Test + public void testListAllMovies() { try { ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target("http://localhost:8080/RestEasyTutorial/rest/movies/listmovies"); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + final List movies = simple.listMovies(); + System.out.println(movies); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + + @Test + public void testAddMovie() { + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); ServicesInterface simple = target.proxy(ServicesInterface.class); - final List movies = simple.listMovies(); + final Response moviesResponse = simple.addMovie(batmanMovie); - /* - if (response.getStatus() != 200) { + if (moviesResponse.getStatus() != 201) { + System.out.println(moviesResponse.readEntity(String.class)); throw new RuntimeException("Failed : HTTP error code : " - + response.getStatus()); + + moviesResponse.getStatus()); } - System.out.println("Server response : \n"); - System.out.println(response.readEntity(String.class)); + moviesResponse.close(); - response.close(); -*/ } catch (Exception e) { - e.printStackTrace(); - } } diff --git a/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json b/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json new file mode 100644 index 0000000000..28061d5bf9 --- /dev/null +++ b/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json @@ -0,0 +1,22 @@ +{ + "title": "Batman", + "year": "1989", + "rated": "PG-13", + "released": "23 Jun 1989", + "runtime": "126 min", + "genre": "Action, Adventure", + "director": "Tim Burton", + "writer": "Bob Kane (Batman characters), Sam Hamm (story), Sam Hamm (screenplay), Warren Skaaren (screenplay)", + "actors": "Michael Keaton, Jack Nicholson, Kim Basinger, Robert Wuhl", + "plot": "The Dark Knight of Gotham City begins his war on crime with his first major enemy being the clownishly homicidal Joker.", + "language": "English, French", + "country": "USA, UK", + "awards": "Won 1 Oscar. Another 9 wins & 22 nominations.", + "poster": "http://ia.media-imdb.com/images/M/MV5BMTYwNjAyODIyMF5BMl5BanBnXkFtZTYwNDMwMDk2._V1_SX300.jpg", + "metascore": "66", + "imdbRating": "7.6", + "imdbVotes": "256,000", + "imdbID": "tt0096895", + "type": "movie", + "response": "True" +} \ No newline at end of file diff --git a/RestEasy Example/src/test/resources/server/movies/transformer.json b/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json similarity index 99% rename from RestEasy Example/src/test/resources/server/movies/transformer.json rename to RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json index 2154868265..a3b033a8ba 100644 --- a/RestEasy Example/src/test/resources/server/movies/transformer.json +++ b/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json @@ -17,6 +17,6 @@ "imdbRating": "7.1", "imdbVotes": "492,225", "imdbID": "tt0418279", - "Type": "movie", + "type": "movie", "response": "True" } \ No newline at end of file From 4174ff7dd2a1deb7e023067326e8fa2495385003 Mon Sep 17 00:00:00 2001 From: Giuseppe Bueti Date: Sun, 31 Jan 2016 11:05:11 +0100 Subject: [PATCH 09/84] Added testCase for all Services --- RestEasy Example/pom.xml | 15 -- .../baeldung/client/ServicesInterface.java | 10 +- .../com/baeldung/server/MovieCrudService.java | 15 +- .../src/main/webapp/WEB-INF/web.xml | 2 +- .../com/baeldung/server/RestEasyClient.java | 112 ----------- .../baeldung/server/RestEasyClientTest.java | 189 ++++++++++++++++++ 6 files changed, 206 insertions(+), 137 deletions(-) delete mode 100644 RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java create mode 100644 RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java diff --git a/RestEasy Example/pom.xml b/RestEasy Example/pom.xml index 8dabfc863b..6935238d91 100644 --- a/RestEasy Example/pom.xml +++ b/RestEasy Example/pom.xml @@ -99,21 +99,6 @@ 2.4 - - com.fasterxml.jackson.core - jackson-core - 2.7.0 - - - - com.fasterxml.jackson.core - jackson-annotations - 2.7.0 - - - - - diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java index 2585c32438..7efed546d8 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -17,6 +17,12 @@ public interface ServicesInterface { Movie movieByImdbID(@QueryParam("imdbID") String imdbID); + @GET + @Path("/listmovies") + @Produces({"application/json"}) + List listMovies(); + + @POST @Path("/addmovie") @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @@ -34,9 +40,5 @@ public interface ServicesInterface { Response deleteMovie(@QueryParam("imdbID") String imdbID); - @GET - @Path("/listmovies") - @Produces({"application/json"}) - List listMovies(); } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index 60e0121966..18366e2faa 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -18,18 +18,21 @@ public class MovieCrudService { private Map inventory = new HashMap(); + @GET @Path("/getinfo") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){ - System.out.println("*** Calling getinfo ***"); + System.out.println("*** Calling getinfo for a given ImdbID***"); + + if(inventory.containsKey(imdbID)){ + return inventory.get(imdbID); + }else return null; - Movie movie=new Movie(); - movie.setImdbID(imdbID); - return movie; } + @POST @Path("/addmovie") @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @@ -41,6 +44,7 @@ public class MovieCrudService { return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is Already in the database.").build(); } + inventory.put(movie.getImdbID(),movie); return Response.status(Response.Status.CREATED).build(); @@ -54,7 +58,7 @@ public class MovieCrudService { System.out.println("*** Calling updateMovie ***"); - if (null!=inventory.get(movie.getImdbID())){ + if (null==inventory.get(movie.getImdbID())){ return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is not in the database.\nUnable to Update").build(); } @@ -79,6 +83,7 @@ public class MovieCrudService { return Response.status(Response.Status.OK).build(); } + @GET @Path("/listmovies") @Produces({"application/json"}) diff --git a/RestEasy Example/src/main/webapp/WEB-INF/web.xml b/RestEasy Example/src/main/webapp/WEB-INF/web.xml index c66d3b56ae..ab3bc1aa83 100644 --- a/RestEasy Example/src/main/webapp/WEB-INF/web.xml +++ b/RestEasy Example/src/main/webapp/WEB-INF/web.xml @@ -33,7 +33,7 @@ javax.ws.rs.Application - com.baeldung.server.service.RestEasyServices + com.baeldung.server.RestEasyServices diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java deleted file mode 100644 index c77f494862..0000000000 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.baeldung.server; - -import com.baeldung.model.Movie; -import com.baeldung.client.ServicesInterface; -import org.apache.commons.io.IOUtils; -import org.codehaus.jackson.map.DeserializationConfig; -import org.codehaus.jackson.map.ObjectMapper; -import org.jboss.resteasy.client.jaxrs.ResteasyClient; -import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; -import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import javax.naming.NamingException; -import javax.ws.rs.core.Link; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriBuilder; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileReader; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.List; -import java.util.Locale; - -public class RestEasyClient { - - - Movie transformerMovie=null; - Movie batmanMovie=null; - ObjectMapper jsonMapper=null; - - @BeforeClass - public static void loadMovieInventory(){ - - - - } - - @Before - public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { - - - jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); - jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); - SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); - jsonMapper.setDateFormat(sdf); - - try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/transformer.json")) { - String transformerMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); - transformerMovie = jsonMapper.readValue(transformerMovieAsString, Movie.class); - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException("Test is going to die ...", e); - } - - try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/batman.json")) { - String batmanMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); - batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); - - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException("Test is going to die ...", e); - } - - } - - @Test - public void testListAllMovies() { - - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); - - final List movies = simple.listMovies(); - System.out.println(movies); - - } catch (Exception e) { - e.printStackTrace(); - } - } - - - - @Test - public void testAddMovie() { - - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - - ServicesInterface simple = target.proxy(ServicesInterface.class); - final Response moviesResponse = simple.addMovie(batmanMovie); - - if (moviesResponse.getStatus() != 201) { - System.out.println(moviesResponse.readEntity(String.class)); - throw new RuntimeException("Failed : HTTP error code : " - + moviesResponse.getStatus()); - } - - moviesResponse.close(); - - } catch (Exception e) { - e.printStackTrace(); - } - } - -} \ No newline at end of file diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java new file mode 100644 index 0000000000..fb4205bcd7 --- /dev/null +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java @@ -0,0 +1,189 @@ +package com.baeldung.server; + +import com.baeldung.model.Movie; +import com.baeldung.client.ServicesInterface; +import org.apache.commons.io.IOUtils; +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; +import org.jboss.resteasy.client.jaxrs.ResteasyClient; +import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; +import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import javax.naming.NamingException; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; +import java.util.List; +import java.util.Locale; + +public class RestEasyClientTest { + + + Movie transformerMovie=null; + Movie batmanMovie=null; + ObjectMapper jsonMapper=null; + + @BeforeClass + public static void loadMovieInventory(){ + + + + } + + @Before + public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { + + + jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); + jsonMapper.setDateFormat(sdf); + + try (InputStream inputStream = new RestEasyClientTest().getClass().getResourceAsStream("./movies/transformer.json")) { + String transformerMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + transformerMovie = jsonMapper.readValue(transformerMovieAsString, Movie.class); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + try (InputStream inputStream = new RestEasyClientTest().getClass().getResourceAsStream("./movies/batman.json")) { + String batmanMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + } + + + @Test + public void testListAllMovies() { + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(transformerMovie); + moviesResponse.close(); + moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + + final List movies = simple.listMovies(); + System.out.println(movies); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testMovieByImdbID() { + + String transformerImdbId="tt0418279"; + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(transformerMovie); + moviesResponse.close(); + + final Movie movies = simple.movieByImdbID(transformerImdbId); + System.out.println(movies); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testAddMovie() { + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = simple.addMovie(transformerMovie); + + if (moviesResponse.getStatus() != Response.Status.CREATED.getStatusCode()) { + //System.out.println(moviesResponse.readEntity(String.class)); + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); + } + moviesResponse.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testDeleteMovie() { + + String transformerImdbId="tt0418279"; + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = simple.deleteMovie(transformerImdbId); + moviesResponse.close(); + + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + System.out.println(moviesResponse.readEntity(String.class)); + throw new RuntimeException("Failed : HTTP error code : " + moviesResponse.getStatus()); + } + + moviesResponse.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testUpdateMovie() { + + try { + + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + batmanMovie.setImdbVotes("300,000"); + moviesResponse = simple.updateMovie(batmanMovie); + + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + //System.out.println(moviesResponse.readEntity(String.class)); + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); + } + + moviesResponse.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file From 85d299e739fa00317d687fdd2af2ffa43a9dfc1c Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sun, 7 Feb 2016 20:29:30 +0100 Subject: [PATCH 10/84] Minor Bugfix --- .../baeldung/client/ServicesInterface.java | 4 +- .../main/java/com/baeldung/model/Movie.java | 22 +-- .../com/baeldung/server/MovieCrudService.java | 12 +- .../src/main/webapp/WEB-INF/web.xml | 37 +---- .../java/baeldung/client/RestEasyClient.java | 7 + .../baeldung/server/RestEasyClientTest.java | 149 +++++++----------- 6 files changed, 81 insertions(+), 150 deletions(-) create mode 100644 RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java index 7efed546d8..749cabc757 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -14,7 +14,7 @@ public interface ServicesInterface { @GET @Path("/getinfo") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - Movie movieByImdbID(@QueryParam("imdbID") String imdbID); + Movie movieByImdbId(@QueryParam("imdbId") String imdbId); @GET @@ -37,7 +37,7 @@ public interface ServicesInterface { @DELETE @Path("/deletemovie") - Response deleteMovie(@QueryParam("imdbID") String imdbID); + Response deleteMovie(@QueryParam("imdbId") String imdbID); diff --git a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java index 052ba081c1..a2b2bd5250 100644 --- a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -13,7 +13,7 @@ import javax.xml.bind.annotation.XmlType; "country", "director", "genre", - "imdbID", + "imdbId", "imdbRating", "imdbVotes", "language", @@ -36,7 +36,7 @@ public class Movie { protected String country; protected String director; protected String genre; - protected String imdbID; + protected String imdbId; protected String imdbRating; protected String imdbVotes; protected String language; @@ -173,27 +173,27 @@ public class Movie { } /** - * Recupera il valore della propriet� imdbID. + * Recupera il valore della propriet� imdbId. * * @return * possible object is * {@link String } * */ - public String getImdbID() { - return imdbID; + public String getImdbId() { + return imdbId; } /** - * Imposta il valore della propriet� imdbID. + * Imposta il valore della propriet� imdbId. * * @param value * allowed object is * {@link String } * */ - public void setImdbID(String value) { - this.imdbID = value; + public void setImdbId(String value) { + this.imdbId = value; } /** @@ -540,7 +540,7 @@ public class Movie { ", country='" + country + '\'' + ", director='" + director + '\'' + ", genre='" + genre + '\'' + - ", imdbID='" + imdbID + '\'' + + ", imdbId='" + imdbId + '\'' + ", imdbRating='" + imdbRating + '\'' + ", imdbVotes='" + imdbVotes + '\'' + ", language='" + language + '\'' + @@ -564,14 +564,14 @@ public class Movie { Movie movie = (Movie) o; - if (imdbID != null ? !imdbID.equals(movie.imdbID) : movie.imdbID != null) return false; + if (imdbId != null ? !imdbId.equals(movie.imdbId) : movie.imdbId != null) return false; return title != null ? title.equals(movie.title) : movie.title == null; } @Override public int hashCode() { - int result = imdbID != null ? imdbID.hashCode() : 0; + int result = imdbId != null ? imdbId.hashCode() : 0; result = 31 * result + (title != null ? title.hashCode() : 0); return result; } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index 18366e2faa..29226aa0e0 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -22,7 +22,7 @@ public class MovieCrudService { @GET @Path("/getinfo") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){ + public Movie movieByImdbID(@QueryParam("imdbId") String imdbID){ System.out.println("*** Calling getinfo for a given ImdbID***"); @@ -40,12 +40,12 @@ public class MovieCrudService { System.out.println("*** Calling addMovie ***"); - if (null!=inventory.get(movie.getImdbID())){ + if (null!=inventory.get(movie.getImdbId())){ return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is Already in the database.").build(); } - inventory.put(movie.getImdbID(),movie); + inventory.put(movie.getImdbId(),movie); return Response.status(Response.Status.CREATED).build(); } @@ -58,11 +58,11 @@ public class MovieCrudService { System.out.println("*** Calling updateMovie ***"); - if (null==inventory.get(movie.getImdbID())){ + if (null==inventory.get(movie.getImdbId())){ return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is not in the database.\nUnable to Update").build(); } - inventory.put(movie.getImdbID(),movie); + inventory.put(movie.getImdbId(),movie); return Response.status(Response.Status.OK).build(); } @@ -70,7 +70,7 @@ public class MovieCrudService { @DELETE @Path("/deletemovie") - public Response deleteMovie(@QueryParam("imdbID") String imdbID){ + public Response deleteMovie(@QueryParam("imdbId") String imdbID){ System.out.println("*** Calling deleteMovie ***"); diff --git a/RestEasy Example/src/main/webapp/WEB-INF/web.xml b/RestEasy Example/src/main/webapp/WEB-INF/web.xml index ab3bc1aa83..f70fdf7975 100644 --- a/RestEasy Example/src/main/webapp/WEB-INF/web.xml +++ b/RestEasy Example/src/main/webapp/WEB-INF/web.xml @@ -5,47 +5,12 @@ RestEasy Example - - - org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap - - - - RestEasy Example - - - webAppRootKey - RestEasyExample - - - - + resteasy.servlet.mapping.prefix /rest - - resteasy-servlet - - org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher - - - javax.ws.rs.Application - com.baeldung.server.RestEasyServices - - - - - resteasy-servlet - /rest/* - - - - - index.html - - \ No newline at end of file diff --git a/RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java b/RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java new file mode 100644 index 0000000000..b474b3d4f8 --- /dev/null +++ b/RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java @@ -0,0 +1,7 @@ +package baeldung.client; + +/** + * Created by Admin on 29/01/2016. + */ +public class RestEasyClient { +} diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java index fb4205bcd7..b6a2e2a0c1 100644 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java @@ -1,7 +1,7 @@ package com.baeldung.server; -import com.baeldung.model.Movie; import com.baeldung.client.ServicesInterface; +import com.baeldung.model.Movie; import org.apache.commons.io.IOUtils; import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; @@ -9,7 +9,6 @@ import org.jboss.resteasy.client.jaxrs.ResteasyClient; import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import javax.naming.NamingException; @@ -23,22 +22,13 @@ import java.util.Locale; public class RestEasyClientTest { - Movie transformerMovie=null; Movie batmanMovie=null; ObjectMapper jsonMapper=null; - @BeforeClass - public static void loadMovieInventory(){ - - - - } - @Before public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { - jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); @@ -57,133 +47,102 @@ public class RestEasyClientTest { batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); } catch (Exception e) { - e.printStackTrace(); throw new RuntimeException("Test is going to die ...", e); } - } - @Test public void testListAllMovies() { - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); - Response moviesResponse = simple.addMovie(transformerMovie); - moviesResponse.close(); - moviesResponse = simple.addMovie(batmanMovie); - moviesResponse.close(); + Response moviesResponse = simple.addMovie(transformerMovie); + moviesResponse.close(); + moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); - final List movies = simple.listMovies(); - System.out.println(movies); - - } catch (Exception e) { - e.printStackTrace(); - } + List movies = simple.listMovies(); + System.out.println(movies); } - @Test - public void testMovieByImdbID() { + public void testMovieByImdbId() { String transformerImdbId="tt0418279"; - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); - Response moviesResponse = simple.addMovie(transformerMovie); - moviesResponse.close(); + Response moviesResponse = simple.addMovie(transformerMovie); + moviesResponse.close(); - final Movie movies = simple.movieByImdbID(transformerImdbId); - System.out.println(movies); - - } catch (Exception e) { - e.printStackTrace(); - } + Movie movies = simple.movieByImdbId(transformerImdbId); + System.out.println(movies); } @Test public void testAddMovie() { - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); - Response moviesResponse = simple.addMovie(batmanMovie); - moviesResponse.close(); - moviesResponse = simple.addMovie(transformerMovie); + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = simple.addMovie(transformerMovie); - if (moviesResponse.getStatus() != Response.Status.CREATED.getStatusCode()) { - //System.out.println(moviesResponse.readEntity(String.class)); - System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); - } - moviesResponse.close(); - - } catch (Exception e) { - e.printStackTrace(); + if (moviesResponse.getStatus() != Response.Status.CREATED.getStatusCode()) { + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); } + + moviesResponse.close(); + System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); } @Test - public void testDeleteMovie() { + public void testDeleteMovi1e() { - String transformerImdbId="tt0418279"; + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = simple.deleteMovie(batmanMovie.getImdbId()); - Response moviesResponse = simple.addMovie(batmanMovie); - moviesResponse.close(); - moviesResponse = simple.deleteMovie(transformerImdbId); - moviesResponse.close(); - - if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { - System.out.println(moviesResponse.readEntity(String.class)); - throw new RuntimeException("Failed : HTTP error code : " + moviesResponse.getStatus()); - } - - moviesResponse.close(); - - } catch (Exception e) { - e.printStackTrace(); + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + System.out.println(moviesResponse.readEntity(String.class)); + throw new RuntimeException("Failed : HTTP error code : " + moviesResponse.getStatus()); } + + moviesResponse.close(); + System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); } @Test public void testUpdateMovie() { - try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + batmanMovie.setImdbVotes("300,000"); + moviesResponse = simple.updateMovie(batmanMovie); - Response moviesResponse = simple.addMovie(batmanMovie); - moviesResponse.close(); - batmanMovie.setImdbVotes("300,000"); - moviesResponse = simple.updateMovie(batmanMovie); - - if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { - //System.out.println(moviesResponse.readEntity(String.class)); - System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); - } - - moviesResponse.close(); - - } catch (Exception e) { - e.printStackTrace(); + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); } + + moviesResponse.close(); + System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); } } \ No newline at end of file From d4a4d20fa03d9356ee122834846551cd71427c44 Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sun, 7 Feb 2016 20:39:35 +0100 Subject: [PATCH 11/84] Minor Bugfix --- .../main/java/com/baeldung/model/Movie.java | 321 +----------------- 1 file changed, 1 insertion(+), 320 deletions(-) diff --git a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java index a2b2bd5250..805ba95209 100644 --- a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -52,482 +52,163 @@ public class Movie { protected String writer; protected String year; - /** - * Recupera il valore della propriet� actors. - * - * @return - * possible object is - * {@link String } - * - */ + public String getActors() { return actors; } - /** - * Imposta il valore della propriet� actors. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setActors(String value) { this.actors = value; } - /** - * Recupera il valore della propriet� awards. - * - * @return - * possible object is - * {@link String } - * - */ public String getAwards() { return awards; } - /** - * Imposta il valore della propriet� awards. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setAwards(String value) { this.awards = value; } - /** - * Recupera il valore della propriet� country. - * - * @return - * possible object is - * {@link String } - * - */ public String getCountry() { return country; } - /** - * Imposta il valore della propriet� country. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setCountry(String value) { this.country = value; } - /** - * Recupera il valore della propriet� director. - * - * @return - * possible object is - * {@link String } - * - */ public String getDirector() { return director; } - /** - * Imposta il valore della propriet� director. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setDirector(String value) { this.director = value; } - /** - * Recupera il valore della propriet� genre. - * - * @return - * possible object is - * {@link String } - * - */ public String getGenre() { return genre; } - /** - * Imposta il valore della propriet� genre. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setGenre(String value) { this.genre = value; } - /** - * Recupera il valore della propriet� imdbId. - * - * @return - * possible object is - * {@link String } - * - */ public String getImdbId() { return imdbId; } - /** - * Imposta il valore della propriet� imdbId. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setImdbId(String value) { this.imdbId = value; } - /** - * Recupera il valore della propriet� imdbRating. - * - * @return - * possible object is - * {@link String } - * - */ public String getImdbRating() { return imdbRating; } - /** - * Imposta il valore della propriet� imdbRating. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setImdbRating(String value) { this.imdbRating = value; } - /** - * Recupera il valore della propriet� imdbVotes. - * - * @return - * possible object is - * {@link String } - * - */ public String getImdbVotes() { return imdbVotes; } - /** - * Imposta il valore della propriet� imdbVotes. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setImdbVotes(String value) { this.imdbVotes = value; } - /** - * Recupera il valore della propriet� language. - * - * @return - * possible object is - * {@link String } - * - */ public String getLanguage() { return language; } - /** - * Imposta il valore della propriet� language. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setLanguage(String value) { this.language = value; } - /** - * Recupera il valore della propriet� metascore. - * - * @return - * possible object is - * {@link String } - * - */ public String getMetascore() { return metascore; } - /** - * Imposta il valore della propriet� metascore. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setMetascore(String value) { this.metascore = value; } - /** - * Recupera il valore della propriet� plot. - * - * @return - * possible object is - * {@link String } - * - */ public String getPlot() { return plot; } - /** - * Imposta il valore della propriet� plot. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setPlot(String value) { this.plot = value; } - /** - * Recupera il valore della propriet� poster. - * - * @return - * possible object is - * {@link String } - * - */ public String getPoster() { return poster; } - /** - * Imposta il valore della propriet� poster. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setPoster(String value) { this.poster = value; } - /** - * Recupera il valore della propriet� rated. - * - * @return - * possible object is - * {@link String } - * - */ public String getRated() { return rated; } - /** - * Imposta il valore della propriet� rated. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setRated(String value) { this.rated = value; } - /** - * Recupera il valore della propriet� released. - * - * @return - * possible object is - * {@link String } - * - */ public String getReleased() { return released; } - /** - * Imposta il valore della propriet� released. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setReleased(String value) { this.released = value; } - /** - * Recupera il valore della propriet� response. - * - * @return - * possible object is - * {@link String } - * - */ public String getResponse() { return response; } - /** - * Imposta il valore della propriet� response. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setResponse(String value) { this.response = value; } - /** - * Recupera il valore della propriet� runtime. - * - * @return - * possible object is - * {@link String } - * - */ public String getRuntime() { return runtime; } - /** - * Imposta il valore della propriet� runtime. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setRuntime(String value) { this.runtime = value; } - /** - * Recupera il valore della propriet� title. - * - * @return - * possible object is - * {@link String } - * - */ public String getTitle() { return title; } - /** - * Imposta il valore della propriet� title. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setTitle(String value) { this.title = value; } - /** - * Recupera il valore della propriet� type. - * - * @return - * possible object is - * {@link String } - * - */ public String getType() { return type; } - /** - * Imposta il valore della propriet� type. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setType(String value) { this.type = value; } - /** - * Recupera il valore della propriet� writer. - * - * @return - * possible object is - * {@link String } - * - */ public String getWriter() { return writer; } - /** - * Imposta il valore della propriet� writer. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setWriter(String value) { this.writer = value; } - /** - * Recupera il valore della propriet� year. - * - * @return - * possible object is - * {@link String } - * - */ public String getYear() { return year; } - /** - * Imposta il valore della propriet� year. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setYear(String value) { this.year = value; } From 183ae3d3b9bae2ce9151c4ab0702b74ecb54d1dd Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sun, 7 Feb 2016 20:47:42 +0100 Subject: [PATCH 12/84] Minor Bugfix --- .../main/java/com/baeldung/model/Movie.java | 260 ------------------ 1 file changed, 260 deletions(-) diff --git a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java index 8eb8283d28..76e84a6378 100644 --- a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -84,390 +84,130 @@ public class Movie { this.director = value; } - public String getGenre() { return genre; } - public void setGenre(String value) { this.genre = value; } - /** - * Recupera il valore della propriet� imdbId. - * - * @return - * possible object is - * {@link String } - * - */ public String getImdbId() { return imdbId; } - /** - * Imposta il valore della propriet� imdbId. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setImdbId(String value) { this.imdbId = value; } - /** - * Recupera il valore della propriet� imdbRating. - * - * @return - * possible object is - * {@link String } - * - */ public String getImdbRating() { return imdbRating; } - /** - * Imposta il valore della propriet� imdbRating. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setImdbRating(String value) { this.imdbRating = value; } - /** - * Recupera il valore della propriet� imdbVotes. - * - * @return - * possible object is - * {@link String } - * - */ public String getImdbVotes() { return imdbVotes; } public void setImdbVotes(String value) { - /** - * Imposta il valore della propriet� imdbVotes. - * - * @param value - * allowed object is - * {@link String } - * - */ this.imdbVotes = value; } public String getLanguage() { - /** - * Recupera il valore della propriet� language. - * - * @return - * possible object is - * {@link String } - * - */ return language; } - /** - * Imposta il valore della propriet� language. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setLanguage(String value) { this.language = value; } - /** - * Recupera il valore della propriet� metascore. - * - * @return - * possible object is - * {@link String } - * - */ public String getMetascore() { return metascore; } - /** - * Imposta il valore della propriet� metascore. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setMetascore(String value) { this.metascore = value; } - /** - * Recupera il valore della propriet� plot. - * - * @return - * possible object is - * {@link String } - * - */ public String getPlot() { return plot; } - /** - * Imposta il valore della propriet� plot. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setPlot(String value) { this.plot = value; } - /** - * Recupera il valore della propriet� poster. - * - * @return - * possible object is - * {@link String } - * - */ public String getPoster() { return poster; } - /** - * Imposta il valore della propriet� poster. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setPoster(String value) { this.poster = value; } - /** - * Recupera il valore della propriet� rated. - * - * @return - * possible object is - * {@link String } - * - */ public String getRated() { return rated; } - /** - * Imposta il valore della propriet� rated. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setRated(String value) { this.rated = value; } - /** - * Recupera il valore della propriet� released. - * - * @return - * possible object is - * {@link String } - * - */ public String getReleased() { return released; } - /** - * Imposta il valore della propriet� released. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setReleased(String value) { this.released = value; } - /** - * Recupera il valore della propriet� response. - * - * @return - * possible object is - * {@link String } - * - */ public String getResponse() { return response; } - /** - * Imposta il valore della propriet� response. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setResponse(String value) { this.response = value; } - /** - * Recupera il valore della propriet� runtime. - * - * @return - * possible object is - * {@link String } - * - */ public String getRuntime() { return runtime; } - /** - * Imposta il valore della propriet� runtime. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setRuntime(String value) { this.runtime = value; } - /** - * Recupera il valore della propriet� title. - * - * @return - * possible object is - * {@link String } - * - */ public String getTitle() { return title; } - /** - * Imposta il valore della propriet� title. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setTitle(String value) { this.title = value; } -<<<<<<< HEAD -======= - /** - * Recupera il valore della propriet� type. - * - * @return - * possible object is - * {@link String } - * - */ ->>>>>>> origin/master public String getType() { return type; } -<<<<<<< HEAD -======= - /** - * Imposta il valore della propriet� type. - * - * @param value - * allowed object is - * {@link String } - * - */ ->>>>>>> origin/master public void setType(String value) { this.type = value; } -<<<<<<< HEAD -======= - /** - * Recupera il valore della propriet� writer. - * - * @return - * possible object is - * {@link String } - * - */ ->>>>>>> origin/master public String getWriter() { return writer; } -<<<<<<< HEAD -======= - /** - * Imposta il valore della propriet� writer. - * - * @param value - * allowed object is - * {@link String } - * - */ ->>>>>>> origin/master public void setWriter(String value) { this.writer = value; } -<<<<<<< HEAD -======= - /** - * Recupera il valore della propriet� year. - * - * @return - * possible object is - * {@link String } - * - */ ->>>>>>> origin/master public String getYear() { return year; } -<<<<<<< HEAD -======= - /** - * Imposta il valore della propriet� year. - * - * @param value - * allowed object is - * {@link String } - * - */ ->>>>>>> origin/master public void setYear(String value) { this.year = value; } From 03b6cbf3e545dcb817abf364f1edea3e2b074a03 Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sun, 7 Feb 2016 20:55:46 +0100 Subject: [PATCH 13/84] CleanUp Code --- RestEasy Example/pom.xml | 3 +- .../baeldung/client/ServicesInterface.java | 1 - .../main/java/com/baeldung/model/Movie.java | 2 -- .../com/baeldung/server/MovieCrudService.java | 17 ++++------- .../com/baeldung/server/RestEasyServices.java | 8 ----- .../src/main/resources/schema1.xsd | 29 ------------------- .../src/main/webapp/WEB-INF/web.xml | 3 -- .../java/baeldung/client/RestEasyClient.java | 7 ----- .../baeldung/server/RestEasyClientTest.java | 1 - 9 files changed, 7 insertions(+), 64 deletions(-) delete mode 100644 RestEasy Example/src/main/resources/schema1.xsd delete mode 100644 RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java diff --git a/RestEasy Example/pom.xml b/RestEasy Example/pom.xml index 6935238d91..9c890da2b7 100644 --- a/RestEasy Example/pom.xml +++ b/RestEasy Example/pom.xml @@ -36,7 +36,7 @@ - + org.jboss.resteasy jaxrs-api @@ -101,5 +101,4 @@ - \ No newline at end of file diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java index 749cabc757..3c8d6abc00 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -1,7 +1,6 @@ package com.baeldung.client; import com.baeldung.model.Movie; - import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; diff --git a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java index 76e84a6378..7590f10487 100644 --- a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -1,11 +1,9 @@ - package com.baeldung.model; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlType; - @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "movie", propOrder = { "actors", diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index 29226aa0e0..bbb3b1e953 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -1,7 +1,6 @@ package com.baeldung.server; import com.baeldung.model.Movie; - import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -11,23 +10,21 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; - @Path("/movies") public class MovieCrudService { - private Map inventory = new HashMap(); @GET @Path("/getinfo") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Movie movieByImdbID(@QueryParam("imdbId") String imdbID){ + public Movie movieByImdbId(@QueryParam("imdbId") String imdbId){ System.out.println("*** Calling getinfo for a given ImdbID***"); - if(inventory.containsKey(imdbID)){ - return inventory.get(imdbID); + if(inventory.containsKey(imdbId)){ + return inventory.get(imdbId); }else return null; } @@ -70,16 +67,16 @@ public class MovieCrudService { @DELETE @Path("/deletemovie") - public Response deleteMovie(@QueryParam("imdbId") String imdbID){ + public Response deleteMovie(@QueryParam("imdbId") String imdbId){ System.out.println("*** Calling deleteMovie ***"); - if (null==inventory.get(imdbID)){ + if (null==inventory.get(imdbId)){ return Response.status(Response.Status.NOT_FOUND) .entity("Movie is not in the database.\nUnable to Delete").build(); } - inventory.remove(imdbID); + inventory.remove(imdbId); return Response.status(Response.Status.OK).build(); } @@ -93,6 +90,4 @@ public class MovieCrudService { } - - } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java b/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java index 8c57d2c9b4..7726e49f38 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java @@ -1,19 +1,11 @@ package com.baeldung.server; - import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; -import java.util.Arrays; import java.util.HashSet; import java.util.Map; import java.util.Set; -/** - * Created by Admin on 29/01/2016. - */ - - - @ApplicationPath("/rest") public class RestEasyServices extends Application { diff --git a/RestEasy Example/src/main/resources/schema1.xsd b/RestEasy Example/src/main/resources/schema1.xsd deleted file mode 100644 index 0d74b7c366..0000000000 --- a/RestEasy Example/src/main/resources/schema1.xsd +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/RestEasy Example/src/main/webapp/WEB-INF/web.xml b/RestEasy Example/src/main/webapp/WEB-INF/web.xml index f70fdf7975..1e7f64004d 100644 --- a/RestEasy Example/src/main/webapp/WEB-INF/web.xml +++ b/RestEasy Example/src/main/webapp/WEB-INF/web.xml @@ -5,12 +5,9 @@ RestEasy Example - resteasy.servlet.mapping.prefix /rest - - \ No newline at end of file diff --git a/RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java b/RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java deleted file mode 100644 index b474b3d4f8..0000000000 --- a/RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java +++ /dev/null @@ -1,7 +0,0 @@ -package baeldung.client; - -/** - * Created by Admin on 29/01/2016. - */ -public class RestEasyClient { -} diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java index b6a2e2a0c1..faa39e0199 100644 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java @@ -10,7 +10,6 @@ import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; import org.junit.Before; import org.junit.Test; - import javax.naming.NamingException; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; From c3ef41a10ddf15cc2e919272334fe666579863da Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Mon, 8 Feb 2016 14:00:11 +0100 Subject: [PATCH 14/84] CleanUp and reformatting Code; dependency fix in pom.xml --- RestEasy Example/pom.xml | 147 +++++++----------- .../baeldung/client/ServicesInterface.java | 15 +- .../main/java/com/baeldung/model/Movie.java | 56 ++----- .../com/baeldung/server/MovieCrudService.java | 49 +++--- .../WEB-INF/jboss-deployment-structure.xml | 22 +-- .../src/main/webapp/WEB-INF/web.xml | 14 +- .../baeldung/server/RestEasyClientTest.java | 23 ++- .../com/baeldung/server/movies/batman.json | 2 +- .../baeldung/server/movies/transformer.json | 2 +- 9 files changed, 125 insertions(+), 205 deletions(-) diff --git a/RestEasy Example/pom.xml b/RestEasy Example/pom.xml index 9c890da2b7..ec9e87b0d1 100644 --- a/RestEasy Example/pom.xml +++ b/RestEasy Example/pom.xml @@ -1,104 +1,77 @@ - - 4.0.0 + + 4.0.0 - com.baeldung - resteasy-tutorial - 1.0 - war + com.baeldung + resteasy-tutorial + 1.0 + war - - - jboss - http://repository.jboss.org/nexus/content/groups/public/ - - + + 3.0.14.Final + - - 3.0.14.Final - runtime - + + RestEasyTutorial + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + - - RestEasyTutorial - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.8 - 1.8 - - - - + - + - - org.jboss.resteasy - jaxrs-api - 3.0.12.Final - ${resteasy.scope} - - - - org.jboss.resteasy - resteasy-servlet-initializer - ${resteasy.version} - ${resteasy.scope} - - - jboss-jaxrs-api_2.0_spec - org.jboss.spec.javax.ws.rs - - - - - - org.jboss.resteasy - resteasy-client - ${resteasy.version} - ${resteasy.scope} - + + org.jboss.resteasy + resteasy-servlet-initializer + ${resteasy.version} + - - javax.ws.rs - javax.ws.rs-api - 2.0.1 - + + org.jboss.resteasy + resteasy-client + ${resteasy.version} + - - org.jboss.resteasy - resteasy-jackson-provider - ${resteasy.version} - ${resteasy.scope} - + - - org.jboss.resteasy - resteasy-jaxb-provider - ${resteasy.version} - ${resteasy.scope} - + + org.jboss.resteasy + resteasy-jaxb-provider + ${resteasy.version} + - + + org.jboss.resteasy + resteasy-jackson-provider + ${resteasy.version} + - - junit - junit - 4.4 - + - - commons-io - commons-io - 2.4 - + + junit + junit + 4.4 + + + + commons-io + commons-io + 2.4 + + + - \ No newline at end of file diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java index 3c8d6abc00..3d03c16faf 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -9,35 +9,28 @@ import java.util.List; @Path("/movies") public interface ServicesInterface { - @GET @Path("/getinfo") - @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) Movie movieByImdbId(@QueryParam("imdbId") String imdbId); - @GET @Path("/listmovies") - @Produces({"application/json"}) + @Produces({ "application/json" }) List listMovies(); - @POST @Path("/addmovie") - @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) Response addMovie(Movie movie); - @PUT @Path("/updatemovie") - @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) Response updateMovie(Movie movie); - @DELETE @Path("/deletemovie") Response deleteMovie(@QueryParam("imdbId") String imdbID); - - } diff --git a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java index 7590f10487..5aade4591a 100644 --- a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -5,28 +5,7 @@ import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) -@XmlType(name = "movie", propOrder = { - "actors", - "awards", - "country", - "director", - "genre", - "imdbId", - "imdbRating", - "imdbVotes", - "language", - "metascore", - "plot", - "poster", - "rated", - "released", - "response", - "runtime", - "title", - "type", - "writer", - "year" -}) +@XmlType(name = "movie", propOrder = { "actors", "awards", "country", "director", "genre", "imdbId", "imdbRating", "imdbVotes", "language", "metascore", "plot", "poster", "rated", "released", "response", "runtime", "title", "type", "writer", "year" }) public class Movie { protected String actors; @@ -212,37 +191,22 @@ public class Movie { @Override public String toString() { - return "Movie{" + - "actors='" + actors + '\'' + - ", awards='" + awards + '\'' + - ", country='" + country + '\'' + - ", director='" + director + '\'' + - ", genre='" + genre + '\'' + - ", imdbId='" + imdbId + '\'' + - ", imdbRating='" + imdbRating + '\'' + - ", imdbVotes='" + imdbVotes + '\'' + - ", language='" + language + '\'' + - ", metascore='" + metascore + '\'' + - ", poster='" + poster + '\'' + - ", rated='" + rated + '\'' + - ", released='" + released + '\'' + - ", response='" + response + '\'' + - ", runtime='" + runtime + '\'' + - ", title='" + title + '\'' + - ", type='" + type + '\'' + - ", writer='" + writer + '\'' + - ", year='" + year + '\'' + - '}'; + return "Movie{" + "actors='" + actors + '\'' + ", awards='" + awards + '\'' + ", country='" + country + '\'' + ", director='" + director + '\'' + ", genre='" + genre + '\'' + ", imdbId='" + imdbId + '\'' + ", imdbRating='" + imdbRating + '\'' + + ", imdbVotes='" + imdbVotes + '\'' + ", language='" + language + '\'' + ", metascore='" + metascore + '\'' + ", poster='" + poster + '\'' + ", rated='" + rated + '\'' + ", released='" + released + '\'' + ", response='" + response + '\'' + + ", runtime='" + runtime + '\'' + ", title='" + title + '\'' + ", type='" + type + '\'' + ", writer='" + writer + '\'' + ", year='" + year + '\'' + '}'; } @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; Movie movie = (Movie) o; - if (imdbId != null ? !imdbId.equals(movie.imdbId) : movie.imdbId != null) return false; + if (imdbId != null ? !imdbId.equals(movie.imdbId) : movie.imdbId != null) + return false; return title != null ? title.equals(movie.title) : movie.title == null; } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index bbb3b1e953..6a0699874a 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -13,78 +13,71 @@ import java.util.stream.Collectors; @Path("/movies") public class MovieCrudService { - private Map inventory = new HashMap(); - + private Map inventory = new HashMap(); @GET @Path("/getinfo") - @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Movie movieByImdbId(@QueryParam("imdbId") String imdbId){ + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + public Movie movieByImdbId(@QueryParam("imdbId") String imdbId) { System.out.println("*** Calling getinfo for a given ImdbID***"); - if(inventory.containsKey(imdbId)){ + if (inventory.containsKey(imdbId)) { return inventory.get(imdbId); - }else return null; + } else + return null; } - @POST @Path("/addmovie") - @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Response addMovie(Movie movie){ + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + public Response addMovie(Movie movie) { System.out.println("*** Calling addMovie ***"); - if (null!=inventory.get(movie.getImdbId())){ - return Response.status(Response.Status.NOT_MODIFIED) - .entity("Movie is Already in the database.").build(); + if (null != inventory.get(movie.getImdbId())) { + return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is Already in the database.").build(); } - inventory.put(movie.getImdbId(),movie); + inventory.put(movie.getImdbId(), movie); return Response.status(Response.Status.CREATED).build(); } - @PUT @Path("/updatemovie") - @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Response updateMovie(Movie movie){ + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + public Response updateMovie(Movie movie) { System.out.println("*** Calling updateMovie ***"); - if (null==inventory.get(movie.getImdbId())){ - return Response.status(Response.Status.NOT_MODIFIED) - .entity("Movie is not in the database.\nUnable to Update").build(); + if (null == inventory.get(movie.getImdbId())) { + return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is not in the database.\nUnable to Update").build(); } - inventory.put(movie.getImdbId(),movie); + inventory.put(movie.getImdbId(), movie); return Response.status(Response.Status.OK).build(); } - @DELETE @Path("/deletemovie") - public Response deleteMovie(@QueryParam("imdbId") String imdbId){ + public Response deleteMovie(@QueryParam("imdbId") String imdbId) { System.out.println("*** Calling deleteMovie ***"); - if (null==inventory.get(imdbId)){ - return Response.status(Response.Status.NOT_FOUND) - .entity("Movie is not in the database.\nUnable to Delete").build(); + if (null == inventory.get(imdbId)) { + return Response.status(Response.Status.NOT_FOUND).entity("Movie is not in the database.\nUnable to Delete").build(); } inventory.remove(imdbId); return Response.status(Response.Status.OK).build(); } - @GET @Path("/listmovies") - @Produces({"application/json"}) - public List listMovies(){ + @Produces({ "application/json" }) + public List listMovies() { return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new)); diff --git a/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml b/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml index 84d75934a7..7879603d19 100644 --- a/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml +++ b/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml @@ -1,16 +1,16 @@ - - - - + + + + - - - - - + + + + + - - + + \ No newline at end of file diff --git a/RestEasy Example/src/main/webapp/WEB-INF/web.xml b/RestEasy Example/src/main/webapp/WEB-INF/web.xml index 1e7f64004d..d5f00293f4 100644 --- a/RestEasy Example/src/main/webapp/WEB-INF/web.xml +++ b/RestEasy Example/src/main/webapp/WEB-INF/web.xml @@ -1,13 +1,13 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> - RestEasy Example + RestEasy Example - - resteasy.servlet.mapping.prefix - /rest - + + resteasy.servlet.mapping.prefix + /rest + \ No newline at end of file diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java index faa39e0199..3760ae2415 100644 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java @@ -21,14 +21,14 @@ import java.util.Locale; public class RestEasyClientTest { - Movie transformerMovie=null; - Movie batmanMovie=null; - ObjectMapper jsonMapper=null; + Movie transformerMovie = null; + Movie batmanMovie = null; + ObjectMapper jsonMapper = null; @Before public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { - jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + jsonMapper = new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); jsonMapper.setDateFormat(sdf); @@ -69,7 +69,7 @@ public class RestEasyClientTest { @Test public void testMovieByImdbId() { - String transformerImdbId="tt0418279"; + String transformerImdbId = "tt0418279"; ResteasyClient client = new ResteasyClientBuilder().build(); ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); @@ -82,7 +82,6 @@ public class RestEasyClientTest { System.out.println(movies); } - @Test public void testAddMovie() { @@ -99,10 +98,9 @@ public class RestEasyClientTest { } moviesResponse.close(); - System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); + System.out.println("Response Code: " + Response.Status.OK.getStatusCode()); } - @Test public void testDeleteMovi1e() { @@ -116,14 +114,13 @@ public class RestEasyClientTest { if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { System.out.println(moviesResponse.readEntity(String.class)); - throw new RuntimeException("Failed : HTTP error code : " + moviesResponse.getStatus()); + throw new RuntimeException("Failed : HTTP error code : " + moviesResponse.getStatus()); } moviesResponse.close(); - System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); + System.out.println("Response Code: " + Response.Status.OK.getStatusCode()); } - @Test public void testUpdateMovie() { @@ -137,11 +134,11 @@ public class RestEasyClientTest { moviesResponse = simple.updateMovie(batmanMovie); if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { - System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); } moviesResponse.close(); - System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); + System.out.println("Response Code: " + Response.Status.OK.getStatusCode()); } } \ No newline at end of file diff --git a/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json b/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json index 28061d5bf9..173901c948 100644 --- a/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json +++ b/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json @@ -16,7 +16,7 @@ "metascore": "66", "imdbRating": "7.6", "imdbVotes": "256,000", - "imdbID": "tt0096895", + "imdbId": "tt0096895", "type": "movie", "response": "True" } \ No newline at end of file diff --git a/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json b/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json index a3b033a8ba..a4fd061a82 100644 --- a/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json +++ b/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json @@ -16,7 +16,7 @@ "metascore": "61", "imdbRating": "7.1", "imdbVotes": "492,225", - "imdbID": "tt0418279", + "imdbId": "tt0418279", "type": "movie", "response": "True" } \ No newline at end of file From d362112a31f1c283556f94f2a470eed1053dbdd6 Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Mon, 8 Feb 2016 16:08:15 +0100 Subject: [PATCH 15/84] CleanUp and reformatting Code. --- .../src/main/java/com/baeldung/server/MovieCrudService.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index 6a0699874a..b7f3215f3f 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -26,7 +26,6 @@ public class MovieCrudService { return inventory.get(imdbId); } else return null; - } @POST @@ -41,7 +40,6 @@ public class MovieCrudService { } inventory.put(movie.getImdbId(), movie); - return Response.status(Response.Status.CREATED).build(); } @@ -55,9 +53,9 @@ public class MovieCrudService { if (null == inventory.get(movie.getImdbId())) { return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is not in the database.\nUnable to Update").build(); } + inventory.put(movie.getImdbId(), movie); return Response.status(Response.Status.OK).build(); - } @DELETE @@ -80,7 +78,6 @@ public class MovieCrudService { public List listMovies() { return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new)); - } } From 4d3ce9afe9cf7c73a2955c0852c3b335828f3c3a Mon Sep 17 00:00:00 2001 From: Oleksandr Borkovskyi Date: Tue, 9 Feb 2016 13:59:35 +0200 Subject: [PATCH 16/84] Updated test --- .../src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java b/guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java index 4569019e60..903381bd29 100644 --- a/guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java +++ b/guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java @@ -27,7 +27,7 @@ public class GuavaMiscUtilsTest { List stackTraceElements = Throwables.lazyStackTrace(e); - assertEquals(27, stackTraceElements.size()); + assertTrue(stackTraceElements.size() > 0); } @Test From 420cbb202b3d101b10ea1b4eb9f8784ede50f112 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Tue, 9 Feb 2016 14:27:46 +0200 Subject: [PATCH 17/84] Update README.md --- core-java/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java/README.md b/core-java/README.md index 9a7c0d3776..23fe12465f 100644 --- a/core-java/README.md +++ b/core-java/README.md @@ -12,3 +12,4 @@ - [Convert a Map to an Array, List or Set in Java](http://www.baeldung.com/convert-map-values-to-array-list-set) - [Java – Write to File](http://www.baeldung.com/java-write-to-file) - [Java Scanner](http://www.baeldung.com/java-scanner) +- [Java Timer](http://www.baeldung.com/java-timer-and-timertask) From 0757706c4718548c8031ef97be8c702196689977 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Tue, 9 Feb 2016 14:30:27 +0200 Subject: [PATCH 18/84] Update README.md --- jackson/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/jackson/README.md b/jackson/README.md index 046c6b6cfb..d49ad9088a 100644 --- a/jackson/README.md +++ b/jackson/README.md @@ -10,3 +10,4 @@ - [Jackson – Custom Deserializer](http://www.baeldung.com/jackson-deserialization) - [Jackson Exceptions – Problems and Solutions](http://www.baeldung.com/jackson-exception) - [Jackson Date](http://www.baeldung.com/jackson-serialize-dates) +- [Jackson – Bidirectional Relationships](http://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion) From 86157e6beeb25cd85221038fbeb7291b48127aef Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Tue, 9 Feb 2016 14:31:59 +0200 Subject: [PATCH 19/84] Update README.md --- spring-security-login-and-registration/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-security-login-and-registration/README.md b/spring-security-login-and-registration/README.md index a5a9701ff8..57e7fd28f0 100644 --- a/spring-security-login-and-registration/README.md +++ b/spring-security-login-and-registration/README.md @@ -7,7 +7,7 @@ - [Spring Security Registration Tutorial](http://www.baeldung.com/spring-security-registration) - [The Registration Process With Spring Security](http://www.baeldung.com/registration-with-spring-mvc-and-spring-security) - [Registration – Activate a New Account by Email](http://www.baeldung.com/registration-verify-user-by-email) - +- [Registration with Spring Security – Password Encoding](http://www.baeldung.com/spring-security-registration-password-encoding-bcrypt) ### Build the Project ``` From 58d169cd241c998618969c2ead01eac5b2262ee9 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Tue, 9 Feb 2016 14:33:43 +0200 Subject: [PATCH 20/84] Update README.md --- spring-security-login-and-registration/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-login-and-registration/README.md b/spring-security-login-and-registration/README.md index 57e7fd28f0..299d096c37 100644 --- a/spring-security-login-and-registration/README.md +++ b/spring-security-login-and-registration/README.md @@ -8,6 +8,7 @@ - [The Registration Process With Spring Security](http://www.baeldung.com/registration-with-spring-mvc-and-spring-security) - [Registration – Activate a New Account by Email](http://www.baeldung.com/registration-verify-user-by-email) - [Registration with Spring Security – Password Encoding](http://www.baeldung.com/spring-security-registration-password-encoding-bcrypt) +- [Spring Security – Roles and Privileges](http://www.baeldung.com/role-and-privilege-for-spring-security-registration) ### Build the Project ``` From d806fa3925b681e4e81a74de4a27dc093d267806 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Tue, 9 Feb 2016 14:35:18 +0200 Subject: [PATCH 21/84] Update README.md --- spring-security-rest-full/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-rest-full/README.md b/spring-security-rest-full/README.md index cffbcf40f0..64478589a3 100644 --- a/spring-security-rest-full/README.md +++ b/spring-security-rest-full/README.md @@ -13,6 +13,7 @@ - [Integration Testing with the Maven Cargo plugin](http://www.baeldung.com/2011/10/16/how-to-set-up-integration-testing-with-the-maven-cargo-plugin/) - [Introduction to Spring Data JPA](http://www.baeldung.com/2011/12/22/the-persistence-layer-with-spring-data-jpa/) - [Project Configuration with Spring](http://www.baeldung.com/2012/03/12/project-configuration-with-spring/) +- [REST Query Language with Spring and JPA Criteria](http://www.baeldung.com/rest-search-language-spring-jpa-criteria) ### Build the Project From ad0dd95004b88106744065cf68c28770433466bf Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Tue, 9 Feb 2016 14:38:17 +0200 Subject: [PATCH 22/84] Update README.md --- spring-security-rest-full/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-rest-full/README.md b/spring-security-rest-full/README.md index 64478589a3..c5f52f44ad 100644 --- a/spring-security-rest-full/README.md +++ b/spring-security-rest-full/README.md @@ -14,6 +14,7 @@ - [Introduction to Spring Data JPA](http://www.baeldung.com/2011/12/22/the-persistence-layer-with-spring-data-jpa/) - [Project Configuration with Spring](http://www.baeldung.com/2012/03/12/project-configuration-with-spring/) - [REST Query Language with Spring and JPA Criteria](http://www.baeldung.com/rest-search-language-spring-jpa-criteria) +- [REST Query Language with Spring Data JPA Specifications](http://www.baeldung.com/rest-api-search-language-spring-data-specifications) ### Build the Project From 00ecb8c947a542eba055e214e3cda4d17a43c903 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Tue, 9 Feb 2016 14:43:43 +0200 Subject: [PATCH 23/84] Update README.md --- spring-security-rest-full/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-rest-full/README.md b/spring-security-rest-full/README.md index c5f52f44ad..e2db0e0307 100644 --- a/spring-security-rest-full/README.md +++ b/spring-security-rest-full/README.md @@ -15,6 +15,7 @@ - [Project Configuration with Spring](http://www.baeldung.com/2012/03/12/project-configuration-with-spring/) - [REST Query Language with Spring and JPA Criteria](http://www.baeldung.com/rest-search-language-spring-jpa-criteria) - [REST Query Language with Spring Data JPA Specifications](http://www.baeldung.com/rest-api-search-language-spring-data-specifications) +- [REST Query Language with Spring Data JPA and QueryDSL](http://www.baeldung.com/rest-api-search-language-spring-data-querydsl) ### Build the Project From 2bad287a2eee1acd22deaf9b6ef5c00d08a6d149 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Tue, 9 Feb 2016 14:44:49 +0200 Subject: [PATCH 24/84] Update README.md --- spring-jpa/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-jpa/README.md b/spring-jpa/README.md index da55ca7b75..f974c6e22c 100644 --- a/spring-jpa/README.md +++ b/spring-jpa/README.md @@ -9,3 +9,4 @@ - [The DAO with JPA and Spring](http://www.baeldung.com/spring-dao-jpa) - [JPA Pagination](http://www.baeldung.com/jpa-pagination) - [Sorting with JPA](http://www.baeldung.com/jpa-sort) +- [Spring JPA – Multiple Databases](http://www.baeldung.com/spring-data-jpa-multiple-databases) From 5d896c28d386e6f86783dac649f0ba10fd1d54ff Mon Sep 17 00:00:00 2001 From: David Morley Date: Wed, 10 Feb 2016 05:14:57 -0600 Subject: [PATCH 25/84] Clean up Guava 19 examples --- .../test/java/com/baeldung/guava/GuavaMiscUtilsTest.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java b/guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java index 903381bd29..c7b8441b78 100644 --- a/guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java +++ b/guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java @@ -1,16 +1,9 @@ package com.baeldung.guava; -import com.baeldung.guava.entity.User; -import com.google.common.base.CharMatcher; import com.google.common.base.Throwables; import com.google.common.collect.*; -import com.google.common.hash.HashCode; -import com.google.common.hash.HashFunction; -import com.google.common.hash.Hashing; -import com.google.common.reflect.TypeToken; import org.junit.Test; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; From ed0c9aca109f02ea1e68db24df642578c67dc501 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 13:33:35 +0200 Subject: [PATCH 26/84] Update README.md --- spring-security-login-and-registration/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-login-and-registration/README.md b/spring-security-login-and-registration/README.md index 299d096c37..7235ef3bf6 100644 --- a/spring-security-login-and-registration/README.md +++ b/spring-security-login-and-registration/README.md @@ -9,6 +9,7 @@ - [Registration – Activate a New Account by Email](http://www.baeldung.com/registration-verify-user-by-email) - [Registration with Spring Security – Password Encoding](http://www.baeldung.com/spring-security-registration-password-encoding-bcrypt) - [Spring Security – Roles and Privileges](http://www.baeldung.com/role-and-privilege-for-spring-security-registration) +- [Prevent Brute Force Authentication Attempts with Spring Security](http://www.baeldung.com/spring-security-block-brute-force-authentication-attempts) ### Build the Project ``` From 855c8b153ca209f171750f069bf007ecb520b803 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 13:34:34 +0200 Subject: [PATCH 27/84] Update README.md --- spring-security-login-and-registration/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-login-and-registration/README.md b/spring-security-login-and-registration/README.md index 7235ef3bf6..8f47992c1d 100644 --- a/spring-security-login-and-registration/README.md +++ b/spring-security-login-and-registration/README.md @@ -10,6 +10,7 @@ - [Registration with Spring Security – Password Encoding](http://www.baeldung.com/spring-security-registration-password-encoding-bcrypt) - [Spring Security – Roles and Privileges](http://www.baeldung.com/role-and-privilege-for-spring-security-registration) - [Prevent Brute Force Authentication Attempts with Spring Security](http://www.baeldung.com/spring-security-block-brute-force-authentication-attempts) +- [Spring Security – Reset Your Password](http://www.baeldung.com/spring-security-registration-i-forgot-my-password) ### Build the Project ``` From 32373392a952a6c5bdb72fbc5754202889e4962c Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 13:35:55 +0200 Subject: [PATCH 28/84] Update README.md --- spring-security-rest-full/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-rest-full/README.md b/spring-security-rest-full/README.md index e2db0e0307..3df3947d5e 100644 --- a/spring-security-rest-full/README.md +++ b/spring-security-rest-full/README.md @@ -16,6 +16,7 @@ - [REST Query Language with Spring and JPA Criteria](http://www.baeldung.com/rest-search-language-spring-jpa-criteria) - [REST Query Language with Spring Data JPA Specifications](http://www.baeldung.com/rest-api-search-language-spring-data-specifications) - [REST Query Language with Spring Data JPA and QueryDSL](http://www.baeldung.com/rest-api-search-language-spring-data-querydsl) +- [REST Query Language – Advanced Search Operations](http://www.baeldung.com/rest-api-query-search-language-more-operations) ### Build the Project From bf6519b860da52ed8491c7bddeb7cc2d7d684dab Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 13:37:24 +0200 Subject: [PATCH 29/84] Update README.md --- spring-security-login-and-registration/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-login-and-registration/README.md b/spring-security-login-and-registration/README.md index 8f47992c1d..5b1980dcbf 100644 --- a/spring-security-login-and-registration/README.md +++ b/spring-security-login-and-registration/README.md @@ -11,6 +11,7 @@ - [Spring Security – Roles and Privileges](http://www.baeldung.com/role-and-privilege-for-spring-security-registration) - [Prevent Brute Force Authentication Attempts with Spring Security](http://www.baeldung.com/spring-security-block-brute-force-authentication-attempts) - [Spring Security – Reset Your Password](http://www.baeldung.com/spring-security-registration-i-forgot-my-password) +- [Spring Security Registration – Resend Verification Email](http://www.baeldung.com/spring-security-registration-verification-email) ### Build the Project ``` From e051f36401d19a16a0b36af987807a9f48140a22 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 13:41:29 +0200 Subject: [PATCH 30/84] Update README.md --- spring-security-login-and-registration/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-login-and-registration/README.md b/spring-security-login-and-registration/README.md index 5b1980dcbf..ee565dca65 100644 --- a/spring-security-login-and-registration/README.md +++ b/spring-security-login-and-registration/README.md @@ -12,6 +12,7 @@ - [Prevent Brute Force Authentication Attempts with Spring Security](http://www.baeldung.com/spring-security-block-brute-force-authentication-attempts) - [Spring Security – Reset Your Password](http://www.baeldung.com/spring-security-registration-i-forgot-my-password) - [Spring Security Registration – Resend Verification Email](http://www.baeldung.com/spring-security-registration-verification-email) +- [The Registration API becomes RESTful](http://www.baeldung.com/registration-restful-api) ### Build the Project ``` From 24a8e3803ce097e584b2b1c46f7ff4b497ce4912 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 13:42:54 +0200 Subject: [PATCH 31/84] Update README.md --- spring-security-rest-full/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-rest-full/README.md b/spring-security-rest-full/README.md index 3df3947d5e..6b56f4b918 100644 --- a/spring-security-rest-full/README.md +++ b/spring-security-rest-full/README.md @@ -17,6 +17,7 @@ - [REST Query Language with Spring Data JPA Specifications](http://www.baeldung.com/rest-api-search-language-spring-data-specifications) - [REST Query Language with Spring Data JPA and QueryDSL](http://www.baeldung.com/rest-api-search-language-spring-data-querydsl) - [REST Query Language – Advanced Search Operations](http://www.baeldung.com/rest-api-query-search-language-more-operations) +- [Metrics for your Spring REST API](http://www.baeldung.com/spring-rest-api-metrics) ### Build the Project From 6b0d2165ce7c67a1d367c825dde605e2e121b78a Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 13:44:08 +0200 Subject: [PATCH 32/84] Update README.md --- spring-security-login-and-registration/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-login-and-registration/README.md b/spring-security-login-and-registration/README.md index ee565dca65..e1b3e61acc 100644 --- a/spring-security-login-and-registration/README.md +++ b/spring-security-login-and-registration/README.md @@ -13,6 +13,7 @@ - [Spring Security – Reset Your Password](http://www.baeldung.com/spring-security-registration-i-forgot-my-password) - [Spring Security Registration – Resend Verification Email](http://www.baeldung.com/spring-security-registration-verification-email) - [The Registration API becomes RESTful](http://www.baeldung.com/registration-restful-api) +- [Registration – Password Strength and Rules](http://www.baeldung.com/registration-password-strength-and-rules) ### Build the Project ``` From 11065cd00481cc472e9144a4274e7400a3ad53ab Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 13:45:30 +0200 Subject: [PATCH 33/84] Update README.md --- spring-security-login-and-registration/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-login-and-registration/README.md b/spring-security-login-and-registration/README.md index e1b3e61acc..07fb21bfff 100644 --- a/spring-security-login-and-registration/README.md +++ b/spring-security-login-and-registration/README.md @@ -14,6 +14,7 @@ - [Spring Security Registration – Resend Verification Email](http://www.baeldung.com/spring-security-registration-verification-email) - [The Registration API becomes RESTful](http://www.baeldung.com/registration-restful-api) - [Registration – Password Strength and Rules](http://www.baeldung.com/registration-password-strength-and-rules) +- [Updating your Password](http://www.baeldung.com/updating-your-password/) ### Build the Project ``` From 95f51e577bc80b8cafe200d2d9ff5b9732eb9566 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 13:46:41 +0200 Subject: [PATCH 34/84] Update README.md --- spring-security-rest-full/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-rest-full/README.md b/spring-security-rest-full/README.md index 6b56f4b918..b592f7558c 100644 --- a/spring-security-rest-full/README.md +++ b/spring-security-rest-full/README.md @@ -18,6 +18,7 @@ - [REST Query Language with Spring Data JPA and QueryDSL](http://www.baeldung.com/rest-api-search-language-spring-data-querydsl) - [REST Query Language – Advanced Search Operations](http://www.baeldung.com/rest-api-query-search-language-more-operations) - [Metrics for your Spring REST API](http://www.baeldung.com/spring-rest-api-metrics) +- [REST Query Language with RSQL](http://www.baeldung.com/rest-api-search-language-rsql-fiql) ### Build the Project From 9f3be320b49901d8900cab69d010f50346e579c4 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 13:47:44 +0200 Subject: [PATCH 35/84] Update README.md --- httpclient/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/httpclient/README.md b/httpclient/README.md index 1cf788287c..e06c57da71 100644 --- a/httpclient/README.md +++ b/httpclient/README.md @@ -16,3 +16,4 @@ - [HttpClient Basic Authentication](http://www.baeldung.com/httpclient-4-basic-authentication) - [Multipart Upload with HttpClient 4](http://www.baeldung.com/httpclient-multipart-upload) - [HttpAsyncClient Tutorial](http://www.baeldung.com/httpasyncclient-tutorial) +- [HttpClient 4 Tutorial](http://www.baeldung.com/httpclient-guide) From ac96fae1a7bd262e77ae1e51b650ad2115bf35a3 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 13:50:01 +0200 Subject: [PATCH 36/84] Update README.md --- jackson/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/jackson/README.md b/jackson/README.md index d49ad9088a..c17d89912f 100644 --- a/jackson/README.md +++ b/jackson/README.md @@ -11,3 +11,4 @@ - [Jackson Exceptions – Problems and Solutions](http://www.baeldung.com/jackson-exception) - [Jackson Date](http://www.baeldung.com/jackson-serialize-dates) - [Jackson – Bidirectional Relationships](http://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion) +- [Jackson JSON Tutorial](http://www.baeldung.com/jackson) From dbaf47927008cf4fb467d93a66b55982df354083 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 13:50:52 +0200 Subject: [PATCH 37/84] Update README.md --- jackson/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/jackson/README.md b/jackson/README.md index c17d89912f..cd08926386 100644 --- a/jackson/README.md +++ b/jackson/README.md @@ -12,3 +12,4 @@ - [Jackson Date](http://www.baeldung.com/jackson-serialize-dates) - [Jackson – Bidirectional Relationships](http://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion) - [Jackson JSON Tutorial](http://www.baeldung.com/jackson) +- [Jackson – Working with Maps and nulls](http://www.baeldung.com/jackson-map-null-values-or-null-key) From c273b4eda0ce1a5011dca9c04550d0763309a069 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 13:52:25 +0200 Subject: [PATCH 38/84] Update README.md --- jackson/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/jackson/README.md b/jackson/README.md index cd08926386..1a445b701b 100644 --- a/jackson/README.md +++ b/jackson/README.md @@ -13,3 +13,4 @@ - [Jackson – Bidirectional Relationships](http://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion) - [Jackson JSON Tutorial](http://www.baeldung.com/jackson) - [Jackson – Working with Maps and nulls](http://www.baeldung.com/jackson-map-null-values-or-null-key) +- [Jackson – Decide What Fields Get Serialized/Deserializaed](http://www.baeldung.com/jackson-field-serializable-deserializable-or-not) From 9bba394195c6d274d4a120cc0be6c0ba869a774d Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 13:53:22 +0200 Subject: [PATCH 39/84] Update README.md --- jackson/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/jackson/README.md b/jackson/README.md index 1a445b701b..e226721d61 100644 --- a/jackson/README.md +++ b/jackson/README.md @@ -14,3 +14,4 @@ - [Jackson JSON Tutorial](http://www.baeldung.com/jackson) - [Jackson – Working with Maps and nulls](http://www.baeldung.com/jackson-map-null-values-or-null-key) - [Jackson – Decide What Fields Get Serialized/Deserializaed](http://www.baeldung.com/jackson-field-serializable-deserializable-or-not) +- [A Guide to Jackson Annotations](http://www.baeldung.com/jackson-annotations) From 6a56dcaa15798e1f6724f3c74683f4e8512778f7 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:03:31 +0200 Subject: [PATCH 40/84] Create README.md --- spring-data-mongodb/README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 spring-data-mongodb/README.md diff --git a/spring-data-mongodb/README.md b/spring-data-mongodb/README.md new file mode 100644 index 0000000000..7d194b72c8 --- /dev/null +++ b/spring-data-mongodb/README.md @@ -0,0 +1,7 @@ +========= + +## Spring Data MongoDB + + +### Relevant Articles: +-[A Guide to Queries in Spring Data MongoDB](http://www.baeldung.com/queries-in-spring-data-mongodb) From 034c57571330f02a7616ca4406478728c10c42d6 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:03:50 +0200 Subject: [PATCH 41/84] Update README.md --- spring-data-mongodb/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-data-mongodb/README.md b/spring-data-mongodb/README.md index 7d194b72c8..d92edc8940 100644 --- a/spring-data-mongodb/README.md +++ b/spring-data-mongodb/README.md @@ -4,4 +4,4 @@ ### Relevant Articles: --[A Guide to Queries in Spring Data MongoDB](http://www.baeldung.com/queries-in-spring-data-mongodb) +- [A Guide to Queries in Spring Data MongoDB](http://www.baeldung.com/queries-in-spring-data-mongodb) From 344c29cc11c84e3de0d042fa448076d5bd63aa24 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:11:27 +0200 Subject: [PATCH 42/84] Update README.md --- spring-data-mongodb/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-data-mongodb/README.md b/spring-data-mongodb/README.md index d92edc8940..4a140c1be6 100644 --- a/spring-data-mongodb/README.md +++ b/spring-data-mongodb/README.md @@ -5,3 +5,4 @@ ### Relevant Articles: - [A Guide to Queries in Spring Data MongoDB](http://www.baeldung.com/queries-in-spring-data-mongodb) +- [Spring Data MongoDB – Indexes, Annotations and Converters](http://www.baeldung.com/spring-data-mongodb-index-annotations-converter) From 04947776da282b21b1b640bef6d76e9167fe7aa5 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:12:20 +0200 Subject: [PATCH 43/84] Update README.md --- spring-data-mongodb/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-data-mongodb/README.md b/spring-data-mongodb/README.md index 4a140c1be6..e784176879 100644 --- a/spring-data-mongodb/README.md +++ b/spring-data-mongodb/README.md @@ -6,3 +6,4 @@ ### Relevant Articles: - [A Guide to Queries in Spring Data MongoDB](http://www.baeldung.com/queries-in-spring-data-mongodb) - [Spring Data MongoDB – Indexes, Annotations and Converters](http://www.baeldung.com/spring-data-mongodb-index-annotations-converter) +- [Custom Cascading in Spring Data MongoDB](http://www.baeldung.com/cascading-with-dbref-and-lifecycle-events-in-spring-data-mongodb) From 11b30eea02668bf6c54f5e78a48821e843b07672 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:13:27 +0200 Subject: [PATCH 44/84] Update README.md --- spring-security-rest-full/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-rest-full/README.md b/spring-security-rest-full/README.md index b592f7558c..c6d6ed5848 100644 --- a/spring-security-rest-full/README.md +++ b/spring-security-rest-full/README.md @@ -19,6 +19,7 @@ - [REST Query Language – Advanced Search Operations](http://www.baeldung.com/rest-api-query-search-language-more-operations) - [Metrics for your Spring REST API](http://www.baeldung.com/spring-rest-api-metrics) - [REST Query Language with RSQL](http://www.baeldung.com/rest-api-search-language-rsql-fiql) +- [Spring RestTemplate Tutorial](http://www.baeldung.com/rest-template) ### Build the Project From 09e896a3577ff8a42c8516f972bdaf8c5abdeeda Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:20:12 +0200 Subject: [PATCH 45/84] Create README.md --- spring-katharsis/README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 spring-katharsis/README.md diff --git a/spring-katharsis/README.md b/spring-katharsis/README.md new file mode 100644 index 0000000000..ec0141f41a --- /dev/null +++ b/spring-katharsis/README.md @@ -0,0 +1,6 @@ +========= + +## Java Web Application + +### Relevant Articles: +- [JSON API in a Java Web Application](http://www.baeldung.com/json-api-java-spring-web-app) From 15b5ac439e675d47bd1151caad401654e129ab92 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:24:04 +0200 Subject: [PATCH 46/84] Update README.md --- spring-all/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-all/README.md b/spring-all/README.md index baab7ec083..977b8b7357 100644 --- a/spring-all/README.md +++ b/spring-all/README.md @@ -8,3 +8,4 @@ This project is used to replicate Spring Exceptions only. ### Relevant articles: - [Properties with Spring](http://www.baeldung.com/2012/02/06/properties-with-spring) - checkout the `org.baeldung.properties` package for all scenarios of properties injection and usage - [Spring Profiles](http://www.baeldung.com/spring-profiles) +- [A Spring Custom Annotation for a Better DAO](http://www.baeldung.com/spring-annotation-bean-pre-processor) From 0b55104f32e22152e9dd2657a3359c0f2fe57e85 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:25:03 +0200 Subject: [PATCH 47/84] Update README.md --- spring-data-mongodb/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-data-mongodb/README.md b/spring-data-mongodb/README.md index e784176879..c5d2d2df41 100644 --- a/spring-data-mongodb/README.md +++ b/spring-data-mongodb/README.md @@ -7,3 +7,4 @@ - [A Guide to Queries in Spring Data MongoDB](http://www.baeldung.com/queries-in-spring-data-mongodb) - [Spring Data MongoDB – Indexes, Annotations and Converters](http://www.baeldung.com/spring-data-mongodb-index-annotations-converter) - [Custom Cascading in Spring Data MongoDB](http://www.baeldung.com/cascading-with-dbref-and-lifecycle-events-in-spring-data-mongodb) +- [GridFS in Spring Data MongoDB](http://www.baeldung.com/spring-data-mongodb-gridfs) From f602cea186c7fe224b2c537116d4e7db9fe73e97 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:25:49 +0200 Subject: [PATCH 48/84] Update README.md --- spring-mvc-java/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index bf76c7e1d4..113f690920 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -4,3 +4,4 @@ ### Relevant Articles: +- [http://www.baeldung.com/spring-bean-annotations](http://www.baeldung.com/spring-bean-annotations) From d91f7672ff4f8f4b7938be4fd686f3685cce4ac8 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:26:17 +0200 Subject: [PATCH 49/84] Update README.md --- spring-mvc-java/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index 113f690920..ccbd1288f0 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -4,4 +4,4 @@ ### Relevant Articles: -- [http://www.baeldung.com/spring-bean-annotations](http://www.baeldung.com/spring-bean-annotations) +- [Spring Bean Annotations](http://www.baeldung.com/spring-bean-annotations) From eb3b30e9b1fa190c26629ef7152607e1c7e02bea Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:27:10 +0200 Subject: [PATCH 50/84] Update README.md --- spring-security-rest/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-rest/README.md b/spring-security-rest/README.md index a3fce32a9c..d17cb24f7e 100644 --- a/spring-security-rest/README.md +++ b/spring-security-rest/README.md @@ -5,3 +5,4 @@ ### Relevant Articles: - [Spring REST Service Security](http://www.baeldung.com/2011/10/31/securing-a-restful-web-service-with-spring-security-3-1-part-3/) +- [Setting Up Swagger 2 with a Spring REST API](http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api) From 238f5d340f286e64f91da86a0eb49864e29ea4b6 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:28:15 +0200 Subject: [PATCH 51/84] Update README.md --- spring-mvc-java/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index ccbd1288f0..f6bb37b600 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -5,3 +5,4 @@ ### Relevant Articles: - [Spring Bean Annotations](http://www.baeldung.com/spring-bean-annotations) +- [Introduction to Pointcut Expressions in Spring](http://www.baeldung.com/spring-aop-pointcut-tutorial) From fda7b00e6be4707912c038b7d28e74b52964839b Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:29:26 +0200 Subject: [PATCH 52/84] Update README.md --- spring-mvc-java/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index f6bb37b600..2de2373149 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -6,3 +6,4 @@ ### Relevant Articles: - [Spring Bean Annotations](http://www.baeldung.com/spring-bean-annotations) - [Introduction to Pointcut Expressions in Spring](http://www.baeldung.com/spring-aop-pointcut-tutorial) +- [Introduction to Advice Types in Spring](http://www.baeldung.com/spring-aop-advice-tutorial) From 03ddc9056e271240838413ddd857d520d2995db2 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:30:23 +0200 Subject: [PATCH 53/84] Update README.md --- spring-data-cassandra/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-data-cassandra/README.md b/spring-data-cassandra/README.md index a245ff62a1..85ea3b8649 100644 --- a/spring-data-cassandra/README.md +++ b/spring-data-cassandra/README.md @@ -2,7 +2,7 @@ ### Relevant Articles: - [Introduction to Spring Data Cassandra](http://www.baeldung.com/spring-data-cassandra-tutorial) - +- [http://www.baeldung.com/spring-data-cassandratemplate-cqltemplate](http://www.baeldung.com/spring-data-cassandratemplate-cqltemplate) ### Build the Project with Tests Running ``` mvn clean install From cde526808ad9c504871c6aa8b7aecb28f2b9542b Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:31:11 +0200 Subject: [PATCH 54/84] Update README.md --- spring-data-cassandra/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-data-cassandra/README.md b/spring-data-cassandra/README.md index 85ea3b8649..456eefcf18 100644 --- a/spring-data-cassandra/README.md +++ b/spring-data-cassandra/README.md @@ -2,7 +2,8 @@ ### Relevant Articles: - [Introduction to Spring Data Cassandra](http://www.baeldung.com/spring-data-cassandra-tutorial) -- [http://www.baeldung.com/spring-data-cassandratemplate-cqltemplate](http://www.baeldung.com/spring-data-cassandratemplate-cqltemplate) +- [Using the CassandraTemplate from Spring Data](http://www.baeldung.com/spring-data-cassandratemplate-cqltemplate) + ### Build the Project with Tests Running ``` mvn clean install From cd8c2517dbf51fdd0a590afb8ab3b9f000ab62b3 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:32:10 +0200 Subject: [PATCH 55/84] Update README.md --- guava18/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/guava18/README.md b/guava18/README.md index 8960b4676e..9924d7c16f 100644 --- a/guava18/README.md +++ b/guava18/README.md @@ -7,7 +7,6 @@ - [Guava Collections Cookbook](http://www.baeldung.com/guava-collections) - [Guava Ordering Cookbook](http://www.baeldung.com/guava-order) - [Guava Functional Cookbook](http://www.baeldung.com/guava-functions-predicates) - - [Hamcrest Collections Cookbook](http://www.baeldung.com/hamcrest-collections-arrays) - - [Partition a List in Java](http://www.baeldung.com/java-list-split) +- [Guava 18: What’s New?](http://www.baeldung.com/whats-new-in-guava-18) From f7bd8093d037108b03591e7c32b584b5c495fdba Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:33:03 +0200 Subject: [PATCH 56/84] Update README.md --- core-java-8/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-8/README.md b/core-java-8/README.md index e3fa3f9848..62040ba5cf 100644 --- a/core-java-8/README.md +++ b/core-java-8/README.md @@ -5,3 +5,4 @@ ### Relevant Articles: // - [Java 8 – Powerful Comparison with Lambdas](http://www.baeldung.com/java-8-sort-lambda) - [Java – Directory Size](http://www.baeldung.com/java-folder-size) +- [Java – Try with Resources](http://www.baeldung.com/java-try-with-resources) From 2b4b9fa79c3fdb70590e7ee8f356da6e2cdac740 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:33:55 +0200 Subject: [PATCH 57/84] Update README.md --- spring-security-mvc-ldap/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-security-mvc-ldap/README.md b/spring-security-mvc-ldap/README.md index 260b4b38d8..686f611f99 100644 --- a/spring-security-mvc-ldap/README.md +++ b/spring-security-mvc-ldap/README.md @@ -5,7 +5,7 @@ ### Relevant Article: - [Spring Security - security none, filters none, access permitAll](http://www.baeldung.com/security-none-filters-none-access-permitAll) - [Spring Security Basic Authentication](http://www.baeldung.com/spring-security-basic-authentication) - +- [Intro to Spring Security LDAP](http://www.baeldung.com/spring-security-ldap) ### Notes - the project uses Spring Boot - simply run 'SampleLDAPApplication.java' to start up Spring Boot with a Tomcat container and embedded LDAP server. From 573f4bed69eafd21bbbb3908f538bb746de175dc Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Wed, 10 Feb 2016 14:35:56 +0200 Subject: [PATCH 58/84] Update README.md --- spring-batch/README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spring-batch/README.md b/spring-batch/README.md index 8b13789179..953e652cea 100644 --- a/spring-batch/README.md +++ b/spring-batch/README.md @@ -1 +1,7 @@ +========= +## Spring Batch + + +### Relevant Articles: +- [Introduction to Spring Batch](http://www.baeldung.com/introduction-to-spring-batch) From ebce69d28d14f77da8c390b92db58d15cd66ce04 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Thu, 11 Feb 2016 02:25:41 +0200 Subject: [PATCH 59/84] Update README.md --- spring-data-mongodb/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-data-mongodb/README.md b/spring-data-mongodb/README.md index c5d2d2df41..d656bc897c 100644 --- a/spring-data-mongodb/README.md +++ b/spring-data-mongodb/README.md @@ -8,3 +8,4 @@ - [Spring Data MongoDB – Indexes, Annotations and Converters](http://www.baeldung.com/spring-data-mongodb-index-annotations-converter) - [Custom Cascading in Spring Data MongoDB](http://www.baeldung.com/cascading-with-dbref-and-lifecycle-events-in-spring-data-mongodb) - [GridFS in Spring Data MongoDB](http://www.baeldung.com/spring-data-mongodb-gridfs) +- [Introduction to Spring Data MongoDB](http://www.baeldung.com/spring-data-mongodb-tutorial) From c3e5eb7ccfd942761e01d9b15ed21cf57f8511fe Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Thu, 11 Feb 2016 02:40:35 +0200 Subject: [PATCH 60/84] Update README.md --- core-java-8/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-8/README.md b/core-java-8/README.md index 62040ba5cf..ab89bbdaf2 100644 --- a/core-java-8/README.md +++ b/core-java-8/README.md @@ -6,3 +6,4 @@ // - [Java 8 – Powerful Comparison with Lambdas](http://www.baeldung.com/java-8-sort-lambda) - [Java – Directory Size](http://www.baeldung.com/java-folder-size) - [Java – Try with Resources](http://www.baeldung.com/java-try-with-resources) +- [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips) From a7cd46c02230f5f589fa58bb60e3873f092de5ec Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Thu, 11 Feb 2016 02:45:49 +0200 Subject: [PATCH 61/84] Create README.md --- mockito-mocks-spring-beans/README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 mockito-mocks-spring-beans/README.md diff --git a/mockito-mocks-spring-beans/README.md b/mockito-mocks-spring-beans/README.md new file mode 100644 index 0000000000..3ced7161fa --- /dev/null +++ b/mockito-mocks-spring-beans/README.md @@ -0,0 +1,7 @@ +========= + +## Mockito Mocks into Spring Beans + + +### Relevant Articles: +- [Injecting Mockito Mocks into Spring Beans](http://www.baeldung.com/injecting-mocks-in-spring) From fffeb216f5c22fa3399e61d4a0211c0595be9bfc Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Thu, 11 Feb 2016 02:47:43 +0200 Subject: [PATCH 62/84] Update README.md --- spring-security-rest/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-security-rest/README.md b/spring-security-rest/README.md index d17cb24f7e..6261fbe83d 100644 --- a/spring-security-rest/README.md +++ b/spring-security-rest/README.md @@ -6,3 +6,4 @@ ### Relevant Articles: - [Spring REST Service Security](http://www.baeldung.com/2011/10/31/securing-a-restful-web-service-with-spring-security-3-1-part-3/) - [Setting Up Swagger 2 with a Spring REST API](http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api) +- [Custom Error Message Handling for REST API](http://www.baeldung.com/global-error-handler-in-a-spring-rest-api) From ae8d5ca8db2087d120e236c8d1a01d693fb3068b Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Thu, 11 Feb 2016 02:48:47 +0200 Subject: [PATCH 63/84] Update README.md --- spring-hibernate4/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-hibernate4/README.md b/spring-hibernate4/README.md index acf20632fe..4c0c6706d6 100644 --- a/spring-hibernate4/README.md +++ b/spring-hibernate4/README.md @@ -7,6 +7,7 @@ - [The DAO with Spring 3 and Hibernate](http://www.baeldung.com/2011/12/02/the-persistence-layer-with-spring-3-1-and-hibernate/) - [Hibernate Pagination](http://www.baeldung.com/hibernate-pagination) - [Sorting with Hibernate](http://www.baeldung.com/hibernate-sort) +- [Auditing with JPA, Hibernate, and Spring Data JPA](http://www.baeldung.com/database-auditing-jpa) ### Quick Start From 80da6e8668dbe02bcd3777529ec801dafb10e8d9 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Thu, 11 Feb 2016 02:51:08 +0200 Subject: [PATCH 64/84] Create README.md --- spring-freemarker/README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 spring-freemarker/README.md diff --git a/spring-freemarker/README.md b/spring-freemarker/README.md new file mode 100644 index 0000000000..bd939d11e9 --- /dev/null +++ b/spring-freemarker/README.md @@ -0,0 +1,7 @@ +========= + +## Using FreeMarker in Spring MVC + + +### Relevant Articles: +- [Introduction to Using FreeMarker in Spring MVC](http://www.baeldung.com/freemarker-in-spring-mvc-tutorial) From 02ebbd4044ef107f69bc89823827f37064afe50b Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Thu, 11 Feb 2016 02:52:17 +0200 Subject: [PATCH 65/84] Update README.md --- spring-security-rest-full/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-security-rest-full/README.md b/spring-security-rest-full/README.md index c6d6ed5848..f648c49244 100644 --- a/spring-security-rest-full/README.md +++ b/spring-security-rest-full/README.md @@ -20,7 +20,7 @@ - [Metrics for your Spring REST API](http://www.baeldung.com/spring-rest-api-metrics) - [REST Query Language with RSQL](http://www.baeldung.com/rest-api-search-language-rsql-fiql) - [Spring RestTemplate Tutorial](http://www.baeldung.com/rest-template) - +- [A Guide to CSRF Protection in Spring Security](http://www.baeldung.com/spring-security-csrf) ### Build the Project ``` From 7011fa40fec956a2bfce298d896129ccccc96354 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Thu, 11 Feb 2016 02:53:18 +0200 Subject: [PATCH 66/84] Update README.md --- jackson/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/jackson/README.md b/jackson/README.md index e226721d61..53b9c7c31d 100644 --- a/jackson/README.md +++ b/jackson/README.md @@ -15,3 +15,4 @@ - [Jackson – Working with Maps and nulls](http://www.baeldung.com/jackson-map-null-values-or-null-key) - [Jackson – Decide What Fields Get Serialized/Deserializaed](http://www.baeldung.com/jackson-field-serializable-deserializable-or-not) - [A Guide to Jackson Annotations](http://www.baeldung.com/jackson-annotations) +- [Working with Tree Model Nodes in Jackson](http://www.baeldung.com/jackson-json-node-tree-model) From edcb2e2e69edcc25c8f9233a899fab5020a2f7f4 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Thu, 11 Feb 2016 02:54:21 +0200 Subject: [PATCH 67/84] Update README.md --- core-java-8/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-8/README.md b/core-java-8/README.md index ab89bbdaf2..8bef3a1be0 100644 --- a/core-java-8/README.md +++ b/core-java-8/README.md @@ -7,3 +7,4 @@ - [Java – Directory Size](http://www.baeldung.com/java-folder-size) - [Java – Try with Resources](http://www.baeldung.com/java-try-with-resources) - [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips) +- [The Double Colon Operator in Java 8](http://www.baeldung.com/java-8-double-colon-operator) From 71ef292b3a18992345b58007cb0b9c26d63ff56c Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Thu, 11 Feb 2016 02:56:17 +0200 Subject: [PATCH 68/84] Create README.md --- spring-zuul/README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 spring-zuul/README.md diff --git a/spring-zuul/README.md b/spring-zuul/README.md new file mode 100644 index 0000000000..41a77f70c8 --- /dev/null +++ b/spring-zuul/README.md @@ -0,0 +1,7 @@ +========= + +## Spring REST with a Zuul + + +### Relevant Articles: +- [Spring REST with a Zuul Proxy](http://www.baeldung.com/spring-rest-with-zuul-proxy) From e77e6ebfcf045138cf9e8b2cf9650516d09718e0 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Thu, 11 Feb 2016 03:03:08 +0200 Subject: [PATCH 69/84] Update README.md --- spring-rest/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-rest/README.md b/spring-rest/README.md index 3b93b06d66..9d373962c4 100644 --- a/spring-rest/README.md +++ b/spring-rest/README.md @@ -7,3 +7,4 @@ - [Spring @RequestMapping](http://www.baeldung.com/spring-requestmapping) - [Http Message Converters with the Spring Framework](http://www.baeldung.com/spring-httpmessageconverter-rest) - [Redirect in Spring](http://www.baeldung.com/spring-redirect-and-forward) +- [Returning Custom Status Codes from Spring Controllers](http://www.baeldung.com/spring-mvc-controller-custom-http-status-code) From b0d8d832804b8366d025e02f01e5d6ea2821af81 Mon Sep 17 00:00:00 2001 From: MichelLeBon Date: Thu, 11 Feb 2016 03:04:45 +0200 Subject: [PATCH 70/84] Update README.md --- spring-mvc-java/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index 2de2373149..e5264b0370 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -7,3 +7,4 @@ - [Spring Bean Annotations](http://www.baeldung.com/spring-bean-annotations) - [Introduction to Pointcut Expressions in Spring](http://www.baeldung.com/spring-aop-pointcut-tutorial) - [Introduction to Advice Types in Spring](http://www.baeldung.com/spring-aop-advice-tutorial) +- [A Guide to the ViewResolver in Spring MVC](http://www.baeldung.com/spring-mvc-view-resolver-tutorial) From 45358317b6b4f33ead9c4d923c3d7d3bf3e7c281 Mon Sep 17 00:00:00 2001 From: DOHA Date: Sat, 13 Feb 2016 22:25:31 +0200 Subject: [PATCH 71/84] OpenID Connect example --- spring-openid/.classpath | 32 ++++++++ spring-openid/.project | 48 +++++++++++ spring-openid/pom.xml | 68 ++++++++++++++++ .../config/GoogleOpenIdConnectConfig.java | 51 ++++++++++++ .../org/baeldung/config/HomeController.java | 22 +++++ .../org/baeldung/config/SecurityConfig.java | 49 +++++++++++ .../config/SpringOpenidApplication.java | 13 +++ .../security/OpenIdConnectFilter.java | 71 ++++++++++++++++ .../security/OpenIdConnectUserDetails.java | 81 +++++++++++++++++++ .../src/main/resources/application.properties | 6 ++ 10 files changed, 441 insertions(+) create mode 100644 spring-openid/.classpath create mode 100644 spring-openid/.project create mode 100644 spring-openid/pom.xml create mode 100644 spring-openid/src/main/java/org/baeldung/config/GoogleOpenIdConnectConfig.java create mode 100644 spring-openid/src/main/java/org/baeldung/config/HomeController.java create mode 100644 spring-openid/src/main/java/org/baeldung/config/SecurityConfig.java create mode 100644 spring-openid/src/main/java/org/baeldung/config/SpringOpenidApplication.java create mode 100644 spring-openid/src/main/java/org/baeldung/security/OpenIdConnectFilter.java create mode 100644 spring-openid/src/main/java/org/baeldung/security/OpenIdConnectUserDetails.java create mode 100644 spring-openid/src/main/resources/application.properties diff --git a/spring-openid/.classpath b/spring-openid/.classpath new file mode 100644 index 0000000000..0cad5db2d0 --- /dev/null +++ b/spring-openid/.classpath @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-openid/.project b/spring-openid/.project new file mode 100644 index 0000000000..81874eb896 --- /dev/null +++ b/spring-openid/.project @@ -0,0 +1,48 @@ + + + spring-openid + + + + + + org.eclipse.wst.jsdt.core.javascriptValidator + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.springframework.ide.eclipse.core.springnature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.wst.jsdt.core.jsNature + + diff --git a/spring-openid/pom.xml b/spring-openid/pom.xml new file mode 100644 index 0000000000..641fe93a09 --- /dev/null +++ b/spring-openid/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + org.baeldung + spring-openid + 0.0.1-SNAPSHOT + war + + spring-openid + Spring OpenID sample project + + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + + + + + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-tomcat + provided + + + + org.springframework.security.oauth + spring-security-oauth2 + + + + org.springframework.security + spring-security-jwt + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + UTF-8 + 1.8 + + diff --git a/spring-openid/src/main/java/org/baeldung/config/GoogleOpenIdConnectConfig.java b/spring-openid/src/main/java/org/baeldung/config/GoogleOpenIdConnectConfig.java new file mode 100644 index 0000000000..8e9c6e974e --- /dev/null +++ b/spring-openid/src/main/java/org/baeldung/config/GoogleOpenIdConnectConfig.java @@ -0,0 +1,51 @@ +package org.baeldung.config; + +import java.util.Arrays; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.oauth2.client.OAuth2ClientContext; +import org.springframework.security.oauth2.client.OAuth2RestTemplate; +import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails; +import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client; + +@Configuration +@EnableOAuth2Client +public class GoogleOpenIdConnectConfig { + @Value("${google.clientId}") + private String clientId; + + @Value("${google.clientSecret}") + private String clientSecret; + + @Value("${google.accessTokenUri}") + private String accessTokenUri; + + @Value("${google.userAuthorizationUri}") + private String userAuthorizationUri; + + @Value("${google.redirectUri}") + private String redirectUri; + + @Bean + public OAuth2ProtectedResourceDetails googleOpenId() { + final AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails(); + details.setClientId(clientId); + details.setClientSecret(clientSecret); + details.setAccessTokenUri(accessTokenUri); + details.setUserAuthorizationUri(userAuthorizationUri); + details.setScope(Arrays.asList("openid", "email")); + details.setPreEstablishedRedirectUri(redirectUri); + details.setUseCurrentUri(false); + return details; + } + + @Bean + public OAuth2RestTemplate googleOpenIdTemplate(final OAuth2ClientContext clientContext) { + final OAuth2RestTemplate template = new OAuth2RestTemplate(googleOpenId(), clientContext); + return template; + } + +} diff --git a/spring-openid/src/main/java/org/baeldung/config/HomeController.java b/spring-openid/src/main/java/org/baeldung/config/HomeController.java new file mode 100644 index 0000000000..f0a5378019 --- /dev/null +++ b/spring-openid/src/main/java/org/baeldung/config/HomeController.java @@ -0,0 +1,22 @@ +package org.baeldung.config; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class HomeController { + private final Logger logger = LoggerFactory.getLogger(getClass()); + + @RequestMapping("/") + @ResponseBody + public final String home() { + final String username = SecurityContextHolder.getContext().getAuthentication().getName(); + logger.info(username); + return "Welcome, " + username; + } + +} diff --git a/spring-openid/src/main/java/org/baeldung/config/SecurityConfig.java b/spring-openid/src/main/java/org/baeldung/config/SecurityConfig.java new file mode 100644 index 0000000000..d929bfd631 --- /dev/null +++ b/spring-openid/src/main/java/org/baeldung/config/SecurityConfig.java @@ -0,0 +1,49 @@ +package org.baeldung.config; + +import org.baeldung.security.OpenIdConnectFilter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.builders.WebSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.oauth2.client.OAuth2RestTemplate; +import org.springframework.security.oauth2.client.filter.OAuth2ClientContextFilter; +import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint; +import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter; + +@Configuration +@EnableWebSecurity +public class SecurityConfig extends WebSecurityConfigurerAdapter { + @Autowired + private OAuth2RestTemplate restTemplate; + + @Override + public void configure(WebSecurity web) throws Exception { + web.ignoring().antMatchers("/resources/**"); + } + + @Bean + public OpenIdConnectFilter myFilter() { + final OpenIdConnectFilter filter = new OpenIdConnectFilter("/google-login"); + filter.setRestTemplate(restTemplate); + return filter; + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + // @formatter:off + http + .addFilterAfter(new OAuth2ClientContextFilter(), AbstractPreAuthenticatedProcessingFilter.class) + .addFilterAfter(myFilter(), OAuth2ClientContextFilter.class) + .httpBasic().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/google-login")) + .and() + .authorizeRequests() + // .antMatchers("/","/index*").permitAll() + .anyRequest().authenticated() + ; + + // @formatter:on + } +} \ No newline at end of file diff --git a/spring-openid/src/main/java/org/baeldung/config/SpringOpenidApplication.java b/spring-openid/src/main/java/org/baeldung/config/SpringOpenidApplication.java new file mode 100644 index 0000000000..608e8a6819 --- /dev/null +++ b/spring-openid/src/main/java/org/baeldung/config/SpringOpenidApplication.java @@ -0,0 +1,13 @@ +package org.baeldung.config; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringOpenidApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringOpenidApplication.class, args); + } + +} diff --git a/spring-openid/src/main/java/org/baeldung/security/OpenIdConnectFilter.java b/spring-openid/src/main/java/org/baeldung/security/OpenIdConnectFilter.java new file mode 100644 index 0000000000..c0970ab3cf --- /dev/null +++ b/spring-openid/src/main/java/org/baeldung/security/OpenIdConnectFilter.java @@ -0,0 +1,71 @@ +package org.baeldung.security; + +import java.io.IOException; +import java.util.Map; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.jwt.Jwt; +import org.springframework.security.jwt.JwtHelper; +import org.springframework.security.oauth2.client.OAuth2RestOperations; +import org.springframework.security.oauth2.client.OAuth2RestTemplate; +import org.springframework.security.oauth2.common.OAuth2AccessToken; +import org.springframework.security.oauth2.common.exceptions.InvalidTokenException; +import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; +import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class OpenIdConnectFilter extends AbstractAuthenticationProcessingFilter { + public OAuth2RestOperations restTemplate; + + public OpenIdConnectFilter(String defaultFilterProcessesUrl) { + super(defaultFilterProcessesUrl); + setAuthenticationManager(new NoopAuthenticationManager()); + } + + @Override + public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException { + + OAuth2AccessToken accessToken; + try { + accessToken = restTemplate.getAccessToken(); + } catch (final OAuth2Exception e) { + throw new BadCredentialsException("Could not obtain access token", e); + } + try { + final String idToken = accessToken.getAdditionalInformation().get("id_token").toString(); + final Jwt tokenDecoded = JwtHelper.decode(idToken); + System.out.println("===== : " + tokenDecoded.getClaims()); + + final Map authInfo = new ObjectMapper().readValue(tokenDecoded.getClaims(), Map.class); + + final OpenIdConnectUserDetails user = new OpenIdConnectUserDetails(authInfo, accessToken); + return new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities()); + } catch (final InvalidTokenException e) { + throw new BadCredentialsException("Could not obtain user details from token", e); + } + + } + + public void setRestTemplate(OAuth2RestTemplate restTemplate2) { + restTemplate = restTemplate2; + + } + + private static class NoopAuthenticationManager implements AuthenticationManager { + + @Override + public Authentication authenticate(Authentication authentication) throws AuthenticationException { + throw new UnsupportedOperationException("No authentication should be done with this AuthenticationManager"); + } + + } +} diff --git a/spring-openid/src/main/java/org/baeldung/security/OpenIdConnectUserDetails.java b/spring-openid/src/main/java/org/baeldung/security/OpenIdConnectUserDetails.java new file mode 100644 index 0000000000..f0d91fdc27 --- /dev/null +++ b/spring-openid/src/main/java/org/baeldung/security/OpenIdConnectUserDetails.java @@ -0,0 +1,81 @@ +package org.baeldung.security; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Map; + +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.oauth2.common.OAuth2AccessToken; + +public class OpenIdConnectUserDetails implements UserDetails { + + private static final long serialVersionUID = 1L; + + private String userId; + private String username; + private OAuth2AccessToken token; + + public OpenIdConnectUserDetails(Map userInfo, OAuth2AccessToken token) { + this.userId = userInfo.get("sub"); + this.username = userInfo.get("email"); + this.token = token; + } + + @Override + public String getUsername() { + return username; + } + + @Override + public Collection getAuthorities() { + return Arrays.asList(new SimpleGrantedAuthority("ROLE_USER")); + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public OAuth2AccessToken getToken() { + return token; + } + + public void setToken(OAuth2AccessToken token) { + this.token = token; + } + + public void setUsername(String username) { + this.username = username; + } + + @Override + public String getPassword() { + return null; + } + + @Override + public boolean isAccountNonExpired() { + return true; + } + + @Override + public boolean isAccountNonLocked() { + return true; + } + + @Override + public boolean isCredentialsNonExpired() { + return true; + } + + @Override + public boolean isEnabled() { + return true; + } + +} diff --git a/spring-openid/src/main/resources/application.properties b/spring-openid/src/main/resources/application.properties new file mode 100644 index 0000000000..fa567a164c --- /dev/null +++ b/spring-openid/src/main/resources/application.properties @@ -0,0 +1,6 @@ +server.port=8081 +google.clientId=497324626536-d6fphsh1qpl2o6j2j66nukajrfqc0rtq.apps.googleusercontent.com +google.clientSecret=vtueZycMsrRjjCjnY6JzbEZT +google.accessTokenUri=https://www.googleapis.com/oauth2/v3/token +google.userAuthorizationUri=https://accounts.google.com/o/oauth2/auth +google.redirectUri=http://localhost:8081/google-login \ No newline at end of file From 4b5ad0629a854078437e8248bbbf341165f7e4f8 Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sat, 30 Jan 2016 17:48:56 +0100 Subject: [PATCH 72/84] RestEasy Tutorial, CRUD Services example --- RestEasy Example/pom.xml | 91 +++ .../src/main/java/com/baeldung/Movie.java | 535 ++++++++++++++++++ .../baeldung/client/ServicesInterface.java | 44 ++ .../server/service/MovieCrudService.java | 94 +++ .../server/service/RestEasyServices.java | 40 ++ .../src/main/resources/schema1.xsd | 29 + .../main/webapp/WEB-INF/classes/logback.xml | 3 + .../WEB-INF/jboss-deployment-structure.xml | 16 + .../src/main/webapp/WEB-INF/jboss-web.xml | 4 + .../src/main/webapp/WEB-INF/web.xml | 51 ++ .../com/baeldung/server/RestEasyClient.java | 50 ++ .../resources/server/movies/transformer.json | 22 + 12 files changed, 979 insertions(+) create mode 100644 RestEasy Example/pom.xml create mode 100644 RestEasy Example/src/main/java/com/baeldung/Movie.java create mode 100644 RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java create mode 100644 RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java create mode 100644 RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java create mode 100644 RestEasy Example/src/main/resources/schema1.xsd create mode 100644 RestEasy Example/src/main/webapp/WEB-INF/classes/logback.xml create mode 100644 RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml create mode 100644 RestEasy Example/src/main/webapp/WEB-INF/jboss-web.xml create mode 100644 RestEasy Example/src/main/webapp/WEB-INF/web.xml create mode 100644 RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java create mode 100644 RestEasy Example/src/test/resources/server/movies/transformer.json diff --git a/RestEasy Example/pom.xml b/RestEasy Example/pom.xml new file mode 100644 index 0000000000..b16c5e8267 --- /dev/null +++ b/RestEasy Example/pom.xml @@ -0,0 +1,91 @@ + + + 4.0.0 + + com.baeldung + resteasy-tutorial + 1.0 + war + + + + jboss + http://repository.jboss.org/nexus/content/groups/public/ + + + + + 3.0.14.Final + runtime + + + + RestEasyTutorial + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + + org.jboss.resteasy + jaxrs-api + 3.0.12.Final + ${resteasy.scope} + + + + org.jboss.resteasy + resteasy-servlet-initializer + ${resteasy.version} + ${resteasy.scope} + + + jboss-jaxrs-api_2.0_spec + org.jboss.spec.javax.ws.rs + + + + + + org.jboss.resteasy + resteasy-client + ${resteasy.version} + ${resteasy.scope} + + + + + javax.ws.rs + javax.ws.rs-api + 2.0.1 + + + + org.jboss.resteasy + resteasy-jackson-provider + ${resteasy.version} + ${resteasy.scope} + + + + org.jboss.resteasy + resteasy-jaxb-provider + ${resteasy.version} + ${resteasy.scope} + + + + + + \ No newline at end of file diff --git a/RestEasy Example/src/main/java/com/baeldung/Movie.java b/RestEasy Example/src/main/java/com/baeldung/Movie.java new file mode 100644 index 0000000000..c0041d2e95 --- /dev/null +++ b/RestEasy Example/src/main/java/com/baeldung/Movie.java @@ -0,0 +1,535 @@ + +package com.baeldung; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlType; + + +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "movie", propOrder = { + "actors", + "awards", + "country", + "director", + "genre", + "imdbID", + "imdbRating", + "imdbVotes", + "language", + "metascore", + "plot", + "poster", + "rated", + "released", + "response", + "runtime", + "title", + "type", + "writer", + "year" +}) +public class Movie { + + protected String actors; + protected String awards; + protected String country; + protected String director; + protected String genre; + protected String imdbID; + protected String imdbRating; + protected String imdbVotes; + protected String language; + protected String metascore; + protected String plot; + protected String poster; + protected String rated; + protected String released; + protected String response; + protected String runtime; + protected String title; + protected String type; + protected String writer; + protected String year; + + /** + * Recupera il valore della propriet� actors. + * + * @return + * possible object is + * {@link String } + * + */ + public String getActors() { + return actors; + } + + /** + * Imposta il valore della propriet� actors. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setActors(String value) { + this.actors = value; + } + + /** + * Recupera il valore della propriet� awards. + * + * @return + * possible object is + * {@link String } + * + */ + public String getAwards() { + return awards; + } + + /** + * Imposta il valore della propriet� awards. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setAwards(String value) { + this.awards = value; + } + + /** + * Recupera il valore della propriet� country. + * + * @return + * possible object is + * {@link String } + * + */ + public String getCountry() { + return country; + } + + /** + * Imposta il valore della propriet� country. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setCountry(String value) { + this.country = value; + } + + /** + * Recupera il valore della propriet� director. + * + * @return + * possible object is + * {@link String } + * + */ + public String getDirector() { + return director; + } + + /** + * Imposta il valore della propriet� director. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setDirector(String value) { + this.director = value; + } + + /** + * Recupera il valore della propriet� genre. + * + * @return + * possible object is + * {@link String } + * + */ + public String getGenre() { + return genre; + } + + /** + * Imposta il valore della propriet� genre. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setGenre(String value) { + this.genre = value; + } + + /** + * Recupera il valore della propriet� imdbID. + * + * @return + * possible object is + * {@link String } + * + */ + public String getImdbID() { + return imdbID; + } + + /** + * Imposta il valore della propriet� imdbID. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setImdbID(String value) { + this.imdbID = value; + } + + /** + * Recupera il valore della propriet� imdbRating. + * + * @return + * possible object is + * {@link String } + * + */ + public String getImdbRating() { + return imdbRating; + } + + /** + * Imposta il valore della propriet� imdbRating. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setImdbRating(String value) { + this.imdbRating = value; + } + + /** + * Recupera il valore della propriet� imdbVotes. + * + * @return + * possible object is + * {@link String } + * + */ + public String getImdbVotes() { + return imdbVotes; + } + + /** + * Imposta il valore della propriet� imdbVotes. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setImdbVotes(String value) { + this.imdbVotes = value; + } + + /** + * Recupera il valore della propriet� language. + * + * @return + * possible object is + * {@link String } + * + */ + public String getLanguage() { + return language; + } + + /** + * Imposta il valore della propriet� language. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setLanguage(String value) { + this.language = value; + } + + /** + * Recupera il valore della propriet� metascore. + * + * @return + * possible object is + * {@link String } + * + */ + public String getMetascore() { + return metascore; + } + + /** + * Imposta il valore della propriet� metascore. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setMetascore(String value) { + this.metascore = value; + } + + /** + * Recupera il valore della propriet� plot. + * + * @return + * possible object is + * {@link String } + * + */ + public String getPlot() { + return plot; + } + + /** + * Imposta il valore della propriet� plot. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setPlot(String value) { + this.plot = value; + } + + /** + * Recupera il valore della propriet� poster. + * + * @return + * possible object is + * {@link String } + * + */ + public String getPoster() { + return poster; + } + + /** + * Imposta il valore della propriet� poster. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setPoster(String value) { + this.poster = value; + } + + /** + * Recupera il valore della propriet� rated. + * + * @return + * possible object is + * {@link String } + * + */ + public String getRated() { + return rated; + } + + /** + * Imposta il valore della propriet� rated. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setRated(String value) { + this.rated = value; + } + + /** + * Recupera il valore della propriet� released. + * + * @return + * possible object is + * {@link String } + * + */ + public String getReleased() { + return released; + } + + /** + * Imposta il valore della propriet� released. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setReleased(String value) { + this.released = value; + } + + /** + * Recupera il valore della propriet� response. + * + * @return + * possible object is + * {@link String } + * + */ + public String getResponse() { + return response; + } + + /** + * Imposta il valore della propriet� response. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setResponse(String value) { + this.response = value; + } + + /** + * Recupera il valore della propriet� runtime. + * + * @return + * possible object is + * {@link String } + * + */ + public String getRuntime() { + return runtime; + } + + /** + * Imposta il valore della propriet� runtime. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setRuntime(String value) { + this.runtime = value; + } + + /** + * Recupera il valore della propriet� title. + * + * @return + * possible object is + * {@link String } + * + */ + public String getTitle() { + return title; + } + + /** + * Imposta il valore della propriet� title. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setTitle(String value) { + this.title = value; + } + + /** + * Recupera il valore della propriet� type. + * + * @return + * possible object is + * {@link String } + * + */ + public String getType() { + return type; + } + + /** + * Imposta il valore della propriet� type. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setType(String value) { + this.type = value; + } + + /** + * Recupera il valore della propriet� writer. + * + * @return + * possible object is + * {@link String } + * + */ + public String getWriter() { + return writer; + } + + /** + * Imposta il valore della propriet� writer. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setWriter(String value) { + this.writer = value; + } + + /** + * Recupera il valore della propriet� year. + * + * @return + * possible object is + * {@link String } + * + */ + public String getYear() { + return year; + } + + /** + * Imposta il valore della propriet� year. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setYear(String value) { + this.year = value; + } + +} diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java new file mode 100644 index 0000000000..53e88961be --- /dev/null +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -0,0 +1,44 @@ +package com.baeldung.client; + +import com.baeldung.Movie; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + + +public interface ServicesInterface { + + + @GET + @Path("/getinfo") + @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + Movie movieByImdbID(@QueryParam("imdbID") String imdbID); + + + @POST + @Path("/addmovie") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + Response addMovie(Movie movie); + + + @PUT + @Path("/updatemovie") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + Response updateMovie(Movie movie); + + + @DELETE + @Path("/deletemovie") + Response deleteMovie(@QueryParam("imdbID") String imdbID); + + + @GET + @Path("/listmovies") + @Produces({"application/json"}) + List listMovies(); + +} diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java new file mode 100644 index 0000000000..d1973e7037 --- /dev/null +++ b/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java @@ -0,0 +1,94 @@ +package com.baeldung.server.service; + +import com.baeldung.Movie; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.Provider; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + + +@Path("/movies") +public class MovieCrudService { + + + private Map inventory = new HashMap(); + + @GET + @Path("/getinfo") + @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){ + + System.out.println("*** Calling getinfo ***"); + + Movie movie=new Movie(); + movie.setImdbID(imdbID); + return movie; + } + + @POST + @Path("/addmovie") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + public Response addMovie(Movie movie){ + + System.out.println("*** Calling addMovie ***"); + + if (null!=inventory.get(movie.getImdbID())){ + return Response.status(Response.Status.NOT_MODIFIED) + .entity("Movie is Already in the database.").build(); + } + inventory.put(movie.getImdbID(),movie); + + return Response.status(Response.Status.CREATED).build(); + } + + + @PUT + @Path("/updatemovie") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + public Response updateMovie(Movie movie){ + + System.out.println("*** Calling updateMovie ***"); + + if (null!=inventory.get(movie.getImdbID())){ + return Response.status(Response.Status.NOT_MODIFIED) + .entity("Movie is not in the database.\nUnable to Update").build(); + } + inventory.put(movie.getImdbID(),movie); + return Response.status(Response.Status.OK).build(); + + } + + + @DELETE + @Path("/deletemovie") + public Response deleteMovie(@QueryParam("imdbID") String imdbID){ + + System.out.println("*** Calling deleteMovie ***"); + + if (null==inventory.get(imdbID)){ + return Response.status(Response.Status.NOT_FOUND) + .entity("Movie is not in the database.\nUnable to Delete").build(); + } + + inventory.remove(imdbID); + return Response.status(Response.Status.OK).build(); + } + + @GET + @Path("/listmovies") + @Produces({"application/json"}) + public List listMovies(){ + + return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new)); + + } + + + +} diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java b/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java new file mode 100644 index 0000000000..16b6200ad1 --- /dev/null +++ b/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java @@ -0,0 +1,40 @@ +package com.baeldung.server.service; + + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * Created by Admin on 29/01/2016. + */ + + + +@ApplicationPath("/rest") +public class RestEasyServices extends Application { + + private Set singletons = new HashSet(); + + public RestEasyServices() { + singletons.add(new MovieCrudService()); + } + + @Override + public Set getSingletons() { + return singletons; + } + + @Override + public Set> getClasses() { + return super.getClasses(); + } + + @Override + public Map getProperties() { + return super.getProperties(); + } +} diff --git a/RestEasy Example/src/main/resources/schema1.xsd b/RestEasy Example/src/main/resources/schema1.xsd new file mode 100644 index 0000000000..0d74b7c366 --- /dev/null +++ b/RestEasy Example/src/main/resources/schema1.xsd @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/RestEasy Example/src/main/webapp/WEB-INF/classes/logback.xml b/RestEasy Example/src/main/webapp/WEB-INF/classes/logback.xml new file mode 100644 index 0000000000..d94e9f71ab --- /dev/null +++ b/RestEasy Example/src/main/webapp/WEB-INF/classes/logback.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml b/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml new file mode 100644 index 0000000000..84d75934a7 --- /dev/null +++ b/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RestEasy Example/src/main/webapp/WEB-INF/jboss-web.xml b/RestEasy Example/src/main/webapp/WEB-INF/jboss-web.xml new file mode 100644 index 0000000000..694bb71332 --- /dev/null +++ b/RestEasy Example/src/main/webapp/WEB-INF/jboss-web.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/RestEasy Example/src/main/webapp/WEB-INF/web.xml b/RestEasy Example/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..c66d3b56ae --- /dev/null +++ b/RestEasy Example/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,51 @@ + + + + RestEasy Example + + + + org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap + + + + RestEasy Example + + + webAppRootKey + RestEasyExample + + + + + + resteasy.servlet.mapping.prefix + /rest + + + + + resteasy-servlet + + org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher + + + javax.ws.rs.Application + com.baeldung.server.service.RestEasyServices + + + + + resteasy-servlet + /rest/* + + + + + index.html + + + + \ No newline at end of file diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java new file mode 100644 index 0000000000..e711233979 --- /dev/null +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java @@ -0,0 +1,50 @@ +package com.baeldung.server; + +import com.baeldung.Movie; +import com.baeldung.client.ServicesInterface; +import org.jboss.resteasy.client.jaxrs.ResteasyClient; +import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; +import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; + +import java.util.List; + +public class RestEasyClient { + + public static void main(String[] args) { + + Movie st = new Movie(); + st.setImdbID("12345"); + + /* + * Alternatively you can use this simple String to send + * instead of using a Student instance + * + * String jsonString = "{\"id\":12,\"firstName\":\"Catain\",\"lastName\":\"Hook\",\"age\":10}"; + */ + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target("http://localhost:8080/RestEasyTutorial/rest/movies/listmovies"); + + ServicesInterface simple = target.proxy(ServicesInterface.class); + final List movies = simple.listMovies(); + + /* + if (response.getStatus() != 200) { + throw new RuntimeException("Failed : HTTP error code : " + + response.getStatus()); + } + + System.out.println("Server response : \n"); + System.out.println(response.readEntity(String.class)); + + response.close(); +*/ + } catch (Exception e) { + + e.printStackTrace(); + + } + } + +} \ No newline at end of file diff --git a/RestEasy Example/src/test/resources/server/movies/transformer.json b/RestEasy Example/src/test/resources/server/movies/transformer.json new file mode 100644 index 0000000000..2154868265 --- /dev/null +++ b/RestEasy Example/src/test/resources/server/movies/transformer.json @@ -0,0 +1,22 @@ +{ + "title": "Transformers", + "year": "2007", + "rated": "PG-13", + "released": "03 Jul 2007", + "runtime": "144 min", + "genre": "Action, Adventure, Sci-Fi", + "director": "Michael Bay", + "writer": "Roberto Orci (screenplay), Alex Kurtzman (screenplay), John Rogers (story), Roberto Orci (story), Alex Kurtzman (story)", + "actors": "Shia LaBeouf, Megan Fox, Josh Duhamel, Tyrese Gibson", + "plot": "A long time ago, far away on the planet of Cybertron, a war is being waged between the noble Autobots (led by the wise Optimus Prime) and the devious Decepticons (commanded by the dreaded Megatron) for control over the Allspark, a mystical talisman that would grant unlimited power to whoever possesses it. The Autobots managed to smuggle the Allspark off the planet, but Megatron blasts off in search of it. He eventually tracks it to the planet of Earth (circa 1850), but his reckless desire for power sends him right into the Arctic Ocean, and the sheer cold forces him into a paralyzed state. His body is later found by Captain Archibald Witwicky, but before going into a comatose state Megatron uses the last of his energy to engrave into the Captain's glasses a map showing the location of the Allspark, and to send a transmission to Cybertron. Megatron is then carried away aboard the Captain's ship. A century later, Captain Witwicky's grandson Sam Witwicky (nicknamed Spike by his friends) buys his first car. To his shock, he discovers it to be Bumblebee, an Autobot in disguise who is to protect Spike, who possesses the Captain's glasses and the map engraved on them. But Bumblebee is not the only Transformer to have arrived on Earth - in the desert of Qatar, the Decepticons Blackout and Scorponok attack a U.S. military base, causing the Pentagon to send their special Sector Seven agents to capture all \"specimens of this alien race.\" Spike and his girlfriend Mikaela find themselves in the midst of a grand battle between the Autobots and the Decepticons, stretching from Hoover Dam all the way to Los Angeles. Meanwhile, deep inside Hoover Dam, the cryogenically stored body of Megatron awakens.", + "language": "English, Spanish", + "country": "USA", + "awards": "Nominated for 3 Oscars. Another 18 wins & 40 nominations.", + "poster": "http://ia.media-imdb.com/images/M/MV5BMTQwNjU5MzUzNl5BMl5BanBnXkFtZTYwMzc1MTI3._V1_SX300.jpg", + "metascore": "61", + "imdbRating": "7.1", + "imdbVotes": "492,225", + "imdbID": "tt0418279", + "Type": "movie", + "response": "True" +} \ No newline at end of file From 663def8e2c00957b8913afcb118bde494c85f987 Mon Sep 17 00:00:00 2001 From: Giuseppe Bueti Date: Sat, 30 Jan 2016 20:39:28 +0100 Subject: [PATCH 73/84] Added testCase for List All Movies and Add a Movie --- RestEasy Example/pom.xml | 29 +++++ .../baeldung/client/ServicesInterface.java | 6 +- .../java/com/baeldung/{ => model}/Movie.java | 45 +++++++- .../{service => }/MovieCrudService.java | 5 +- .../{service => }/RestEasyServices.java | 2 +- .../com/baeldung/server/RestEasyClient.java | 104 ++++++++++++++---- .../com/baeldung/server/movies/batman.json | 22 ++++ .../baeldung}/server/movies/transformer.json | 2 +- 8 files changed, 184 insertions(+), 31 deletions(-) rename RestEasy Example/src/main/java/com/baeldung/{ => model}/Movie.java (86%) rename RestEasy Example/src/main/java/com/baeldung/server/{service => }/MovieCrudService.java (96%) rename RestEasy Example/src/main/java/com/baeldung/server/{service => }/RestEasyServices.java (95%) create mode 100644 RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json rename RestEasy Example/src/test/resources/{ => com/baeldung}/server/movies/transformer.json (99%) diff --git a/RestEasy Example/pom.xml b/RestEasy Example/pom.xml index b16c5e8267..8dabfc863b 100644 --- a/RestEasy Example/pom.xml +++ b/RestEasy Example/pom.xml @@ -85,6 +85,35 @@ ${resteasy.scope} + + + + junit + junit + 4.4 + + + + commons-io + commons-io + 2.4 + + + + com.fasterxml.jackson.core + jackson-core + 2.7.0 + + + + com.fasterxml.jackson.core + jackson-annotations + 2.7.0 + + + + + diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java index 53e88961be..2585c32438 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -1,15 +1,13 @@ package com.baeldung.client; -import com.baeldung.Movie; +import com.baeldung.model.Movie; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; - +@Path("/movies") public interface ServicesInterface { diff --git a/RestEasy Example/src/main/java/com/baeldung/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java similarity index 86% rename from RestEasy Example/src/main/java/com/baeldung/Movie.java rename to RestEasy Example/src/main/java/com/baeldung/model/Movie.java index c0041d2e95..052ba081c1 100644 --- a/RestEasy Example/src/main/java/com/baeldung/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -1,5 +1,5 @@ -package com.baeldung; +package com.baeldung.model; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; @@ -532,4 +532,47 @@ public class Movie { this.year = value; } + @Override + public String toString() { + return "Movie{" + + "actors='" + actors + '\'' + + ", awards='" + awards + '\'' + + ", country='" + country + '\'' + + ", director='" + director + '\'' + + ", genre='" + genre + '\'' + + ", imdbID='" + imdbID + '\'' + + ", imdbRating='" + imdbRating + '\'' + + ", imdbVotes='" + imdbVotes + '\'' + + ", language='" + language + '\'' + + ", metascore='" + metascore + '\'' + + ", poster='" + poster + '\'' + + ", rated='" + rated + '\'' + + ", released='" + released + '\'' + + ", response='" + response + '\'' + + ", runtime='" + runtime + '\'' + + ", title='" + title + '\'' + + ", type='" + type + '\'' + + ", writer='" + writer + '\'' + + ", year='" + year + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Movie movie = (Movie) o; + + if (imdbID != null ? !imdbID.equals(movie.imdbID) : movie.imdbID != null) return false; + return title != null ? title.equals(movie.title) : movie.title == null; + + } + + @Override + public int hashCode() { + int result = imdbID != null ? imdbID.hashCode() : 0; + result = 31 * result + (title != null ? title.hashCode() : 0); + return result; + } } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java similarity index 96% rename from RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java rename to RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index d1973e7037..60e0121966 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -1,11 +1,10 @@ -package com.baeldung.server.service; +package com.baeldung.server; -import com.baeldung.Movie; +import com.baeldung.model.Movie; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import javax.ws.rs.ext.Provider; import java.util.ArrayList; import java.util.HashMap; import java.util.List; diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java b/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java similarity index 95% rename from RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java rename to RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java index 16b6200ad1..8c57d2c9b4 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java @@ -1,4 +1,4 @@ -package com.baeldung.server.service; +package com.baeldung.server; import javax.ws.rs.ApplicationPath; diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java index e711233979..c77f494862 100644 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java @@ -1,49 +1,111 @@ package com.baeldung.server; -import com.baeldung.Movie; +import com.baeldung.model.Movie; import com.baeldung.client.ServicesInterface; +import org.apache.commons.io.IOUtils; +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; import org.jboss.resteasy.client.jaxrs.ResteasyClient; import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import javax.naming.NamingException; +import javax.ws.rs.core.Link; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.List; +import java.util.Locale; public class RestEasyClient { - public static void main(String[] args) { - Movie st = new Movie(); - st.setImdbID("12345"); + Movie transformerMovie=null; + Movie batmanMovie=null; + ObjectMapper jsonMapper=null; - /* - * Alternatively you can use this simple String to send - * instead of using a Student instance - * - * String jsonString = "{\"id\":12,\"firstName\":\"Catain\",\"lastName\":\"Hook\",\"age\":10}"; - */ + @BeforeClass + public static void loadMovieInventory(){ + + + + } + + @Before + public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { + + + jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); + jsonMapper.setDateFormat(sdf); + + try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/transformer.json")) { + String transformerMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + transformerMovie = jsonMapper.readValue(transformerMovieAsString, Movie.class); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/batman.json")) { + String batmanMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + } + + @Test + public void testListAllMovies() { try { ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target("http://localhost:8080/RestEasyTutorial/rest/movies/listmovies"); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + final List movies = simple.listMovies(); + System.out.println(movies); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + + @Test + public void testAddMovie() { + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); ServicesInterface simple = target.proxy(ServicesInterface.class); - final List movies = simple.listMovies(); + final Response moviesResponse = simple.addMovie(batmanMovie); - /* - if (response.getStatus() != 200) { + if (moviesResponse.getStatus() != 201) { + System.out.println(moviesResponse.readEntity(String.class)); throw new RuntimeException("Failed : HTTP error code : " - + response.getStatus()); + + moviesResponse.getStatus()); } - System.out.println("Server response : \n"); - System.out.println(response.readEntity(String.class)); + moviesResponse.close(); - response.close(); -*/ } catch (Exception e) { - e.printStackTrace(); - } } diff --git a/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json b/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json new file mode 100644 index 0000000000..28061d5bf9 --- /dev/null +++ b/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json @@ -0,0 +1,22 @@ +{ + "title": "Batman", + "year": "1989", + "rated": "PG-13", + "released": "23 Jun 1989", + "runtime": "126 min", + "genre": "Action, Adventure", + "director": "Tim Burton", + "writer": "Bob Kane (Batman characters), Sam Hamm (story), Sam Hamm (screenplay), Warren Skaaren (screenplay)", + "actors": "Michael Keaton, Jack Nicholson, Kim Basinger, Robert Wuhl", + "plot": "The Dark Knight of Gotham City begins his war on crime with his first major enemy being the clownishly homicidal Joker.", + "language": "English, French", + "country": "USA, UK", + "awards": "Won 1 Oscar. Another 9 wins & 22 nominations.", + "poster": "http://ia.media-imdb.com/images/M/MV5BMTYwNjAyODIyMF5BMl5BanBnXkFtZTYwNDMwMDk2._V1_SX300.jpg", + "metascore": "66", + "imdbRating": "7.6", + "imdbVotes": "256,000", + "imdbID": "tt0096895", + "type": "movie", + "response": "True" +} \ No newline at end of file diff --git a/RestEasy Example/src/test/resources/server/movies/transformer.json b/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json similarity index 99% rename from RestEasy Example/src/test/resources/server/movies/transformer.json rename to RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json index 2154868265..a3b033a8ba 100644 --- a/RestEasy Example/src/test/resources/server/movies/transformer.json +++ b/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json @@ -17,6 +17,6 @@ "imdbRating": "7.1", "imdbVotes": "492,225", "imdbID": "tt0418279", - "Type": "movie", + "type": "movie", "response": "True" } \ No newline at end of file From b39ec7d76ee55ffe0165aa82ff23f2da3bd8475c Mon Sep 17 00:00:00 2001 From: Giuseppe Bueti Date: Sun, 31 Jan 2016 11:05:11 +0100 Subject: [PATCH 74/84] Added testCase for all Services --- RestEasy Example/pom.xml | 15 -- .../baeldung/client/ServicesInterface.java | 10 +- .../com/baeldung/server/MovieCrudService.java | 15 +- .../src/main/webapp/WEB-INF/web.xml | 2 +- .../com/baeldung/server/RestEasyClient.java | 112 ----------- .../baeldung/server/RestEasyClientTest.java | 189 ++++++++++++++++++ 6 files changed, 206 insertions(+), 137 deletions(-) delete mode 100644 RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java create mode 100644 RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java diff --git a/RestEasy Example/pom.xml b/RestEasy Example/pom.xml index 8dabfc863b..6935238d91 100644 --- a/RestEasy Example/pom.xml +++ b/RestEasy Example/pom.xml @@ -99,21 +99,6 @@ 2.4 - - com.fasterxml.jackson.core - jackson-core - 2.7.0 - - - - com.fasterxml.jackson.core - jackson-annotations - 2.7.0 - - - - - diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java index 2585c32438..7efed546d8 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -17,6 +17,12 @@ public interface ServicesInterface { Movie movieByImdbID(@QueryParam("imdbID") String imdbID); + @GET + @Path("/listmovies") + @Produces({"application/json"}) + List listMovies(); + + @POST @Path("/addmovie") @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @@ -34,9 +40,5 @@ public interface ServicesInterface { Response deleteMovie(@QueryParam("imdbID") String imdbID); - @GET - @Path("/listmovies") - @Produces({"application/json"}) - List listMovies(); } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index 60e0121966..18366e2faa 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -18,18 +18,21 @@ public class MovieCrudService { private Map inventory = new HashMap(); + @GET @Path("/getinfo") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){ - System.out.println("*** Calling getinfo ***"); + System.out.println("*** Calling getinfo for a given ImdbID***"); + + if(inventory.containsKey(imdbID)){ + return inventory.get(imdbID); + }else return null; - Movie movie=new Movie(); - movie.setImdbID(imdbID); - return movie; } + @POST @Path("/addmovie") @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @@ -41,6 +44,7 @@ public class MovieCrudService { return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is Already in the database.").build(); } + inventory.put(movie.getImdbID(),movie); return Response.status(Response.Status.CREATED).build(); @@ -54,7 +58,7 @@ public class MovieCrudService { System.out.println("*** Calling updateMovie ***"); - if (null!=inventory.get(movie.getImdbID())){ + if (null==inventory.get(movie.getImdbID())){ return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is not in the database.\nUnable to Update").build(); } @@ -79,6 +83,7 @@ public class MovieCrudService { return Response.status(Response.Status.OK).build(); } + @GET @Path("/listmovies") @Produces({"application/json"}) diff --git a/RestEasy Example/src/main/webapp/WEB-INF/web.xml b/RestEasy Example/src/main/webapp/WEB-INF/web.xml index c66d3b56ae..ab3bc1aa83 100644 --- a/RestEasy Example/src/main/webapp/WEB-INF/web.xml +++ b/RestEasy Example/src/main/webapp/WEB-INF/web.xml @@ -33,7 +33,7 @@ javax.ws.rs.Application - com.baeldung.server.service.RestEasyServices + com.baeldung.server.RestEasyServices diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java deleted file mode 100644 index c77f494862..0000000000 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.baeldung.server; - -import com.baeldung.model.Movie; -import com.baeldung.client.ServicesInterface; -import org.apache.commons.io.IOUtils; -import org.codehaus.jackson.map.DeserializationConfig; -import org.codehaus.jackson.map.ObjectMapper; -import org.jboss.resteasy.client.jaxrs.ResteasyClient; -import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; -import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import javax.naming.NamingException; -import javax.ws.rs.core.Link; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriBuilder; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileReader; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.List; -import java.util.Locale; - -public class RestEasyClient { - - - Movie transformerMovie=null; - Movie batmanMovie=null; - ObjectMapper jsonMapper=null; - - @BeforeClass - public static void loadMovieInventory(){ - - - - } - - @Before - public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { - - - jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); - jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); - SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); - jsonMapper.setDateFormat(sdf); - - try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/transformer.json")) { - String transformerMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); - transformerMovie = jsonMapper.readValue(transformerMovieAsString, Movie.class); - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException("Test is going to die ...", e); - } - - try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/batman.json")) { - String batmanMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); - batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); - - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException("Test is going to die ...", e); - } - - } - - @Test - public void testListAllMovies() { - - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); - - final List movies = simple.listMovies(); - System.out.println(movies); - - } catch (Exception e) { - e.printStackTrace(); - } - } - - - - @Test - public void testAddMovie() { - - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - - ServicesInterface simple = target.proxy(ServicesInterface.class); - final Response moviesResponse = simple.addMovie(batmanMovie); - - if (moviesResponse.getStatus() != 201) { - System.out.println(moviesResponse.readEntity(String.class)); - throw new RuntimeException("Failed : HTTP error code : " - + moviesResponse.getStatus()); - } - - moviesResponse.close(); - - } catch (Exception e) { - e.printStackTrace(); - } - } - -} \ No newline at end of file diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java new file mode 100644 index 0000000000..fb4205bcd7 --- /dev/null +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java @@ -0,0 +1,189 @@ +package com.baeldung.server; + +import com.baeldung.model.Movie; +import com.baeldung.client.ServicesInterface; +import org.apache.commons.io.IOUtils; +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; +import org.jboss.resteasy.client.jaxrs.ResteasyClient; +import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; +import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import javax.naming.NamingException; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; +import java.util.List; +import java.util.Locale; + +public class RestEasyClientTest { + + + Movie transformerMovie=null; + Movie batmanMovie=null; + ObjectMapper jsonMapper=null; + + @BeforeClass + public static void loadMovieInventory(){ + + + + } + + @Before + public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { + + + jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); + jsonMapper.setDateFormat(sdf); + + try (InputStream inputStream = new RestEasyClientTest().getClass().getResourceAsStream("./movies/transformer.json")) { + String transformerMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + transformerMovie = jsonMapper.readValue(transformerMovieAsString, Movie.class); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + try (InputStream inputStream = new RestEasyClientTest().getClass().getResourceAsStream("./movies/batman.json")) { + String batmanMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + } + + + @Test + public void testListAllMovies() { + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(transformerMovie); + moviesResponse.close(); + moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + + final List movies = simple.listMovies(); + System.out.println(movies); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testMovieByImdbID() { + + String transformerImdbId="tt0418279"; + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(transformerMovie); + moviesResponse.close(); + + final Movie movies = simple.movieByImdbID(transformerImdbId); + System.out.println(movies); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testAddMovie() { + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = simple.addMovie(transformerMovie); + + if (moviesResponse.getStatus() != Response.Status.CREATED.getStatusCode()) { + //System.out.println(moviesResponse.readEntity(String.class)); + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); + } + moviesResponse.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testDeleteMovie() { + + String transformerImdbId="tt0418279"; + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = simple.deleteMovie(transformerImdbId); + moviesResponse.close(); + + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + System.out.println(moviesResponse.readEntity(String.class)); + throw new RuntimeException("Failed : HTTP error code : " + moviesResponse.getStatus()); + } + + moviesResponse.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Test + public void testUpdateMovie() { + + try { + + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + batmanMovie.setImdbVotes("300,000"); + moviesResponse = simple.updateMovie(batmanMovie); + + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + //System.out.println(moviesResponse.readEntity(String.class)); + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); + } + + moviesResponse.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file From 5cdf7185a4eed764473b76b365f14c2737f7c0bb Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sun, 7 Feb 2016 20:29:30 +0100 Subject: [PATCH 75/84] Minor Bugfix --- .../baeldung/client/ServicesInterface.java | 4 +- .../main/java/com/baeldung/model/Movie.java | 22 +-- .../com/baeldung/server/MovieCrudService.java | 12 +- .../src/main/webapp/WEB-INF/web.xml | 37 +---- .../java/baeldung/client/RestEasyClient.java | 7 + .../baeldung/server/RestEasyClientTest.java | 149 +++++++----------- 6 files changed, 81 insertions(+), 150 deletions(-) create mode 100644 RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java index 7efed546d8..749cabc757 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -14,7 +14,7 @@ public interface ServicesInterface { @GET @Path("/getinfo") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - Movie movieByImdbID(@QueryParam("imdbID") String imdbID); + Movie movieByImdbId(@QueryParam("imdbId") String imdbId); @GET @@ -37,7 +37,7 @@ public interface ServicesInterface { @DELETE @Path("/deletemovie") - Response deleteMovie(@QueryParam("imdbID") String imdbID); + Response deleteMovie(@QueryParam("imdbId") String imdbID); diff --git a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java index 052ba081c1..a2b2bd5250 100644 --- a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -13,7 +13,7 @@ import javax.xml.bind.annotation.XmlType; "country", "director", "genre", - "imdbID", + "imdbId", "imdbRating", "imdbVotes", "language", @@ -36,7 +36,7 @@ public class Movie { protected String country; protected String director; protected String genre; - protected String imdbID; + protected String imdbId; protected String imdbRating; protected String imdbVotes; protected String language; @@ -173,27 +173,27 @@ public class Movie { } /** - * Recupera il valore della propriet� imdbID. + * Recupera il valore della propriet� imdbId. * * @return * possible object is * {@link String } * */ - public String getImdbID() { - return imdbID; + public String getImdbId() { + return imdbId; } /** - * Imposta il valore della propriet� imdbID. + * Imposta il valore della propriet� imdbId. * * @param value * allowed object is * {@link String } * */ - public void setImdbID(String value) { - this.imdbID = value; + public void setImdbId(String value) { + this.imdbId = value; } /** @@ -540,7 +540,7 @@ public class Movie { ", country='" + country + '\'' + ", director='" + director + '\'' + ", genre='" + genre + '\'' + - ", imdbID='" + imdbID + '\'' + + ", imdbId='" + imdbId + '\'' + ", imdbRating='" + imdbRating + '\'' + ", imdbVotes='" + imdbVotes + '\'' + ", language='" + language + '\'' + @@ -564,14 +564,14 @@ public class Movie { Movie movie = (Movie) o; - if (imdbID != null ? !imdbID.equals(movie.imdbID) : movie.imdbID != null) return false; + if (imdbId != null ? !imdbId.equals(movie.imdbId) : movie.imdbId != null) return false; return title != null ? title.equals(movie.title) : movie.title == null; } @Override public int hashCode() { - int result = imdbID != null ? imdbID.hashCode() : 0; + int result = imdbId != null ? imdbId.hashCode() : 0; result = 31 * result + (title != null ? title.hashCode() : 0); return result; } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index 18366e2faa..29226aa0e0 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -22,7 +22,7 @@ public class MovieCrudService { @GET @Path("/getinfo") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){ + public Movie movieByImdbID(@QueryParam("imdbId") String imdbID){ System.out.println("*** Calling getinfo for a given ImdbID***"); @@ -40,12 +40,12 @@ public class MovieCrudService { System.out.println("*** Calling addMovie ***"); - if (null!=inventory.get(movie.getImdbID())){ + if (null!=inventory.get(movie.getImdbId())){ return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is Already in the database.").build(); } - inventory.put(movie.getImdbID(),movie); + inventory.put(movie.getImdbId(),movie); return Response.status(Response.Status.CREATED).build(); } @@ -58,11 +58,11 @@ public class MovieCrudService { System.out.println("*** Calling updateMovie ***"); - if (null==inventory.get(movie.getImdbID())){ + if (null==inventory.get(movie.getImdbId())){ return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is not in the database.\nUnable to Update").build(); } - inventory.put(movie.getImdbID(),movie); + inventory.put(movie.getImdbId(),movie); return Response.status(Response.Status.OK).build(); } @@ -70,7 +70,7 @@ public class MovieCrudService { @DELETE @Path("/deletemovie") - public Response deleteMovie(@QueryParam("imdbID") String imdbID){ + public Response deleteMovie(@QueryParam("imdbId") String imdbID){ System.out.println("*** Calling deleteMovie ***"); diff --git a/RestEasy Example/src/main/webapp/WEB-INF/web.xml b/RestEasy Example/src/main/webapp/WEB-INF/web.xml index ab3bc1aa83..f70fdf7975 100644 --- a/RestEasy Example/src/main/webapp/WEB-INF/web.xml +++ b/RestEasy Example/src/main/webapp/WEB-INF/web.xml @@ -5,47 +5,12 @@ RestEasy Example - - - org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap - - - - RestEasy Example - - - webAppRootKey - RestEasyExample - - - - + resteasy.servlet.mapping.prefix /rest - - resteasy-servlet - - org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher - - - javax.ws.rs.Application - com.baeldung.server.RestEasyServices - - - - - resteasy-servlet - /rest/* - - - - - index.html - - \ No newline at end of file diff --git a/RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java b/RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java new file mode 100644 index 0000000000..b474b3d4f8 --- /dev/null +++ b/RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java @@ -0,0 +1,7 @@ +package baeldung.client; + +/** + * Created by Admin on 29/01/2016. + */ +public class RestEasyClient { +} diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java index fb4205bcd7..b6a2e2a0c1 100644 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java @@ -1,7 +1,7 @@ package com.baeldung.server; -import com.baeldung.model.Movie; import com.baeldung.client.ServicesInterface; +import com.baeldung.model.Movie; import org.apache.commons.io.IOUtils; import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; @@ -9,7 +9,6 @@ import org.jboss.resteasy.client.jaxrs.ResteasyClient; import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import javax.naming.NamingException; @@ -23,22 +22,13 @@ import java.util.Locale; public class RestEasyClientTest { - Movie transformerMovie=null; Movie batmanMovie=null; ObjectMapper jsonMapper=null; - @BeforeClass - public static void loadMovieInventory(){ - - - - } - @Before public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { - jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); @@ -57,133 +47,102 @@ public class RestEasyClientTest { batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); } catch (Exception e) { - e.printStackTrace(); throw new RuntimeException("Test is going to die ...", e); } - } - @Test public void testListAllMovies() { - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); - Response moviesResponse = simple.addMovie(transformerMovie); - moviesResponse.close(); - moviesResponse = simple.addMovie(batmanMovie); - moviesResponse.close(); + Response moviesResponse = simple.addMovie(transformerMovie); + moviesResponse.close(); + moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); - final List movies = simple.listMovies(); - System.out.println(movies); - - } catch (Exception e) { - e.printStackTrace(); - } + List movies = simple.listMovies(); + System.out.println(movies); } - @Test - public void testMovieByImdbID() { + public void testMovieByImdbId() { String transformerImdbId="tt0418279"; - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); - Response moviesResponse = simple.addMovie(transformerMovie); - moviesResponse.close(); + Response moviesResponse = simple.addMovie(transformerMovie); + moviesResponse.close(); - final Movie movies = simple.movieByImdbID(transformerImdbId); - System.out.println(movies); - - } catch (Exception e) { - e.printStackTrace(); - } + Movie movies = simple.movieByImdbId(transformerImdbId); + System.out.println(movies); } @Test public void testAddMovie() { - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); - Response moviesResponse = simple.addMovie(batmanMovie); - moviesResponse.close(); - moviesResponse = simple.addMovie(transformerMovie); + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = simple.addMovie(transformerMovie); - if (moviesResponse.getStatus() != Response.Status.CREATED.getStatusCode()) { - //System.out.println(moviesResponse.readEntity(String.class)); - System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); - } - moviesResponse.close(); - - } catch (Exception e) { - e.printStackTrace(); + if (moviesResponse.getStatus() != Response.Status.CREATED.getStatusCode()) { + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); } + + moviesResponse.close(); + System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); } @Test - public void testDeleteMovie() { + public void testDeleteMovi1e() { - String transformerImdbId="tt0418279"; + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = simple.deleteMovie(batmanMovie.getImdbId()); - Response moviesResponse = simple.addMovie(batmanMovie); - moviesResponse.close(); - moviesResponse = simple.deleteMovie(transformerImdbId); - moviesResponse.close(); - - if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { - System.out.println(moviesResponse.readEntity(String.class)); - throw new RuntimeException("Failed : HTTP error code : " + moviesResponse.getStatus()); - } - - moviesResponse.close(); - - } catch (Exception e) { - e.printStackTrace(); + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + System.out.println(moviesResponse.readEntity(String.class)); + throw new RuntimeException("Failed : HTTP error code : " + moviesResponse.getStatus()); } + + moviesResponse.close(); + System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); } @Test public void testUpdateMovie() { - try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + batmanMovie.setImdbVotes("300,000"); + moviesResponse = simple.updateMovie(batmanMovie); - Response moviesResponse = simple.addMovie(batmanMovie); - moviesResponse.close(); - batmanMovie.setImdbVotes("300,000"); - moviesResponse = simple.updateMovie(batmanMovie); - - if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { - //System.out.println(moviesResponse.readEntity(String.class)); - System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); - } - - moviesResponse.close(); - - } catch (Exception e) { - e.printStackTrace(); + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); } + + moviesResponse.close(); + System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); } } \ No newline at end of file From 9b5818262decbcfcfc302ab15c5f3347dbaeecfd Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sat, 30 Jan 2016 17:48:56 +0100 Subject: [PATCH 76/84] RestEasy Tutorial, CRUD Services example --- .../src/main/java/com/baeldung/Movie.java | 535 ++++++++++++++++++ .../server/service/MovieCrudService.java | 94 +++ .../server/service/RestEasyServices.java | 40 ++ .../com/baeldung/server/RestEasyClient.java | 50 ++ .../resources/server/movies/transformer.json | 22 + 5 files changed, 741 insertions(+) create mode 100644 RestEasy Example/src/main/java/com/baeldung/Movie.java create mode 100644 RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java create mode 100644 RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java create mode 100644 RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java create mode 100644 RestEasy Example/src/test/resources/server/movies/transformer.json diff --git a/RestEasy Example/src/main/java/com/baeldung/Movie.java b/RestEasy Example/src/main/java/com/baeldung/Movie.java new file mode 100644 index 0000000000..c0041d2e95 --- /dev/null +++ b/RestEasy Example/src/main/java/com/baeldung/Movie.java @@ -0,0 +1,535 @@ + +package com.baeldung; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlType; + + +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "movie", propOrder = { + "actors", + "awards", + "country", + "director", + "genre", + "imdbID", + "imdbRating", + "imdbVotes", + "language", + "metascore", + "plot", + "poster", + "rated", + "released", + "response", + "runtime", + "title", + "type", + "writer", + "year" +}) +public class Movie { + + protected String actors; + protected String awards; + protected String country; + protected String director; + protected String genre; + protected String imdbID; + protected String imdbRating; + protected String imdbVotes; + protected String language; + protected String metascore; + protected String plot; + protected String poster; + protected String rated; + protected String released; + protected String response; + protected String runtime; + protected String title; + protected String type; + protected String writer; + protected String year; + + /** + * Recupera il valore della propriet� actors. + * + * @return + * possible object is + * {@link String } + * + */ + public String getActors() { + return actors; + } + + /** + * Imposta il valore della propriet� actors. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setActors(String value) { + this.actors = value; + } + + /** + * Recupera il valore della propriet� awards. + * + * @return + * possible object is + * {@link String } + * + */ + public String getAwards() { + return awards; + } + + /** + * Imposta il valore della propriet� awards. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setAwards(String value) { + this.awards = value; + } + + /** + * Recupera il valore della propriet� country. + * + * @return + * possible object is + * {@link String } + * + */ + public String getCountry() { + return country; + } + + /** + * Imposta il valore della propriet� country. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setCountry(String value) { + this.country = value; + } + + /** + * Recupera il valore della propriet� director. + * + * @return + * possible object is + * {@link String } + * + */ + public String getDirector() { + return director; + } + + /** + * Imposta il valore della propriet� director. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setDirector(String value) { + this.director = value; + } + + /** + * Recupera il valore della propriet� genre. + * + * @return + * possible object is + * {@link String } + * + */ + public String getGenre() { + return genre; + } + + /** + * Imposta il valore della propriet� genre. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setGenre(String value) { + this.genre = value; + } + + /** + * Recupera il valore della propriet� imdbID. + * + * @return + * possible object is + * {@link String } + * + */ + public String getImdbID() { + return imdbID; + } + + /** + * Imposta il valore della propriet� imdbID. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setImdbID(String value) { + this.imdbID = value; + } + + /** + * Recupera il valore della propriet� imdbRating. + * + * @return + * possible object is + * {@link String } + * + */ + public String getImdbRating() { + return imdbRating; + } + + /** + * Imposta il valore della propriet� imdbRating. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setImdbRating(String value) { + this.imdbRating = value; + } + + /** + * Recupera il valore della propriet� imdbVotes. + * + * @return + * possible object is + * {@link String } + * + */ + public String getImdbVotes() { + return imdbVotes; + } + + /** + * Imposta il valore della propriet� imdbVotes. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setImdbVotes(String value) { + this.imdbVotes = value; + } + + /** + * Recupera il valore della propriet� language. + * + * @return + * possible object is + * {@link String } + * + */ + public String getLanguage() { + return language; + } + + /** + * Imposta il valore della propriet� language. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setLanguage(String value) { + this.language = value; + } + + /** + * Recupera il valore della propriet� metascore. + * + * @return + * possible object is + * {@link String } + * + */ + public String getMetascore() { + return metascore; + } + + /** + * Imposta il valore della propriet� metascore. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setMetascore(String value) { + this.metascore = value; + } + + /** + * Recupera il valore della propriet� plot. + * + * @return + * possible object is + * {@link String } + * + */ + public String getPlot() { + return plot; + } + + /** + * Imposta il valore della propriet� plot. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setPlot(String value) { + this.plot = value; + } + + /** + * Recupera il valore della propriet� poster. + * + * @return + * possible object is + * {@link String } + * + */ + public String getPoster() { + return poster; + } + + /** + * Imposta il valore della propriet� poster. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setPoster(String value) { + this.poster = value; + } + + /** + * Recupera il valore della propriet� rated. + * + * @return + * possible object is + * {@link String } + * + */ + public String getRated() { + return rated; + } + + /** + * Imposta il valore della propriet� rated. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setRated(String value) { + this.rated = value; + } + + /** + * Recupera il valore della propriet� released. + * + * @return + * possible object is + * {@link String } + * + */ + public String getReleased() { + return released; + } + + /** + * Imposta il valore della propriet� released. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setReleased(String value) { + this.released = value; + } + + /** + * Recupera il valore della propriet� response. + * + * @return + * possible object is + * {@link String } + * + */ + public String getResponse() { + return response; + } + + /** + * Imposta il valore della propriet� response. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setResponse(String value) { + this.response = value; + } + + /** + * Recupera il valore della propriet� runtime. + * + * @return + * possible object is + * {@link String } + * + */ + public String getRuntime() { + return runtime; + } + + /** + * Imposta il valore della propriet� runtime. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setRuntime(String value) { + this.runtime = value; + } + + /** + * Recupera il valore della propriet� title. + * + * @return + * possible object is + * {@link String } + * + */ + public String getTitle() { + return title; + } + + /** + * Imposta il valore della propriet� title. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setTitle(String value) { + this.title = value; + } + + /** + * Recupera il valore della propriet� type. + * + * @return + * possible object is + * {@link String } + * + */ + public String getType() { + return type; + } + + /** + * Imposta il valore della propriet� type. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setType(String value) { + this.type = value; + } + + /** + * Recupera il valore della propriet� writer. + * + * @return + * possible object is + * {@link String } + * + */ + public String getWriter() { + return writer; + } + + /** + * Imposta il valore della propriet� writer. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setWriter(String value) { + this.writer = value; + } + + /** + * Recupera il valore della propriet� year. + * + * @return + * possible object is + * {@link String } + * + */ + public String getYear() { + return year; + } + + /** + * Imposta il valore della propriet� year. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setYear(String value) { + this.year = value; + } + +} diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java new file mode 100644 index 0000000000..d1973e7037 --- /dev/null +++ b/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java @@ -0,0 +1,94 @@ +package com.baeldung.server.service; + +import com.baeldung.Movie; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.Provider; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + + +@Path("/movies") +public class MovieCrudService { + + + private Map inventory = new HashMap(); + + @GET + @Path("/getinfo") + @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){ + + System.out.println("*** Calling getinfo ***"); + + Movie movie=new Movie(); + movie.setImdbID(imdbID); + return movie; + } + + @POST + @Path("/addmovie") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + public Response addMovie(Movie movie){ + + System.out.println("*** Calling addMovie ***"); + + if (null!=inventory.get(movie.getImdbID())){ + return Response.status(Response.Status.NOT_MODIFIED) + .entity("Movie is Already in the database.").build(); + } + inventory.put(movie.getImdbID(),movie); + + return Response.status(Response.Status.CREATED).build(); + } + + + @PUT + @Path("/updatemovie") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + public Response updateMovie(Movie movie){ + + System.out.println("*** Calling updateMovie ***"); + + if (null!=inventory.get(movie.getImdbID())){ + return Response.status(Response.Status.NOT_MODIFIED) + .entity("Movie is not in the database.\nUnable to Update").build(); + } + inventory.put(movie.getImdbID(),movie); + return Response.status(Response.Status.OK).build(); + + } + + + @DELETE + @Path("/deletemovie") + public Response deleteMovie(@QueryParam("imdbID") String imdbID){ + + System.out.println("*** Calling deleteMovie ***"); + + if (null==inventory.get(imdbID)){ + return Response.status(Response.Status.NOT_FOUND) + .entity("Movie is not in the database.\nUnable to Delete").build(); + } + + inventory.remove(imdbID); + return Response.status(Response.Status.OK).build(); + } + + @GET + @Path("/listmovies") + @Produces({"application/json"}) + public List listMovies(){ + + return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new)); + + } + + + +} diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java b/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java new file mode 100644 index 0000000000..16b6200ad1 --- /dev/null +++ b/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java @@ -0,0 +1,40 @@ +package com.baeldung.server.service; + + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * Created by Admin on 29/01/2016. + */ + + + +@ApplicationPath("/rest") +public class RestEasyServices extends Application { + + private Set singletons = new HashSet(); + + public RestEasyServices() { + singletons.add(new MovieCrudService()); + } + + @Override + public Set getSingletons() { + return singletons; + } + + @Override + public Set> getClasses() { + return super.getClasses(); + } + + @Override + public Map getProperties() { + return super.getProperties(); + } +} diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java new file mode 100644 index 0000000000..e711233979 --- /dev/null +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java @@ -0,0 +1,50 @@ +package com.baeldung.server; + +import com.baeldung.Movie; +import com.baeldung.client.ServicesInterface; +import org.jboss.resteasy.client.jaxrs.ResteasyClient; +import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; +import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; + +import java.util.List; + +public class RestEasyClient { + + public static void main(String[] args) { + + Movie st = new Movie(); + st.setImdbID("12345"); + + /* + * Alternatively you can use this simple String to send + * instead of using a Student instance + * + * String jsonString = "{\"id\":12,\"firstName\":\"Catain\",\"lastName\":\"Hook\",\"age\":10}"; + */ + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target("http://localhost:8080/RestEasyTutorial/rest/movies/listmovies"); + + ServicesInterface simple = target.proxy(ServicesInterface.class); + final List movies = simple.listMovies(); + + /* + if (response.getStatus() != 200) { + throw new RuntimeException("Failed : HTTP error code : " + + response.getStatus()); + } + + System.out.println("Server response : \n"); + System.out.println(response.readEntity(String.class)); + + response.close(); +*/ + } catch (Exception e) { + + e.printStackTrace(); + + } + } + +} \ No newline at end of file diff --git a/RestEasy Example/src/test/resources/server/movies/transformer.json b/RestEasy Example/src/test/resources/server/movies/transformer.json new file mode 100644 index 0000000000..2154868265 --- /dev/null +++ b/RestEasy Example/src/test/resources/server/movies/transformer.json @@ -0,0 +1,22 @@ +{ + "title": "Transformers", + "year": "2007", + "rated": "PG-13", + "released": "03 Jul 2007", + "runtime": "144 min", + "genre": "Action, Adventure, Sci-Fi", + "director": "Michael Bay", + "writer": "Roberto Orci (screenplay), Alex Kurtzman (screenplay), John Rogers (story), Roberto Orci (story), Alex Kurtzman (story)", + "actors": "Shia LaBeouf, Megan Fox, Josh Duhamel, Tyrese Gibson", + "plot": "A long time ago, far away on the planet of Cybertron, a war is being waged between the noble Autobots (led by the wise Optimus Prime) and the devious Decepticons (commanded by the dreaded Megatron) for control over the Allspark, a mystical talisman that would grant unlimited power to whoever possesses it. The Autobots managed to smuggle the Allspark off the planet, but Megatron blasts off in search of it. He eventually tracks it to the planet of Earth (circa 1850), but his reckless desire for power sends him right into the Arctic Ocean, and the sheer cold forces him into a paralyzed state. His body is later found by Captain Archibald Witwicky, but before going into a comatose state Megatron uses the last of his energy to engrave into the Captain's glasses a map showing the location of the Allspark, and to send a transmission to Cybertron. Megatron is then carried away aboard the Captain's ship. A century later, Captain Witwicky's grandson Sam Witwicky (nicknamed Spike by his friends) buys his first car. To his shock, he discovers it to be Bumblebee, an Autobot in disguise who is to protect Spike, who possesses the Captain's glasses and the map engraved on them. But Bumblebee is not the only Transformer to have arrived on Earth - in the desert of Qatar, the Decepticons Blackout and Scorponok attack a U.S. military base, causing the Pentagon to send their special Sector Seven agents to capture all \"specimens of this alien race.\" Spike and his girlfriend Mikaela find themselves in the midst of a grand battle between the Autobots and the Decepticons, stretching from Hoover Dam all the way to Los Angeles. Meanwhile, deep inside Hoover Dam, the cryogenically stored body of Megatron awakens.", + "language": "English, Spanish", + "country": "USA", + "awards": "Nominated for 3 Oscars. Another 18 wins & 40 nominations.", + "poster": "http://ia.media-imdb.com/images/M/MV5BMTQwNjU5MzUzNl5BMl5BanBnXkFtZTYwMzc1MTI3._V1_SX300.jpg", + "metascore": "61", + "imdbRating": "7.1", + "imdbVotes": "492,225", + "imdbID": "tt0418279", + "Type": "movie", + "response": "True" +} \ No newline at end of file From df982db26b1a356a77b0b9eaf1ba6e2bb32c9b51 Mon Sep 17 00:00:00 2001 From: Giuseppe Bueti Date: Sat, 30 Jan 2016 20:39:28 +0100 Subject: [PATCH 77/84] Added testCase for List All Movies and Add a Movie --- .../src/main/java/com/baeldung/Movie.java | 535 ------------------ .../main/java/com/baeldung/model/Movie.java | 22 +- .../com/baeldung/server/MovieCrudService.java | 25 +- .../server/service/MovieCrudService.java | 94 --- .../server/service/RestEasyServices.java | 40 -- .../com/baeldung/server/RestEasyClient.java | 104 +++- .../resources/server/movies/transformer.json | 22 - 7 files changed, 104 insertions(+), 738 deletions(-) delete mode 100644 RestEasy Example/src/main/java/com/baeldung/Movie.java delete mode 100644 RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java delete mode 100644 RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java delete mode 100644 RestEasy Example/src/test/resources/server/movies/transformer.json diff --git a/RestEasy Example/src/main/java/com/baeldung/Movie.java b/RestEasy Example/src/main/java/com/baeldung/Movie.java deleted file mode 100644 index c0041d2e95..0000000000 --- a/RestEasy Example/src/main/java/com/baeldung/Movie.java +++ /dev/null @@ -1,535 +0,0 @@ - -package com.baeldung; - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlType; - - -@XmlAccessorType(XmlAccessType.FIELD) -@XmlType(name = "movie", propOrder = { - "actors", - "awards", - "country", - "director", - "genre", - "imdbID", - "imdbRating", - "imdbVotes", - "language", - "metascore", - "plot", - "poster", - "rated", - "released", - "response", - "runtime", - "title", - "type", - "writer", - "year" -}) -public class Movie { - - protected String actors; - protected String awards; - protected String country; - protected String director; - protected String genre; - protected String imdbID; - protected String imdbRating; - protected String imdbVotes; - protected String language; - protected String metascore; - protected String plot; - protected String poster; - protected String rated; - protected String released; - protected String response; - protected String runtime; - protected String title; - protected String type; - protected String writer; - protected String year; - - /** - * Recupera il valore della propriet� actors. - * - * @return - * possible object is - * {@link String } - * - */ - public String getActors() { - return actors; - } - - /** - * Imposta il valore della propriet� actors. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setActors(String value) { - this.actors = value; - } - - /** - * Recupera il valore della propriet� awards. - * - * @return - * possible object is - * {@link String } - * - */ - public String getAwards() { - return awards; - } - - /** - * Imposta il valore della propriet� awards. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setAwards(String value) { - this.awards = value; - } - - /** - * Recupera il valore della propriet� country. - * - * @return - * possible object is - * {@link String } - * - */ - public String getCountry() { - return country; - } - - /** - * Imposta il valore della propriet� country. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setCountry(String value) { - this.country = value; - } - - /** - * Recupera il valore della propriet� director. - * - * @return - * possible object is - * {@link String } - * - */ - public String getDirector() { - return director; - } - - /** - * Imposta il valore della propriet� director. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setDirector(String value) { - this.director = value; - } - - /** - * Recupera il valore della propriet� genre. - * - * @return - * possible object is - * {@link String } - * - */ - public String getGenre() { - return genre; - } - - /** - * Imposta il valore della propriet� genre. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setGenre(String value) { - this.genre = value; - } - - /** - * Recupera il valore della propriet� imdbID. - * - * @return - * possible object is - * {@link String } - * - */ - public String getImdbID() { - return imdbID; - } - - /** - * Imposta il valore della propriet� imdbID. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setImdbID(String value) { - this.imdbID = value; - } - - /** - * Recupera il valore della propriet� imdbRating. - * - * @return - * possible object is - * {@link String } - * - */ - public String getImdbRating() { - return imdbRating; - } - - /** - * Imposta il valore della propriet� imdbRating. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setImdbRating(String value) { - this.imdbRating = value; - } - - /** - * Recupera il valore della propriet� imdbVotes. - * - * @return - * possible object is - * {@link String } - * - */ - public String getImdbVotes() { - return imdbVotes; - } - - /** - * Imposta il valore della propriet� imdbVotes. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setImdbVotes(String value) { - this.imdbVotes = value; - } - - /** - * Recupera il valore della propriet� language. - * - * @return - * possible object is - * {@link String } - * - */ - public String getLanguage() { - return language; - } - - /** - * Imposta il valore della propriet� language. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setLanguage(String value) { - this.language = value; - } - - /** - * Recupera il valore della propriet� metascore. - * - * @return - * possible object is - * {@link String } - * - */ - public String getMetascore() { - return metascore; - } - - /** - * Imposta il valore della propriet� metascore. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setMetascore(String value) { - this.metascore = value; - } - - /** - * Recupera il valore della propriet� plot. - * - * @return - * possible object is - * {@link String } - * - */ - public String getPlot() { - return plot; - } - - /** - * Imposta il valore della propriet� plot. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setPlot(String value) { - this.plot = value; - } - - /** - * Recupera il valore della propriet� poster. - * - * @return - * possible object is - * {@link String } - * - */ - public String getPoster() { - return poster; - } - - /** - * Imposta il valore della propriet� poster. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setPoster(String value) { - this.poster = value; - } - - /** - * Recupera il valore della propriet� rated. - * - * @return - * possible object is - * {@link String } - * - */ - public String getRated() { - return rated; - } - - /** - * Imposta il valore della propriet� rated. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setRated(String value) { - this.rated = value; - } - - /** - * Recupera il valore della propriet� released. - * - * @return - * possible object is - * {@link String } - * - */ - public String getReleased() { - return released; - } - - /** - * Imposta il valore della propriet� released. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setReleased(String value) { - this.released = value; - } - - /** - * Recupera il valore della propriet� response. - * - * @return - * possible object is - * {@link String } - * - */ - public String getResponse() { - return response; - } - - /** - * Imposta il valore della propriet� response. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setResponse(String value) { - this.response = value; - } - - /** - * Recupera il valore della propriet� runtime. - * - * @return - * possible object is - * {@link String } - * - */ - public String getRuntime() { - return runtime; - } - - /** - * Imposta il valore della propriet� runtime. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setRuntime(String value) { - this.runtime = value; - } - - /** - * Recupera il valore della propriet� title. - * - * @return - * possible object is - * {@link String } - * - */ - public String getTitle() { - return title; - } - - /** - * Imposta il valore della propriet� title. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setTitle(String value) { - this.title = value; - } - - /** - * Recupera il valore della propriet� type. - * - * @return - * possible object is - * {@link String } - * - */ - public String getType() { - return type; - } - - /** - * Imposta il valore della propriet� type. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setType(String value) { - this.type = value; - } - - /** - * Recupera il valore della propriet� writer. - * - * @return - * possible object is - * {@link String } - * - */ - public String getWriter() { - return writer; - } - - /** - * Imposta il valore della propriet� writer. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setWriter(String value) { - this.writer = value; - } - - /** - * Recupera il valore della propriet� year. - * - * @return - * possible object is - * {@link String } - * - */ - public String getYear() { - return year; - } - - /** - * Imposta il valore della propriet� year. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setYear(String value) { - this.year = value; - } - -} diff --git a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java index a2b2bd5250..052ba081c1 100644 --- a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -13,7 +13,7 @@ import javax.xml.bind.annotation.XmlType; "country", "director", "genre", - "imdbId", + "imdbID", "imdbRating", "imdbVotes", "language", @@ -36,7 +36,7 @@ public class Movie { protected String country; protected String director; protected String genre; - protected String imdbId; + protected String imdbID; protected String imdbRating; protected String imdbVotes; protected String language; @@ -173,27 +173,27 @@ public class Movie { } /** - * Recupera il valore della propriet� imdbId. + * Recupera il valore della propriet� imdbID. * * @return * possible object is * {@link String } * */ - public String getImdbId() { - return imdbId; + public String getImdbID() { + return imdbID; } /** - * Imposta il valore della propriet� imdbId. + * Imposta il valore della propriet� imdbID. * * @param value * allowed object is * {@link String } * */ - public void setImdbId(String value) { - this.imdbId = value; + public void setImdbID(String value) { + this.imdbID = value; } /** @@ -540,7 +540,7 @@ public class Movie { ", country='" + country + '\'' + ", director='" + director + '\'' + ", genre='" + genre + '\'' + - ", imdbId='" + imdbId + '\'' + + ", imdbID='" + imdbID + '\'' + ", imdbRating='" + imdbRating + '\'' + ", imdbVotes='" + imdbVotes + '\'' + ", language='" + language + '\'' + @@ -564,14 +564,14 @@ public class Movie { Movie movie = (Movie) o; - if (imdbId != null ? !imdbId.equals(movie.imdbId) : movie.imdbId != null) return false; + if (imdbID != null ? !imdbID.equals(movie.imdbID) : movie.imdbID != null) return false; return title != null ? title.equals(movie.title) : movie.title == null; } @Override public int hashCode() { - int result = imdbId != null ? imdbId.hashCode() : 0; + int result = imdbID != null ? imdbID.hashCode() : 0; result = 31 * result + (title != null ? title.hashCode() : 0); return result; } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index 29226aa0e0..60e0121966 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -18,21 +18,18 @@ public class MovieCrudService { private Map inventory = new HashMap(); - @GET @Path("/getinfo") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Movie movieByImdbID(@QueryParam("imdbId") String imdbID){ + public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){ - System.out.println("*** Calling getinfo for a given ImdbID***"); - - if(inventory.containsKey(imdbID)){ - return inventory.get(imdbID); - }else return null; + System.out.println("*** Calling getinfo ***"); + Movie movie=new Movie(); + movie.setImdbID(imdbID); + return movie; } - @POST @Path("/addmovie") @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @@ -40,12 +37,11 @@ public class MovieCrudService { System.out.println("*** Calling addMovie ***"); - if (null!=inventory.get(movie.getImdbId())){ + if (null!=inventory.get(movie.getImdbID())){ return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is Already in the database.").build(); } - - inventory.put(movie.getImdbId(),movie); + inventory.put(movie.getImdbID(),movie); return Response.status(Response.Status.CREATED).build(); } @@ -58,11 +54,11 @@ public class MovieCrudService { System.out.println("*** Calling updateMovie ***"); - if (null==inventory.get(movie.getImdbId())){ + if (null!=inventory.get(movie.getImdbID())){ return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is not in the database.\nUnable to Update").build(); } - inventory.put(movie.getImdbId(),movie); + inventory.put(movie.getImdbID(),movie); return Response.status(Response.Status.OK).build(); } @@ -70,7 +66,7 @@ public class MovieCrudService { @DELETE @Path("/deletemovie") - public Response deleteMovie(@QueryParam("imdbId") String imdbID){ + public Response deleteMovie(@QueryParam("imdbID") String imdbID){ System.out.println("*** Calling deleteMovie ***"); @@ -83,7 +79,6 @@ public class MovieCrudService { return Response.status(Response.Status.OK).build(); } - @GET @Path("/listmovies") @Produces({"application/json"}) diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java deleted file mode 100644 index d1973e7037..0000000000 --- a/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.baeldung.server.service; - -import com.baeldung.Movie; - -import javax.ws.rs.*; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.Provider; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - - -@Path("/movies") -public class MovieCrudService { - - - private Map inventory = new HashMap(); - - @GET - @Path("/getinfo") - @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){ - - System.out.println("*** Calling getinfo ***"); - - Movie movie=new Movie(); - movie.setImdbID(imdbID); - return movie; - } - - @POST - @Path("/addmovie") - @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Response addMovie(Movie movie){ - - System.out.println("*** Calling addMovie ***"); - - if (null!=inventory.get(movie.getImdbID())){ - return Response.status(Response.Status.NOT_MODIFIED) - .entity("Movie is Already in the database.").build(); - } - inventory.put(movie.getImdbID(),movie); - - return Response.status(Response.Status.CREATED).build(); - } - - - @PUT - @Path("/updatemovie") - @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Response updateMovie(Movie movie){ - - System.out.println("*** Calling updateMovie ***"); - - if (null!=inventory.get(movie.getImdbID())){ - return Response.status(Response.Status.NOT_MODIFIED) - .entity("Movie is not in the database.\nUnable to Update").build(); - } - inventory.put(movie.getImdbID(),movie); - return Response.status(Response.Status.OK).build(); - - } - - - @DELETE - @Path("/deletemovie") - public Response deleteMovie(@QueryParam("imdbID") String imdbID){ - - System.out.println("*** Calling deleteMovie ***"); - - if (null==inventory.get(imdbID)){ - return Response.status(Response.Status.NOT_FOUND) - .entity("Movie is not in the database.\nUnable to Delete").build(); - } - - inventory.remove(imdbID); - return Response.status(Response.Status.OK).build(); - } - - @GET - @Path("/listmovies") - @Produces({"application/json"}) - public List listMovies(){ - - return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new)); - - } - - - -} diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java b/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java deleted file mode 100644 index 16b6200ad1..0000000000 --- a/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.server.service; - - -import javax.ws.rs.ApplicationPath; -import javax.ws.rs.core.Application; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -/** - * Created by Admin on 29/01/2016. - */ - - - -@ApplicationPath("/rest") -public class RestEasyServices extends Application { - - private Set singletons = new HashSet(); - - public RestEasyServices() { - singletons.add(new MovieCrudService()); - } - - @Override - public Set getSingletons() { - return singletons; - } - - @Override - public Set> getClasses() { - return super.getClasses(); - } - - @Override - public Map getProperties() { - return super.getProperties(); - } -} diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java index e711233979..c77f494862 100644 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java @@ -1,49 +1,111 @@ package com.baeldung.server; -import com.baeldung.Movie; +import com.baeldung.model.Movie; import com.baeldung.client.ServicesInterface; +import org.apache.commons.io.IOUtils; +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; import org.jboss.resteasy.client.jaxrs.ResteasyClient; import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import javax.naming.NamingException; +import javax.ws.rs.core.Link; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.List; +import java.util.Locale; public class RestEasyClient { - public static void main(String[] args) { - Movie st = new Movie(); - st.setImdbID("12345"); + Movie transformerMovie=null; + Movie batmanMovie=null; + ObjectMapper jsonMapper=null; - /* - * Alternatively you can use this simple String to send - * instead of using a Student instance - * - * String jsonString = "{\"id\":12,\"firstName\":\"Catain\",\"lastName\":\"Hook\",\"age\":10}"; - */ + @BeforeClass + public static void loadMovieInventory(){ + + + + } + + @Before + public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { + + + jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); + jsonMapper.setDateFormat(sdf); + + try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/transformer.json")) { + String transformerMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + transformerMovie = jsonMapper.readValue(transformerMovieAsString, Movie.class); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/batman.json")) { + String batmanMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + } + + @Test + public void testListAllMovies() { try { ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target("http://localhost:8080/RestEasyTutorial/rest/movies/listmovies"); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + final List movies = simple.listMovies(); + System.out.println(movies); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + + @Test + public void testAddMovie() { + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); ServicesInterface simple = target.proxy(ServicesInterface.class); - final List movies = simple.listMovies(); + final Response moviesResponse = simple.addMovie(batmanMovie); - /* - if (response.getStatus() != 200) { + if (moviesResponse.getStatus() != 201) { + System.out.println(moviesResponse.readEntity(String.class)); throw new RuntimeException("Failed : HTTP error code : " - + response.getStatus()); + + moviesResponse.getStatus()); } - System.out.println("Server response : \n"); - System.out.println(response.readEntity(String.class)); + moviesResponse.close(); - response.close(); -*/ } catch (Exception e) { - e.printStackTrace(); - } } diff --git a/RestEasy Example/src/test/resources/server/movies/transformer.json b/RestEasy Example/src/test/resources/server/movies/transformer.json deleted file mode 100644 index 2154868265..0000000000 --- a/RestEasy Example/src/test/resources/server/movies/transformer.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "title": "Transformers", - "year": "2007", - "rated": "PG-13", - "released": "03 Jul 2007", - "runtime": "144 min", - "genre": "Action, Adventure, Sci-Fi", - "director": "Michael Bay", - "writer": "Roberto Orci (screenplay), Alex Kurtzman (screenplay), John Rogers (story), Roberto Orci (story), Alex Kurtzman (story)", - "actors": "Shia LaBeouf, Megan Fox, Josh Duhamel, Tyrese Gibson", - "plot": "A long time ago, far away on the planet of Cybertron, a war is being waged between the noble Autobots (led by the wise Optimus Prime) and the devious Decepticons (commanded by the dreaded Megatron) for control over the Allspark, a mystical talisman that would grant unlimited power to whoever possesses it. The Autobots managed to smuggle the Allspark off the planet, but Megatron blasts off in search of it. He eventually tracks it to the planet of Earth (circa 1850), but his reckless desire for power sends him right into the Arctic Ocean, and the sheer cold forces him into a paralyzed state. His body is later found by Captain Archibald Witwicky, but before going into a comatose state Megatron uses the last of his energy to engrave into the Captain's glasses a map showing the location of the Allspark, and to send a transmission to Cybertron. Megatron is then carried away aboard the Captain's ship. A century later, Captain Witwicky's grandson Sam Witwicky (nicknamed Spike by his friends) buys his first car. To his shock, he discovers it to be Bumblebee, an Autobot in disguise who is to protect Spike, who possesses the Captain's glasses and the map engraved on them. But Bumblebee is not the only Transformer to have arrived on Earth - in the desert of Qatar, the Decepticons Blackout and Scorponok attack a U.S. military base, causing the Pentagon to send their special Sector Seven agents to capture all \"specimens of this alien race.\" Spike and his girlfriend Mikaela find themselves in the midst of a grand battle between the Autobots and the Decepticons, stretching from Hoover Dam all the way to Los Angeles. Meanwhile, deep inside Hoover Dam, the cryogenically stored body of Megatron awakens.", - "language": "English, Spanish", - "country": "USA", - "awards": "Nominated for 3 Oscars. Another 18 wins & 40 nominations.", - "poster": "http://ia.media-imdb.com/images/M/MV5BMTQwNjU5MzUzNl5BMl5BanBnXkFtZTYwMzc1MTI3._V1_SX300.jpg", - "metascore": "61", - "imdbRating": "7.1", - "imdbVotes": "492,225", - "imdbID": "tt0418279", - "Type": "movie", - "response": "True" -} \ No newline at end of file From 6db36f902b1e7cf6f1c7857e5a14295e8881c732 Mon Sep 17 00:00:00 2001 From: Giuseppe Bueti Date: Sun, 31 Jan 2016 11:05:11 +0100 Subject: [PATCH 78/84] Added testCase for all Services --- .../baeldung/client/ServicesInterface.java | 6 + .../com/baeldung/server/MovieCrudService.java | 15 ++- .../com/baeldung/server/RestEasyClient.java | 112 ------------------ 3 files changed, 16 insertions(+), 117 deletions(-) delete mode 100644 RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java index 749cabc757..8898b83483 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -23,6 +23,12 @@ public interface ServicesInterface { List listMovies(); + @GET + @Path("/listmovies") + @Produces({"application/json"}) + List listMovies(); + + @POST @Path("/addmovie") @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index 60e0121966..18366e2faa 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -18,18 +18,21 @@ public class MovieCrudService { private Map inventory = new HashMap(); + @GET @Path("/getinfo") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){ - System.out.println("*** Calling getinfo ***"); + System.out.println("*** Calling getinfo for a given ImdbID***"); + + if(inventory.containsKey(imdbID)){ + return inventory.get(imdbID); + }else return null; - Movie movie=new Movie(); - movie.setImdbID(imdbID); - return movie; } + @POST @Path("/addmovie") @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @@ -41,6 +44,7 @@ public class MovieCrudService { return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is Already in the database.").build(); } + inventory.put(movie.getImdbID(),movie); return Response.status(Response.Status.CREATED).build(); @@ -54,7 +58,7 @@ public class MovieCrudService { System.out.println("*** Calling updateMovie ***"); - if (null!=inventory.get(movie.getImdbID())){ + if (null==inventory.get(movie.getImdbID())){ return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is not in the database.\nUnable to Update").build(); } @@ -79,6 +83,7 @@ public class MovieCrudService { return Response.status(Response.Status.OK).build(); } + @GET @Path("/listmovies") @Produces({"application/json"}) diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java deleted file mode 100644 index c77f494862..0000000000 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.baeldung.server; - -import com.baeldung.model.Movie; -import com.baeldung.client.ServicesInterface; -import org.apache.commons.io.IOUtils; -import org.codehaus.jackson.map.DeserializationConfig; -import org.codehaus.jackson.map.ObjectMapper; -import org.jboss.resteasy.client.jaxrs.ResteasyClient; -import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; -import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import javax.naming.NamingException; -import javax.ws.rs.core.Link; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriBuilder; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileReader; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.List; -import java.util.Locale; - -public class RestEasyClient { - - - Movie transformerMovie=null; - Movie batmanMovie=null; - ObjectMapper jsonMapper=null; - - @BeforeClass - public static void loadMovieInventory(){ - - - - } - - @Before - public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { - - - jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); - jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); - SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); - jsonMapper.setDateFormat(sdf); - - try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/transformer.json")) { - String transformerMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); - transformerMovie = jsonMapper.readValue(transformerMovieAsString, Movie.class); - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException("Test is going to die ...", e); - } - - try (InputStream inputStream = new RestEasyClient().getClass().getResourceAsStream("./movies/batman.json")) { - String batmanMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); - batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); - - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException("Test is going to die ...", e); - } - - } - - @Test - public void testListAllMovies() { - - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - ServicesInterface simple = target.proxy(ServicesInterface.class); - - final List movies = simple.listMovies(); - System.out.println(movies); - - } catch (Exception e) { - e.printStackTrace(); - } - } - - - - @Test - public void testAddMovie() { - - try { - ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); - - ServicesInterface simple = target.proxy(ServicesInterface.class); - final Response moviesResponse = simple.addMovie(batmanMovie); - - if (moviesResponse.getStatus() != 201) { - System.out.println(moviesResponse.readEntity(String.class)); - throw new RuntimeException("Failed : HTTP error code : " - + moviesResponse.getStatus()); - } - - moviesResponse.close(); - - } catch (Exception e) { - e.printStackTrace(); - } - } - -} \ No newline at end of file From 33223becff3f88fd3bb8e5734f621c780c1ba454 Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sun, 7 Feb 2016 20:29:30 +0100 Subject: [PATCH 79/84] Minor Bugfix --- .../main/java/com/baeldung/model/Movie.java | 22 +++++++++---------- .../com/baeldung/server/MovieCrudService.java | 12 +++++----- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java index 052ba081c1..a2b2bd5250 100644 --- a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -13,7 +13,7 @@ import javax.xml.bind.annotation.XmlType; "country", "director", "genre", - "imdbID", + "imdbId", "imdbRating", "imdbVotes", "language", @@ -36,7 +36,7 @@ public class Movie { protected String country; protected String director; protected String genre; - protected String imdbID; + protected String imdbId; protected String imdbRating; protected String imdbVotes; protected String language; @@ -173,27 +173,27 @@ public class Movie { } /** - * Recupera il valore della propriet� imdbID. + * Recupera il valore della propriet� imdbId. * * @return * possible object is * {@link String } * */ - public String getImdbID() { - return imdbID; + public String getImdbId() { + return imdbId; } /** - * Imposta il valore della propriet� imdbID. + * Imposta il valore della propriet� imdbId. * * @param value * allowed object is * {@link String } * */ - public void setImdbID(String value) { - this.imdbID = value; + public void setImdbId(String value) { + this.imdbId = value; } /** @@ -540,7 +540,7 @@ public class Movie { ", country='" + country + '\'' + ", director='" + director + '\'' + ", genre='" + genre + '\'' + - ", imdbID='" + imdbID + '\'' + + ", imdbId='" + imdbId + '\'' + ", imdbRating='" + imdbRating + '\'' + ", imdbVotes='" + imdbVotes + '\'' + ", language='" + language + '\'' + @@ -564,14 +564,14 @@ public class Movie { Movie movie = (Movie) o; - if (imdbID != null ? !imdbID.equals(movie.imdbID) : movie.imdbID != null) return false; + if (imdbId != null ? !imdbId.equals(movie.imdbId) : movie.imdbId != null) return false; return title != null ? title.equals(movie.title) : movie.title == null; } @Override public int hashCode() { - int result = imdbID != null ? imdbID.hashCode() : 0; + int result = imdbId != null ? imdbId.hashCode() : 0; result = 31 * result + (title != null ? title.hashCode() : 0); return result; } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index 18366e2faa..29226aa0e0 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -22,7 +22,7 @@ public class MovieCrudService { @GET @Path("/getinfo") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){ + public Movie movieByImdbID(@QueryParam("imdbId") String imdbID){ System.out.println("*** Calling getinfo for a given ImdbID***"); @@ -40,12 +40,12 @@ public class MovieCrudService { System.out.println("*** Calling addMovie ***"); - if (null!=inventory.get(movie.getImdbID())){ + if (null!=inventory.get(movie.getImdbId())){ return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is Already in the database.").build(); } - inventory.put(movie.getImdbID(),movie); + inventory.put(movie.getImdbId(),movie); return Response.status(Response.Status.CREATED).build(); } @@ -58,11 +58,11 @@ public class MovieCrudService { System.out.println("*** Calling updateMovie ***"); - if (null==inventory.get(movie.getImdbID())){ + if (null==inventory.get(movie.getImdbId())){ return Response.status(Response.Status.NOT_MODIFIED) .entity("Movie is not in the database.\nUnable to Update").build(); } - inventory.put(movie.getImdbID(),movie); + inventory.put(movie.getImdbId(),movie); return Response.status(Response.Status.OK).build(); } @@ -70,7 +70,7 @@ public class MovieCrudService { @DELETE @Path("/deletemovie") - public Response deleteMovie(@QueryParam("imdbID") String imdbID){ + public Response deleteMovie(@QueryParam("imdbId") String imdbID){ System.out.println("*** Calling deleteMovie ***"); From 783dc6bdc2cb172dd3bacf01faba9531675c45ab Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sun, 7 Feb 2016 20:39:35 +0100 Subject: [PATCH 80/84] Minor Bugfix --- .../main/java/com/baeldung/model/Movie.java | 321 +----------------- 1 file changed, 1 insertion(+), 320 deletions(-) diff --git a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java index a2b2bd5250..805ba95209 100644 --- a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -52,482 +52,163 @@ public class Movie { protected String writer; protected String year; - /** - * Recupera il valore della propriet� actors. - * - * @return - * possible object is - * {@link String } - * - */ + public String getActors() { return actors; } - /** - * Imposta il valore della propriet� actors. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setActors(String value) { this.actors = value; } - /** - * Recupera il valore della propriet� awards. - * - * @return - * possible object is - * {@link String } - * - */ public String getAwards() { return awards; } - /** - * Imposta il valore della propriet� awards. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setAwards(String value) { this.awards = value; } - /** - * Recupera il valore della propriet� country. - * - * @return - * possible object is - * {@link String } - * - */ public String getCountry() { return country; } - /** - * Imposta il valore della propriet� country. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setCountry(String value) { this.country = value; } - /** - * Recupera il valore della propriet� director. - * - * @return - * possible object is - * {@link String } - * - */ public String getDirector() { return director; } - /** - * Imposta il valore della propriet� director. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setDirector(String value) { this.director = value; } - /** - * Recupera il valore della propriet� genre. - * - * @return - * possible object is - * {@link String } - * - */ public String getGenre() { return genre; } - /** - * Imposta il valore della propriet� genre. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setGenre(String value) { this.genre = value; } - /** - * Recupera il valore della propriet� imdbId. - * - * @return - * possible object is - * {@link String } - * - */ public String getImdbId() { return imdbId; } - /** - * Imposta il valore della propriet� imdbId. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setImdbId(String value) { this.imdbId = value; } - /** - * Recupera il valore della propriet� imdbRating. - * - * @return - * possible object is - * {@link String } - * - */ public String getImdbRating() { return imdbRating; } - /** - * Imposta il valore della propriet� imdbRating. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setImdbRating(String value) { this.imdbRating = value; } - /** - * Recupera il valore della propriet� imdbVotes. - * - * @return - * possible object is - * {@link String } - * - */ public String getImdbVotes() { return imdbVotes; } - /** - * Imposta il valore della propriet� imdbVotes. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setImdbVotes(String value) { this.imdbVotes = value; } - /** - * Recupera il valore della propriet� language. - * - * @return - * possible object is - * {@link String } - * - */ public String getLanguage() { return language; } - /** - * Imposta il valore della propriet� language. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setLanguage(String value) { this.language = value; } - /** - * Recupera il valore della propriet� metascore. - * - * @return - * possible object is - * {@link String } - * - */ public String getMetascore() { return metascore; } - /** - * Imposta il valore della propriet� metascore. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setMetascore(String value) { this.metascore = value; } - /** - * Recupera il valore della propriet� plot. - * - * @return - * possible object is - * {@link String } - * - */ public String getPlot() { return plot; } - /** - * Imposta il valore della propriet� plot. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setPlot(String value) { this.plot = value; } - /** - * Recupera il valore della propriet� poster. - * - * @return - * possible object is - * {@link String } - * - */ public String getPoster() { return poster; } - /** - * Imposta il valore della propriet� poster. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setPoster(String value) { this.poster = value; } - /** - * Recupera il valore della propriet� rated. - * - * @return - * possible object is - * {@link String } - * - */ public String getRated() { return rated; } - /** - * Imposta il valore della propriet� rated. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setRated(String value) { this.rated = value; } - /** - * Recupera il valore della propriet� released. - * - * @return - * possible object is - * {@link String } - * - */ public String getReleased() { return released; } - /** - * Imposta il valore della propriet� released. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setReleased(String value) { this.released = value; } - /** - * Recupera il valore della propriet� response. - * - * @return - * possible object is - * {@link String } - * - */ public String getResponse() { return response; } - /** - * Imposta il valore della propriet� response. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setResponse(String value) { this.response = value; } - /** - * Recupera il valore della propriet� runtime. - * - * @return - * possible object is - * {@link String } - * - */ public String getRuntime() { return runtime; } - /** - * Imposta il valore della propriet� runtime. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setRuntime(String value) { this.runtime = value; } - /** - * Recupera il valore della propriet� title. - * - * @return - * possible object is - * {@link String } - * - */ public String getTitle() { return title; } - /** - * Imposta il valore della propriet� title. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setTitle(String value) { this.title = value; } - /** - * Recupera il valore della propriet� type. - * - * @return - * possible object is - * {@link String } - * - */ public String getType() { return type; } - /** - * Imposta il valore della propriet� type. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setType(String value) { this.type = value; } - /** - * Recupera il valore della propriet� writer. - * - * @return - * possible object is - * {@link String } - * - */ public String getWriter() { return writer; } - /** - * Imposta il valore della propriet� writer. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setWriter(String value) { this.writer = value; } - /** - * Recupera il valore della propriet� year. - * - * @return - * possible object is - * {@link String } - * - */ public String getYear() { return year; } - /** - * Imposta il valore della propriet� year. - * - * @param value - * allowed object is - * {@link String } - * - */ public void setYear(String value) { this.year = value; } From 71793635d32d50427bc4165b29b39aaabec3d743 Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sun, 7 Feb 2016 20:55:46 +0100 Subject: [PATCH 81/84] CleanUp Code --- RestEasy Example/pom.xml | 3 +- .../baeldung/client/ServicesInterface.java | 1 - .../main/java/com/baeldung/model/Movie.java | 2 -- .../com/baeldung/server/MovieCrudService.java | 17 ++++------- .../com/baeldung/server/RestEasyServices.java | 8 ----- .../src/main/resources/schema1.xsd | 29 ------------------- .../src/main/webapp/WEB-INF/web.xml | 3 -- .../java/baeldung/client/RestEasyClient.java | 7 ----- .../baeldung/server/RestEasyClientTest.java | 1 - 9 files changed, 7 insertions(+), 64 deletions(-) delete mode 100644 RestEasy Example/src/main/resources/schema1.xsd delete mode 100644 RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java diff --git a/RestEasy Example/pom.xml b/RestEasy Example/pom.xml index 6935238d91..9c890da2b7 100644 --- a/RestEasy Example/pom.xml +++ b/RestEasy Example/pom.xml @@ -36,7 +36,7 @@ - + org.jboss.resteasy jaxrs-api @@ -101,5 +101,4 @@ - \ No newline at end of file diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java index 8898b83483..22669717a8 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -1,7 +1,6 @@ package com.baeldung.client; import com.baeldung.model.Movie; - import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; diff --git a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java index 805ba95209..56ba766ff4 100644 --- a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -1,11 +1,9 @@ - package com.baeldung.model; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlType; - @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "movie", propOrder = { "actors", diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index 29226aa0e0..bbb3b1e953 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -1,7 +1,6 @@ package com.baeldung.server; import com.baeldung.model.Movie; - import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -11,23 +10,21 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; - @Path("/movies") public class MovieCrudService { - private Map inventory = new HashMap(); @GET @Path("/getinfo") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Movie movieByImdbID(@QueryParam("imdbId") String imdbID){ + public Movie movieByImdbId(@QueryParam("imdbId") String imdbId){ System.out.println("*** Calling getinfo for a given ImdbID***"); - if(inventory.containsKey(imdbID)){ - return inventory.get(imdbID); + if(inventory.containsKey(imdbId)){ + return inventory.get(imdbId); }else return null; } @@ -70,16 +67,16 @@ public class MovieCrudService { @DELETE @Path("/deletemovie") - public Response deleteMovie(@QueryParam("imdbId") String imdbID){ + public Response deleteMovie(@QueryParam("imdbId") String imdbId){ System.out.println("*** Calling deleteMovie ***"); - if (null==inventory.get(imdbID)){ + if (null==inventory.get(imdbId)){ return Response.status(Response.Status.NOT_FOUND) .entity("Movie is not in the database.\nUnable to Delete").build(); } - inventory.remove(imdbID); + inventory.remove(imdbId); return Response.status(Response.Status.OK).build(); } @@ -93,6 +90,4 @@ public class MovieCrudService { } - - } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java b/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java index 8c57d2c9b4..7726e49f38 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java @@ -1,19 +1,11 @@ package com.baeldung.server; - import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; -import java.util.Arrays; import java.util.HashSet; import java.util.Map; import java.util.Set; -/** - * Created by Admin on 29/01/2016. - */ - - - @ApplicationPath("/rest") public class RestEasyServices extends Application { diff --git a/RestEasy Example/src/main/resources/schema1.xsd b/RestEasy Example/src/main/resources/schema1.xsd deleted file mode 100644 index 0d74b7c366..0000000000 --- a/RestEasy Example/src/main/resources/schema1.xsd +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/RestEasy Example/src/main/webapp/WEB-INF/web.xml b/RestEasy Example/src/main/webapp/WEB-INF/web.xml index f70fdf7975..1e7f64004d 100644 --- a/RestEasy Example/src/main/webapp/WEB-INF/web.xml +++ b/RestEasy Example/src/main/webapp/WEB-INF/web.xml @@ -5,12 +5,9 @@ RestEasy Example - resteasy.servlet.mapping.prefix /rest - - \ No newline at end of file diff --git a/RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java b/RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java deleted file mode 100644 index b474b3d4f8..0000000000 --- a/RestEasy Example/src/test/java/baeldung/client/RestEasyClient.java +++ /dev/null @@ -1,7 +0,0 @@ -package baeldung.client; - -/** - * Created by Admin on 29/01/2016. - */ -public class RestEasyClient { -} diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java index b6a2e2a0c1..faa39e0199 100644 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java @@ -10,7 +10,6 @@ import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; import org.junit.Before; import org.junit.Test; - import javax.naming.NamingException; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; From 04d9bbb7682999c0d90054150aac69cbfa442781 Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Mon, 8 Feb 2016 14:00:11 +0100 Subject: [PATCH 82/84] CleanUp and reformatting Code; dependency fix in pom.xml --- RestEasy Example/pom.xml | 147 +++++++----------- .../baeldung/client/ServicesInterface.java | 21 +-- .../main/java/com/baeldung/model/Movie.java | 56 ++----- .../com/baeldung/server/MovieCrudService.java | 49 +++--- .../WEB-INF/jboss-deployment-structure.xml | 22 +-- .../src/main/webapp/WEB-INF/web.xml | 14 +- .../baeldung/server/RestEasyClientTest.java | 23 ++- .../com/baeldung/server/movies/batman.json | 2 +- .../baeldung/server/movies/transformer.json | 2 +- 9 files changed, 125 insertions(+), 211 deletions(-) diff --git a/RestEasy Example/pom.xml b/RestEasy Example/pom.xml index 9c890da2b7..ec9e87b0d1 100644 --- a/RestEasy Example/pom.xml +++ b/RestEasy Example/pom.xml @@ -1,104 +1,77 @@ - - 4.0.0 + + 4.0.0 - com.baeldung - resteasy-tutorial - 1.0 - war + com.baeldung + resteasy-tutorial + 1.0 + war - - - jboss - http://repository.jboss.org/nexus/content/groups/public/ - - + + 3.0.14.Final + - - 3.0.14.Final - runtime - + + RestEasyTutorial + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + - - RestEasyTutorial - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.8 - 1.8 - - - - + - + - - org.jboss.resteasy - jaxrs-api - 3.0.12.Final - ${resteasy.scope} - - - - org.jboss.resteasy - resteasy-servlet-initializer - ${resteasy.version} - ${resteasy.scope} - - - jboss-jaxrs-api_2.0_spec - org.jboss.spec.javax.ws.rs - - - - - - org.jboss.resteasy - resteasy-client - ${resteasy.version} - ${resteasy.scope} - + + org.jboss.resteasy + resteasy-servlet-initializer + ${resteasy.version} + - - javax.ws.rs - javax.ws.rs-api - 2.0.1 - + + org.jboss.resteasy + resteasy-client + ${resteasy.version} + - - org.jboss.resteasy - resteasy-jackson-provider - ${resteasy.version} - ${resteasy.scope} - + - - org.jboss.resteasy - resteasy-jaxb-provider - ${resteasy.version} - ${resteasy.scope} - + + org.jboss.resteasy + resteasy-jaxb-provider + ${resteasy.version} + - + + org.jboss.resteasy + resteasy-jackson-provider + ${resteasy.version} + - - junit - junit - 4.4 - + - - commons-io - commons-io - 2.4 - + + junit + junit + 4.4 + + + + commons-io + commons-io + 2.4 + + + - \ No newline at end of file diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java index 22669717a8..3d03c16faf 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -9,41 +9,28 @@ import java.util.List; @Path("/movies") public interface ServicesInterface { - @GET @Path("/getinfo") - @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) Movie movieByImdbId(@QueryParam("imdbId") String imdbId); - @GET @Path("/listmovies") - @Produces({"application/json"}) + @Produces({ "application/json" }) List listMovies(); - - @GET - @Path("/listmovies") - @Produces({"application/json"}) - List listMovies(); - - @POST @Path("/addmovie") - @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) Response addMovie(Movie movie); - @PUT @Path("/updatemovie") - @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) Response updateMovie(Movie movie); - @DELETE @Path("/deletemovie") Response deleteMovie(@QueryParam("imdbId") String imdbID); - - } diff --git a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java index 56ba766ff4..a959682a75 100644 --- a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -5,28 +5,7 @@ import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) -@XmlType(name = "movie", propOrder = { - "actors", - "awards", - "country", - "director", - "genre", - "imdbId", - "imdbRating", - "imdbVotes", - "language", - "metascore", - "plot", - "poster", - "rated", - "released", - "response", - "runtime", - "title", - "type", - "writer", - "year" -}) +@XmlType(name = "movie", propOrder = { "actors", "awards", "country", "director", "genre", "imdbId", "imdbRating", "imdbVotes", "language", "metascore", "plot", "poster", "rated", "released", "response", "runtime", "title", "type", "writer", "year" }) public class Movie { protected String actors; @@ -213,37 +192,22 @@ public class Movie { @Override public String toString() { - return "Movie{" + - "actors='" + actors + '\'' + - ", awards='" + awards + '\'' + - ", country='" + country + '\'' + - ", director='" + director + '\'' + - ", genre='" + genre + '\'' + - ", imdbId='" + imdbId + '\'' + - ", imdbRating='" + imdbRating + '\'' + - ", imdbVotes='" + imdbVotes + '\'' + - ", language='" + language + '\'' + - ", metascore='" + metascore + '\'' + - ", poster='" + poster + '\'' + - ", rated='" + rated + '\'' + - ", released='" + released + '\'' + - ", response='" + response + '\'' + - ", runtime='" + runtime + '\'' + - ", title='" + title + '\'' + - ", type='" + type + '\'' + - ", writer='" + writer + '\'' + - ", year='" + year + '\'' + - '}'; + return "Movie{" + "actors='" + actors + '\'' + ", awards='" + awards + '\'' + ", country='" + country + '\'' + ", director='" + director + '\'' + ", genre='" + genre + '\'' + ", imdbId='" + imdbId + '\'' + ", imdbRating='" + imdbRating + '\'' + + ", imdbVotes='" + imdbVotes + '\'' + ", language='" + language + '\'' + ", metascore='" + metascore + '\'' + ", poster='" + poster + '\'' + ", rated='" + rated + '\'' + ", released='" + released + '\'' + ", response='" + response + '\'' + + ", runtime='" + runtime + '\'' + ", title='" + title + '\'' + ", type='" + type + '\'' + ", writer='" + writer + '\'' + ", year='" + year + '\'' + '}'; } @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; Movie movie = (Movie) o; - if (imdbId != null ? !imdbId.equals(movie.imdbId) : movie.imdbId != null) return false; + if (imdbId != null ? !imdbId.equals(movie.imdbId) : movie.imdbId != null) + return false; return title != null ? title.equals(movie.title) : movie.title == null; } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index bbb3b1e953..6a0699874a 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -13,78 +13,71 @@ import java.util.stream.Collectors; @Path("/movies") public class MovieCrudService { - private Map inventory = new HashMap(); - + private Map inventory = new HashMap(); @GET @Path("/getinfo") - @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Movie movieByImdbId(@QueryParam("imdbId") String imdbId){ + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + public Movie movieByImdbId(@QueryParam("imdbId") String imdbId) { System.out.println("*** Calling getinfo for a given ImdbID***"); - if(inventory.containsKey(imdbId)){ + if (inventory.containsKey(imdbId)) { return inventory.get(imdbId); - }else return null; + } else + return null; } - @POST @Path("/addmovie") - @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Response addMovie(Movie movie){ + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + public Response addMovie(Movie movie) { System.out.println("*** Calling addMovie ***"); - if (null!=inventory.get(movie.getImdbId())){ - return Response.status(Response.Status.NOT_MODIFIED) - .entity("Movie is Already in the database.").build(); + if (null != inventory.get(movie.getImdbId())) { + return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is Already in the database.").build(); } - inventory.put(movie.getImdbId(),movie); + inventory.put(movie.getImdbId(), movie); return Response.status(Response.Status.CREATED).build(); } - @PUT @Path("/updatemovie") - @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) - public Response updateMovie(Movie movie){ + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + public Response updateMovie(Movie movie) { System.out.println("*** Calling updateMovie ***"); - if (null==inventory.get(movie.getImdbId())){ - return Response.status(Response.Status.NOT_MODIFIED) - .entity("Movie is not in the database.\nUnable to Update").build(); + if (null == inventory.get(movie.getImdbId())) { + return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is not in the database.\nUnable to Update").build(); } - inventory.put(movie.getImdbId(),movie); + inventory.put(movie.getImdbId(), movie); return Response.status(Response.Status.OK).build(); } - @DELETE @Path("/deletemovie") - public Response deleteMovie(@QueryParam("imdbId") String imdbId){ + public Response deleteMovie(@QueryParam("imdbId") String imdbId) { System.out.println("*** Calling deleteMovie ***"); - if (null==inventory.get(imdbId)){ - return Response.status(Response.Status.NOT_FOUND) - .entity("Movie is not in the database.\nUnable to Delete").build(); + if (null == inventory.get(imdbId)) { + return Response.status(Response.Status.NOT_FOUND).entity("Movie is not in the database.\nUnable to Delete").build(); } inventory.remove(imdbId); return Response.status(Response.Status.OK).build(); } - @GET @Path("/listmovies") - @Produces({"application/json"}) - public List listMovies(){ + @Produces({ "application/json" }) + public List listMovies() { return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new)); diff --git a/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml b/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml index 84d75934a7..7879603d19 100644 --- a/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml +++ b/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml @@ -1,16 +1,16 @@ - - - - + + + + - - - - - + + + + + - - + + \ No newline at end of file diff --git a/RestEasy Example/src/main/webapp/WEB-INF/web.xml b/RestEasy Example/src/main/webapp/WEB-INF/web.xml index 1e7f64004d..d5f00293f4 100644 --- a/RestEasy Example/src/main/webapp/WEB-INF/web.xml +++ b/RestEasy Example/src/main/webapp/WEB-INF/web.xml @@ -1,13 +1,13 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> - RestEasy Example + RestEasy Example - - resteasy.servlet.mapping.prefix - /rest - + + resteasy.servlet.mapping.prefix + /rest + \ No newline at end of file diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java index faa39e0199..3760ae2415 100644 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java @@ -21,14 +21,14 @@ import java.util.Locale; public class RestEasyClientTest { - Movie transformerMovie=null; - Movie batmanMovie=null; - ObjectMapper jsonMapper=null; + Movie transformerMovie = null; + Movie batmanMovie = null; + ObjectMapper jsonMapper = null; @Before public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { - jsonMapper=new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + jsonMapper = new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); jsonMapper.setDateFormat(sdf); @@ -69,7 +69,7 @@ public class RestEasyClientTest { @Test public void testMovieByImdbId() { - String transformerImdbId="tt0418279"; + String transformerImdbId = "tt0418279"; ResteasyClient client = new ResteasyClientBuilder().build(); ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); @@ -82,7 +82,6 @@ public class RestEasyClientTest { System.out.println(movies); } - @Test public void testAddMovie() { @@ -99,10 +98,9 @@ public class RestEasyClientTest { } moviesResponse.close(); - System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); + System.out.println("Response Code: " + Response.Status.OK.getStatusCode()); } - @Test public void testDeleteMovi1e() { @@ -116,14 +114,13 @@ public class RestEasyClientTest { if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { System.out.println(moviesResponse.readEntity(String.class)); - throw new RuntimeException("Failed : HTTP error code : " + moviesResponse.getStatus()); + throw new RuntimeException("Failed : HTTP error code : " + moviesResponse.getStatus()); } moviesResponse.close(); - System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); + System.out.println("Response Code: " + Response.Status.OK.getStatusCode()); } - @Test public void testUpdateMovie() { @@ -137,11 +134,11 @@ public class RestEasyClientTest { moviesResponse = simple.updateMovie(batmanMovie); if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { - System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); } moviesResponse.close(); - System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); + System.out.println("Response Code: " + Response.Status.OK.getStatusCode()); } } \ No newline at end of file diff --git a/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json b/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json index 28061d5bf9..173901c948 100644 --- a/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json +++ b/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json @@ -16,7 +16,7 @@ "metascore": "66", "imdbRating": "7.6", "imdbVotes": "256,000", - "imdbID": "tt0096895", + "imdbId": "tt0096895", "type": "movie", "response": "True" } \ No newline at end of file diff --git a/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json b/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json index a3b033a8ba..a4fd061a82 100644 --- a/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json +++ b/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json @@ -16,7 +16,7 @@ "metascore": "61", "imdbRating": "7.1", "imdbVotes": "492,225", - "imdbID": "tt0418279", + "imdbId": "tt0418279", "type": "movie", "response": "True" } \ No newline at end of file From 81f498407142bcc91071110ef20d0ee9a5b5da98 Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Mon, 8 Feb 2016 16:08:15 +0100 Subject: [PATCH 83/84] CleanUp and reformatting Code. --- .../src/main/java/com/baeldung/server/MovieCrudService.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index 6a0699874a..b7f3215f3f 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -26,7 +26,6 @@ public class MovieCrudService { return inventory.get(imdbId); } else return null; - } @POST @@ -41,7 +40,6 @@ public class MovieCrudService { } inventory.put(movie.getImdbId(), movie); - return Response.status(Response.Status.CREATED).build(); } @@ -55,9 +53,9 @@ public class MovieCrudService { if (null == inventory.get(movie.getImdbId())) { return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is not in the database.\nUnable to Update").build(); } + inventory.put(movie.getImdbId(), movie); return Response.status(Response.Status.OK).build(); - } @DELETE @@ -80,7 +78,6 @@ public class MovieCrudService { public List listMovies() { return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new)); - } } From 1c8e8a2e0c5683bcc0b992e631807b016d1d45a6 Mon Sep 17 00:00:00 2001 From: Giuseppe Bueti Date: Sat, 13 Feb 2016 23:48:11 +0100 Subject: [PATCH 84/84] Formatting and many fields removed from Movie class --- .../main/java/com/baeldung/model/Movie.java | 188 ++---------------- .../WEB-INF/jboss-deployment-structure.xml | 3 - .../baeldung/server/RestEasyClientTest.java | 2 +- .../com/baeldung/server/movies/batman.json | 20 +- .../baeldung/server/movies/transformer.json | 20 +- 5 files changed, 20 insertions(+), 213 deletions(-) diff --git a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java index 5aade4591a..408f2144fb 100644 --- a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -5,195 +5,33 @@ import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) -@XmlType(name = "movie", propOrder = { "actors", "awards", "country", "director", "genre", "imdbId", "imdbRating", "imdbVotes", "language", "metascore", "plot", "poster", "rated", "released", "response", "runtime", "title", "type", "writer", "year" }) +@XmlType(name = "movie", propOrder = { "imdbId", "title" }) public class Movie { - protected String actors; - protected String awards; - protected String country; - protected String director; - protected String genre; protected String imdbId; - protected String imdbRating; - protected String imdbVotes; - protected String language; - protected String metascore; - protected String plot; - protected String poster; - protected String rated; - protected String released; - protected String response; - protected String runtime; protected String title; - protected String type; - protected String writer; - protected String year; - public String getActors() { - return actors; + public Movie(String imdbId, String title) { + this.imdbId = imdbId; + this.title = title; } - public void setActors(String value) { - this.actors = value; - } - - public String getAwards() { - return awards; - } - - public void setAwards(String value) { - this.awards = value; - } - - public String getCountry() { - return country; - } - - public void setCountry(String value) { - this.country = value; - } - - public String getDirector() { - return director; - } - - public void setDirector(String value) { - this.director = value; - } - - public String getGenre() { - return genre; - } - - public void setGenre(String value) { - this.genre = value; - } + public Movie() {} public String getImdbId() { return imdbId; } - public void setImdbId(String value) { - this.imdbId = value; - } - - public String getImdbRating() { - return imdbRating; - } - - public void setImdbRating(String value) { - this.imdbRating = value; - } - - public String getImdbVotes() { - return imdbVotes; - } - - public void setImdbVotes(String value) { - this.imdbVotes = value; - } - - public String getLanguage() { - return language; - } - - public void setLanguage(String value) { - this.language = value; - } - - public String getMetascore() { - return metascore; - } - - public void setMetascore(String value) { - this.metascore = value; - } - - public String getPlot() { - return plot; - } - - public void setPlot(String value) { - this.plot = value; - } - - public String getPoster() { - return poster; - } - - public void setPoster(String value) { - this.poster = value; - } - - public String getRated() { - return rated; - } - - public void setRated(String value) { - this.rated = value; - } - - public String getReleased() { - return released; - } - - public void setReleased(String value) { - this.released = value; - } - - public String getResponse() { - return response; - } - - public void setResponse(String value) { - this.response = value; - } - - public String getRuntime() { - return runtime; - } - - public void setRuntime(String value) { - this.runtime = value; + public void setImdbId(String imdbId) { + this.imdbId = imdbId; } public String getTitle() { return title; } - public void setTitle(String value) { - this.title = value; - } - - public String getType() { - return type; - } - - public void setType(String value) { - this.type = value; - } - - public String getWriter() { - return writer; - } - - public void setWriter(String value) { - this.writer = value; - } - - public String getYear() { - return year; - } - - public void setYear(String value) { - this.year = value; - } - - @Override - public String toString() { - return "Movie{" + "actors='" + actors + '\'' + ", awards='" + awards + '\'' + ", country='" + country + '\'' + ", director='" + director + '\'' + ", genre='" + genre + '\'' + ", imdbId='" + imdbId + '\'' + ", imdbRating='" + imdbRating + '\'' - + ", imdbVotes='" + imdbVotes + '\'' + ", language='" + language + '\'' + ", metascore='" + metascore + '\'' + ", poster='" + poster + '\'' + ", rated='" + rated + '\'' + ", released='" + released + '\'' + ", response='" + response + '\'' - + ", runtime='" + runtime + '\'' + ", title='" + title + '\'' + ", type='" + type + '\'' + ", writer='" + writer + '\'' + ", year='" + year + '\'' + '}'; + public void setTitle(String title) { + this.title = title; } @Override @@ -217,4 +55,12 @@ public class Movie { result = 31 * result + (title != null ? title.hashCode() : 0); return result; } + + @Override + public String toString() { + return "Movie{" + + "imdbId='" + imdbId + '\'' + + ", title='" + title + '\'' + + '}'; + } } diff --git a/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml b/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml index 7879603d19..e5b893e56a 100644 --- a/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml +++ b/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml @@ -3,14 +3,11 @@ - - - \ No newline at end of file diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java index 3760ae2415..2a13465ebc 100644 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClientTest.java @@ -130,7 +130,7 @@ public class RestEasyClientTest { Response moviesResponse = simple.addMovie(batmanMovie); moviesResponse.close(); - batmanMovie.setImdbVotes("300,000"); + batmanMovie.setTitle("Batman Begins"); moviesResponse = simple.updateMovie(batmanMovie); if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { diff --git a/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json b/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json index 173901c948..82aaaa8f40 100644 --- a/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json +++ b/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json @@ -1,22 +1,4 @@ { "title": "Batman", - "year": "1989", - "rated": "PG-13", - "released": "23 Jun 1989", - "runtime": "126 min", - "genre": "Action, Adventure", - "director": "Tim Burton", - "writer": "Bob Kane (Batman characters), Sam Hamm (story), Sam Hamm (screenplay), Warren Skaaren (screenplay)", - "actors": "Michael Keaton, Jack Nicholson, Kim Basinger, Robert Wuhl", - "plot": "The Dark Knight of Gotham City begins his war on crime with his first major enemy being the clownishly homicidal Joker.", - "language": "English, French", - "country": "USA, UK", - "awards": "Won 1 Oscar. Another 9 wins & 22 nominations.", - "poster": "http://ia.media-imdb.com/images/M/MV5BMTYwNjAyODIyMF5BMl5BanBnXkFtZTYwNDMwMDk2._V1_SX300.jpg", - "metascore": "66", - "imdbRating": "7.6", - "imdbVotes": "256,000", - "imdbId": "tt0096895", - "type": "movie", - "response": "True" + "imdbId": "tt0096895" } \ No newline at end of file diff --git a/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json b/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json index a4fd061a82..634cefc73c 100644 --- a/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json +++ b/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json @@ -1,22 +1,4 @@ { "title": "Transformers", - "year": "2007", - "rated": "PG-13", - "released": "03 Jul 2007", - "runtime": "144 min", - "genre": "Action, Adventure, Sci-Fi", - "director": "Michael Bay", - "writer": "Roberto Orci (screenplay), Alex Kurtzman (screenplay), John Rogers (story), Roberto Orci (story), Alex Kurtzman (story)", - "actors": "Shia LaBeouf, Megan Fox, Josh Duhamel, Tyrese Gibson", - "plot": "A long time ago, far away on the planet of Cybertron, a war is being waged between the noble Autobots (led by the wise Optimus Prime) and the devious Decepticons (commanded by the dreaded Megatron) for control over the Allspark, a mystical talisman that would grant unlimited power to whoever possesses it. The Autobots managed to smuggle the Allspark off the planet, but Megatron blasts off in search of it. He eventually tracks it to the planet of Earth (circa 1850), but his reckless desire for power sends him right into the Arctic Ocean, and the sheer cold forces him into a paralyzed state. His body is later found by Captain Archibald Witwicky, but before going into a comatose state Megatron uses the last of his energy to engrave into the Captain's glasses a map showing the location of the Allspark, and to send a transmission to Cybertron. Megatron is then carried away aboard the Captain's ship. A century later, Captain Witwicky's grandson Sam Witwicky (nicknamed Spike by his friends) buys his first car. To his shock, he discovers it to be Bumblebee, an Autobot in disguise who is to protect Spike, who possesses the Captain's glasses and the map engraved on them. But Bumblebee is not the only Transformer to have arrived on Earth - in the desert of Qatar, the Decepticons Blackout and Scorponok attack a U.S. military base, causing the Pentagon to send their special Sector Seven agents to capture all \"specimens of this alien race.\" Spike and his girlfriend Mikaela find themselves in the midst of a grand battle between the Autobots and the Decepticons, stretching from Hoover Dam all the way to Los Angeles. Meanwhile, deep inside Hoover Dam, the cryogenically stored body of Megatron awakens.", - "language": "English, Spanish", - "country": "USA", - "awards": "Nominated for 3 Oscars. Another 18 wins & 40 nominations.", - "poster": "http://ia.media-imdb.com/images/M/MV5BMTQwNjU5MzUzNl5BMl5BanBnXkFtZTYwMzc1MTI3._V1_SX300.jpg", - "metascore": "61", - "imdbRating": "7.1", - "imdbVotes": "492,225", - "imdbId": "tt0418279", - "type": "movie", - "response": "True" + "imdbId": "tt0418279" } \ No newline at end of file