REST stuff
This commit is contained in:
parent
e6c45b4dbf
commit
7d5609f422
@ -83,6 +83,22 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</pluginManagement>
|
</pluginManagement>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.wildfly.plugins</groupId>
|
||||||
|
<artifactId>wildfly-maven-plugin</artifactId>
|
||||||
|
<version>2.0.1.Final</version>
|
||||||
|
<configuration>
|
||||||
|
<force>true</force>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>install</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>deploy</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package at.technikumwien.movies;
|
||||||
|
|
||||||
|
import javax.persistence.EntityNotFoundException;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.ws.rs.ext.ExceptionMapper;
|
||||||
|
import javax.ws.rs.ext.Provider;
|
||||||
|
|
||||||
|
@Provider
|
||||||
|
public class EntityNotFoundExceptionMapper implements ExceptionMapper<EntityNotFoundException> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Response toResponse(EntityNotFoundException e) {
|
||||||
|
return Response.status(Response.Status.NOT_FOUND).build();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
package at.technikumwien.movies;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.ws.rs.*;
|
||||||
|
import javax.ws.rs.core.Context;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.ws.rs.core.UriInfo;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Path("/movie")
|
||||||
|
public class MovieResource {
|
||||||
|
@Inject
|
||||||
|
private MoviesService moviesService;
|
||||||
|
|
||||||
|
@Context
|
||||||
|
private UriInfo uriInfo;
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Produces({
|
||||||
|
MediaType.APPLICATION_JSON,
|
||||||
|
MediaType.APPLICATION_XML
|
||||||
|
})
|
||||||
|
public List<Movie> retrieveAll() {
|
||||||
|
return moviesService.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Produces({
|
||||||
|
MediaType.APPLICATION_JSON,
|
||||||
|
MediaType.APPLICATION_XML
|
||||||
|
})
|
||||||
|
@Path("/{id}")
|
||||||
|
public Movie retrieve(@PathParam("id") long id) {
|
||||||
|
return moviesService.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DELETE
|
||||||
|
@Path("/{id}")
|
||||||
|
public void delete(@PathParam("id") long id) {
|
||||||
|
moviesService.removeById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Consumes({
|
||||||
|
MediaType.APPLICATION_JSON,
|
||||||
|
MediaType.APPLICATION_XML
|
||||||
|
})
|
||||||
|
public Response create(Movie movie) {
|
||||||
|
movie.setId(null); // Make sure that a new movie is added, not overwriting existing one
|
||||||
|
List<Movie> newMovies = moviesService.save(List.of(movie));
|
||||||
|
|
||||||
|
URI location = uriInfo.getAbsolutePathBuilder().path(newMovies.get(0).getId().toString()).build();
|
||||||
|
|
||||||
|
return Response.created(location).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PUT
|
||||||
|
@Consumes({
|
||||||
|
MediaType.APPLICATION_JSON,
|
||||||
|
MediaType.APPLICATION_XML
|
||||||
|
})
|
||||||
|
@Path("/{id}")
|
||||||
|
public void update(@PathParam("id") long id, Movie movie) {
|
||||||
|
movie.setId(id); // Make sure that a new movie is added, not overwriting existing one
|
||||||
|
moviesService.save(List.of(movie));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package at.technikumwien.movies;
|
||||||
|
|
||||||
|
import javax.ws.rs.ApplicationPath;
|
||||||
|
import javax.ws.rs.core.Application;
|
||||||
|
|
||||||
|
@ApplicationPath("/resources")
|
||||||
|
public class MovieResourceApplication extends Application {
|
||||||
|
}
|
@ -5,6 +5,7 @@ import javax.ejb.*;
|
|||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.EntityNotFoundException;
|
import javax.persistence.EntityNotFoundException;
|
||||||
import javax.persistence.PersistenceContext;
|
import javax.persistence.PersistenceContext;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@ -68,7 +69,7 @@ public class MoviesService {
|
|||||||
|
|
||||||
// TODO maybe check if the movie already exists?
|
// TODO maybe check if the movie already exists?
|
||||||
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
|
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
|
||||||
public void save(List<Movie> movies) {
|
public List<Movie> save(List<Movie> movies) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (Movie movie : movies) {
|
for (Movie movie : movies) {
|
||||||
@ -90,7 +91,7 @@ public class MoviesService {
|
|||||||
{
|
{
|
||||||
LOGGER.info("Rollback! Movie studio: " + movieStudio);
|
LOGGER.info("Rollback! Movie studio: " + movieStudio);
|
||||||
context.setRollbackOnly();
|
context.setRollbackOnly();
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get actor for the movie from the database
|
// Get actor for the movie from the database
|
||||||
@ -112,17 +113,23 @@ public class MoviesService {
|
|||||||
{
|
{
|
||||||
LOGGER.info("Rollback! Movie actor: " + movieActor);
|
LOGGER.info("Rollback! Movie actor: " + movieActor);
|
||||||
context.setRollbackOnly();
|
context.setRollbackOnly();
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<Movie> addedMovies = new ArrayList<>();
|
||||||
|
|
||||||
LOGGER.info("save() >> movies" + movies);
|
LOGGER.info("save() >> movies" + movies);
|
||||||
for (Movie movieToImport : movies) {
|
for (Movie movieToImport : movies) {
|
||||||
em.merge(movieToImport);
|
addedMovies.add(em.merge(movieToImport));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return addedMovies;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user