From c14e7ad1a17b34a60b74a4a4b63e8850567365d6 Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sat, 30 Jan 2016 17:48:56 +0100 Subject: [PATCH 01/13] 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/13] 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/13] 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 80b3e457f2eb16c2082381cf198c0be7c3b096a3 Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sun, 7 Feb 2016 20:29:30 +0100 Subject: [PATCH 04/13] 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 05/13] 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 06/13] 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 07/13] 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 08/13] 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 09/13] 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 10/13] 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 11/13] 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 12/13] 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 13/13] 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)); - } }