Merge pull request #354 from giuseppebueti/master
RestEasy Tutorial Code
This commit is contained in:
commit
ac38b9528a
|
@ -0,0 +1,77 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>resteasy-tutorial</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<properties>
|
||||
<resteasy.version>3.0.14.Final</resteasy.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<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>
|
||||
<groupId>org.jboss.resteasy</groupId>
|
||||
<artifactId>resteasy-servlet-initializer</artifactId>
|
||||
<version>${resteasy.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jboss.resteasy</groupId>
|
||||
<artifactId>resteasy-client</artifactId>
|
||||
<version>${resteasy.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Optional library -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jboss.resteasy</groupId>
|
||||
<artifactId>resteasy-jaxb-provider</artifactId>
|
||||
<version>${resteasy.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jboss.resteasy</groupId>
|
||||
<artifactId>resteasy-jackson-provider</artifactId>
|
||||
<version>${resteasy.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Junit Library -->
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.4</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
|
@ -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>
|
Loading…
Reference in New Issue