From ae772727ce8e66289c6e883c22335d17fbdbdc43 Mon Sep 17 00:00:00 2001 From: "giuseppe.bueti" Date: Sat, 30 Jan 2016 17:48:56 +0100 Subject: [PATCH] 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