Folder renamed e client code reverted
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
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;
|
||||
import java.util.List;
|
||||
|
||||
@Path("/movies")
|
||||
public interface ServicesInterface {
|
||||
|
||||
@GET
|
||||
@Path("/getinfo")
|
||||
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||
Movie movieByImdbId(@QueryParam("imdbId") String imdbId);
|
||||
|
||||
@GET
|
||||
@Path("/listmovies")
|
||||
@Produces({ "application/json" })
|
||||
List<Movie> listMovies();
|
||||
|
||||
@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);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
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 = { "imdbId", "title" })
|
||||
public class Movie {
|
||||
|
||||
protected String imdbId;
|
||||
protected String title;
|
||||
|
||||
public Movie(String imdbId, String title) {
|
||||
this.imdbId = imdbId;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Movie() {}
|
||||
|
||||
public String getImdbId() {
|
||||
return imdbId;
|
||||
}
|
||||
|
||||
public void setImdbId(String imdbId) {
|
||||
this.imdbId = imdbId;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Movie{" +
|
||||
"imdbId='" + imdbId + '\'' +
|
||||
", title='" + title + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
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;
|
||||
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<String, Movie> inventory = new HashMap<String, Movie>();
|
||||
|
||||
@GET
|
||||
@Path("/getinfo")
|
||||
@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)) {
|
||||
return inventory.get(imdbId);
|
||||
} else
|
||||
return null;
|
||||
}
|
||||
|
||||
@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<Movie> listMovies() {
|
||||
|
||||
return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.server;
|
||||
|
||||
import javax.ws.rs.ApplicationPath;
|
||||
import javax.ws.rs.core.Application;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ApplicationPath("/rest")
|
||||
public class RestEasyServices extends Application {
|
||||
|
||||
private Set<Object> singletons = new HashSet<Object>();
|
||||
|
||||
public RestEasyServices() {
|
||||
singletons.add(new MovieCrudService());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Object> getSingletons() {
|
||||
return singletons;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Class<?>> getClasses() {
|
||||
return super.getClasses();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getProperties() {
|
||||
return super.getProperties();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
<configuration scan="true" scanPeriod="10 seconds">
|
||||
|
||||
</configuration>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0"?>
|
||||
<jboss-deployment-structure>
|
||||
<deployment>
|
||||
<exclude-subsystems>
|
||||
<subsystem name="resteasy"/>
|
||||
</exclude-subsystems>
|
||||
<exclusions>
|
||||
<module name="javaee.api"/><module name="javax.ws.rs.api"/>
|
||||
<module name="org.jboss.resteasy.resteasy-jaxrs"/>
|
||||
</exclusions>
|
||||
<local-last value="true"/>
|
||||
</deployment>
|
||||
</jboss-deployment-structure>
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 4.2//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd">
|
||||
<jboss-web>
|
||||
</jboss-web>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
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">
|
||||
|
||||
<display-name>RestEasy Example</display-name>
|
||||
|
||||
<context-param>
|
||||
<param-name>resteasy.servlet.mapping.prefix</param-name>
|
||||
<param-value>/rest</param-value>
|
||||
</context-param>
|
||||
|
||||
</web-app>
|
||||
@@ -0,0 +1,144 @@
|
||||
package com.baeldung.server;
|
||||
|
||||
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;
|
||||
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.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;
|
||||
|
||||
@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) {
|
||||
throw new RuntimeException("Test is going to die ...", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListAllMovies() {
|
||||
|
||||
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();
|
||||
|
||||
List<Movie> movies = simple.listMovies();
|
||||
System.out.println(movies);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMovieByImdbId() {
|
||||
|
||||
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);
|
||||
|
||||
Response moviesResponse = simple.addMovie(transformerMovie);
|
||||
moviesResponse.close();
|
||||
|
||||
Movie movies = simple.movieByImdbId(transformerImdbId);
|
||||
System.out.println(movies);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddMovie() {
|
||||
|
||||
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);
|
||||
|
||||
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 testDeleteMovi1e() {
|
||||
|
||||
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.deleteMovie(batmanMovie.getImdbId());
|
||||
|
||||
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() {
|
||||
|
||||
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();
|
||||
batmanMovie.setTitle("Batman Begins");
|
||||
moviesResponse = simple.updateMovie(batmanMovie);
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"title": "Batman",
|
||||
"imdbId": "tt0096895"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"title": "Transformers",
|
||||
"imdbId": "tt0418279"
|
||||
}
|
||||
Reference in New Issue
Block a user