CleanUp and reformatting Code; dependency fix in pom.xml

This commit is contained in:
giuseppe.bueti 2016-02-08 14:00:11 +01:00
parent 03b6cbf3e5
commit c3ef41a10d
9 changed files with 125 additions and 205 deletions

View File

@ -1,104 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>resteasy-tutorial</artifactId> <artifactId>resteasy-tutorial</artifactId>
<version>1.0</version> <version>1.0</version>
<packaging>war</packaging> <packaging>war</packaging>
<repositories> <properties>
<repository> <resteasy.version>3.0.14.Final</resteasy.version>
<id>jboss</id> </properties>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<properties> <build>
<resteasy.version>3.0.14.Final</resteasy.version> <finalName>RestEasyTutorial</finalName>
<resteasy.scope>runtime</resteasy.scope> <plugins>
</properties> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<build> <dependencies>
<finalName>RestEasyTutorial</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies> <!-- core library -->
<dependency> <dependency>
<groupId>org.jboss.resteasy</groupId> <groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId> <artifactId>resteasy-servlet-initializer</artifactId>
<version>3.0.12.Final</version> <version>${resteasy.version}</version>
<scope>${resteasy.scope}</scope> </dependency>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>${resteasy.version}</version>
<scope>${resteasy.scope}</scope>
<exclusions>
<exclusion>
<artifactId>jboss-jaxrs-api_2.0_spec</artifactId>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>${resteasy.version}</version>
<scope>${resteasy.scope}</scope>
</dependency>
<dependency> <dependency>
<groupId>javax.ws.rs</groupId> <groupId>org.jboss.resteasy</groupId>
<artifactId>javax.ws.rs-api</artifactId> <artifactId>resteasy-client</artifactId>
<version>2.0.1</version> <version>${resteasy.version}</version>
</dependency> </dependency>
<dependency> <!-- Optional library -->
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>${resteasy.version}</version>
<scope>${resteasy.scope}</scope>
</dependency>
<dependency> <dependency>
<groupId>org.jboss.resteasy</groupId> <groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId> <artifactId>resteasy-jaxb-provider</artifactId>
<version>${resteasy.version}</version> <version>${resteasy.version}</version>
<scope>${resteasy.scope}</scope> </dependency>
</dependency>
<!-- Junit Library --> <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency> <!-- Junit Library -->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
</dependency>
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>junit</groupId>
<artifactId>commons-io</artifactId> <artifactId>junit</artifactId>
<version>2.4</version> <version>4.4</version>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</dependencies>
</project> </project>

View File

@ -9,35 +9,28 @@ import java.util.List;
@Path("/movies") @Path("/movies")
public interface ServicesInterface { public interface ServicesInterface {
@GET @GET
@Path("/getinfo") @Path("/getinfo")
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
Movie movieByImdbId(@QueryParam("imdbId") String imdbId); Movie movieByImdbId(@QueryParam("imdbId") String imdbId);
@GET @GET
@Path("/listmovies") @Path("/listmovies")
@Produces({"application/json"}) @Produces({ "application/json" })
List<Movie> listMovies(); List<Movie> listMovies();
@POST @POST
@Path("/addmovie") @Path("/addmovie")
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
Response addMovie(Movie movie); Response addMovie(Movie movie);
@PUT @PUT
@Path("/updatemovie") @Path("/updatemovie")
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
Response updateMovie(Movie movie); Response updateMovie(Movie movie);
@DELETE @DELETE
@Path("/deletemovie") @Path("/deletemovie")
Response deleteMovie(@QueryParam("imdbId") String imdbID); Response deleteMovie(@QueryParam("imdbId") String imdbID);
} }

View File

@ -5,28 +5,7 @@ import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "movie", propOrder = { @XmlType(name = "movie", propOrder = { "actors", "awards", "country", "director", "genre", "imdbId", "imdbRating", "imdbVotes", "language", "metascore", "plot", "poster", "rated", "released", "response", "runtime", "title", "type", "writer", "year" })
"actors",
"awards",
"country",
"director",
"genre",
"imdbId",
"imdbRating",
"imdbVotes",
"language",
"metascore",
"plot",
"poster",
"rated",
"released",
"response",
"runtime",
"title",
"type",
"writer",
"year"
})
public class Movie { public class Movie {
protected String actors; protected String actors;
@ -212,37 +191,22 @@ public class Movie {
@Override @Override
public String toString() { public String toString() {
return "Movie{" + return "Movie{" + "actors='" + actors + '\'' + ", awards='" + awards + '\'' + ", country='" + country + '\'' + ", director='" + director + '\'' + ", genre='" + genre + '\'' + ", imdbId='" + imdbId + '\'' + ", imdbRating='" + imdbRating + '\''
"actors='" + actors + '\'' + + ", imdbVotes='" + imdbVotes + '\'' + ", language='" + language + '\'' + ", metascore='" + metascore + '\'' + ", poster='" + poster + '\'' + ", rated='" + rated + '\'' + ", released='" + released + '\'' + ", response='" + response + '\''
", awards='" + awards + '\'' + + ", runtime='" + runtime + '\'' + ", title='" + title + '\'' + ", type='" + type + '\'' + ", writer='" + writer + '\'' + ", year='" + year + '\'' + '}';
", 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 @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o)
if (o == null || getClass() != o.getClass()) return false; return true;
if (o == null || getClass() != o.getClass())
return false;
Movie movie = (Movie) o; 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; return title != null ? title.equals(movie.title) : movie.title == null;
} }

View File

@ -13,78 +13,71 @@ import java.util.stream.Collectors;
@Path("/movies") @Path("/movies")
public class MovieCrudService { public class MovieCrudService {
private Map<String,Movie> inventory = new HashMap<String, Movie>(); private Map<String, Movie> inventory = new HashMap<String, Movie>();
@GET @GET
@Path("/getinfo") @Path("/getinfo")
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @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***"); System.out.println("*** Calling getinfo for a given ImdbID***");
if(inventory.containsKey(imdbId)){ if (inventory.containsKey(imdbId)) {
return inventory.get(imdbId); return inventory.get(imdbId);
}else return null; } else
return null;
} }
@POST @POST
@Path("/addmovie") @Path("/addmovie")
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response addMovie(Movie movie){ public Response addMovie(Movie movie) {
System.out.println("*** Calling addMovie ***"); System.out.println("*** Calling addMovie ***");
if (null!=inventory.get(movie.getImdbId())){ if (null != inventory.get(movie.getImdbId())) {
return Response.status(Response.Status.NOT_MODIFIED) return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is Already in the database.").build();
.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(); return Response.status(Response.Status.CREATED).build();
} }
@PUT @PUT
@Path("/updatemovie") @Path("/updatemovie")
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response updateMovie(Movie movie){ public Response updateMovie(Movie movie) {
System.out.println("*** Calling updateMovie ***"); System.out.println("*** Calling updateMovie ***");
if (null==inventory.get(movie.getImdbId())){ if (null == inventory.get(movie.getImdbId())) {
return Response.status(Response.Status.NOT_MODIFIED) return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is not in the database.\nUnable to Update").build();
.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(); return Response.status(Response.Status.OK).build();
} }
@DELETE @DELETE
@Path("/deletemovie") @Path("/deletemovie")
public Response deleteMovie(@QueryParam("imdbId") String imdbId){ public Response deleteMovie(@QueryParam("imdbId") String imdbId) {
System.out.println("*** Calling deleteMovie ***"); System.out.println("*** Calling deleteMovie ***");
if (null==inventory.get(imdbId)){ if (null == inventory.get(imdbId)) {
return Response.status(Response.Status.NOT_FOUND) return Response.status(Response.Status.NOT_FOUND).entity("Movie is not in the database.\nUnable to Delete").build();
.entity("Movie is not in the database.\nUnable to Delete").build();
} }
inventory.remove(imdbId); inventory.remove(imdbId);
return Response.status(Response.Status.OK).build(); return Response.status(Response.Status.OK).build();
} }
@GET @GET
@Path("/listmovies") @Path("/listmovies")
@Produces({"application/json"}) @Produces({ "application/json" })
public List<Movie> listMovies(){ public List<Movie> listMovies() {
return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new)); return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new));

View File

@ -1,16 +1,16 @@
<jboss-deployment-structure> <jboss-deployment-structure>
<deployment> <deployment>
<exclude-subsystems> <exclude-subsystems>
<subsystem name="resteasy" /> <subsystem name="resteasy" />
</exclude-subsystems> </exclude-subsystems>
<exclusions> <exclusions>
<module name="javaee.api" /> <module name="javaee.api" />
<module name="javax.ws.rs.api"/> <module name="javax.ws.rs.api" />
<module name="org.jboss.resteasy.resteasy-jaxrs" /> <module name="org.jboss.resteasy.resteasy-jaxrs" />
</exclusions> </exclusions>
<local-last value="true" /> <local-last value="true" />
</deployment> </deployment>
</jboss-deployment-structure> </jboss-deployment-structure>

View File

@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>RestEasy Example</display-name> <display-name>RestEasy Example</display-name>
<context-param> <context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name> <param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value> <param-value>/rest</param-value>
</context-param> </context-param>
</web-app> </web-app>

View File

@ -21,14 +21,14 @@ import java.util.Locale;
public class RestEasyClientTest { public class RestEasyClientTest {
Movie transformerMovie=null; Movie transformerMovie = null;
Movie batmanMovie=null; Movie batmanMovie = null;
ObjectMapper jsonMapper=null; ObjectMapper jsonMapper = null;
@Before @Before
public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { 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); jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
jsonMapper.setDateFormat(sdf); jsonMapper.setDateFormat(sdf);
@ -69,7 +69,7 @@ public class RestEasyClientTest {
@Test @Test
public void testMovieByImdbId() { public void testMovieByImdbId() {
String transformerImdbId="tt0418279"; String transformerImdbId = "tt0418279";
ResteasyClient client = new ResteasyClientBuilder().build(); ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); 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); System.out.println(movies);
} }
@Test @Test
public void testAddMovie() { public void testAddMovie() {
@ -99,10 +98,9 @@ public class RestEasyClientTest {
} }
moviesResponse.close(); moviesResponse.close();
System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); System.out.println("Response Code: " + Response.Status.OK.getStatusCode());
} }
@Test @Test
public void testDeleteMovi1e() { public void testDeleteMovi1e() {
@ -116,14 +114,13 @@ public class RestEasyClientTest {
if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) {
System.out.println(moviesResponse.readEntity(String.class)); 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(); moviesResponse.close();
System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); System.out.println("Response Code: " + Response.Status.OK.getStatusCode());
} }
@Test @Test
public void testUpdateMovie() { public void testUpdateMovie() {
@ -137,11 +134,11 @@ public class RestEasyClientTest {
moviesResponse = simple.updateMovie(batmanMovie); moviesResponse = simple.updateMovie(batmanMovie);
if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { 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(); moviesResponse.close();
System.out.println("Response Code: "+Response.Status.OK.getStatusCode()); System.out.println("Response Code: " + Response.Status.OK.getStatusCode());
} }
} }

View File

@ -16,7 +16,7 @@
"metascore": "66", "metascore": "66",
"imdbRating": "7.6", "imdbRating": "7.6",
"imdbVotes": "256,000", "imdbVotes": "256,000",
"imdbID": "tt0096895", "imdbId": "tt0096895",
"type": "movie", "type": "movie",
"response": "True" "response": "True"
} }

View File

@ -16,7 +16,7 @@
"metascore": "61", "metascore": "61",
"imdbRating": "7.1", "imdbRating": "7.1",
"imdbVotes": "492,225", "imdbVotes": "492,225",
"imdbID": "tt0418279", "imdbId": "tt0418279",
"type": "movie", "type": "movie",
"response": "True" "response": "True"
} }