Files
java-tutorials/resteasy/src/main/java/com/baeldung/server/MovieCrudService.java
T
Abhinab Kanrar c3764e3f44 CORS in JAX-RS (#1484)
* rest with spark java

* 4

* Update Application.java

* indentation changes

* spring @requestmapping shortcuts

* removing spring requestmapping and pushing spring-mvc-java

* Joining/Splitting Strings with Java and Stream API

* adding more join/split functionality

* changing package name

* testcase change

* adding webutils

* adding testcase for WebUtils and ServletRequestUtils

* adding testcase

* spring-security-stormpath

* adding ratpack module

* adding pom.xml

* adding following modules with updated testcase : DB, Filter, Json

* adding spring-boot custom banner tutorial

* changing banner format in plain text

* Delete banner.txt~

* Delete b.txt~

* CORS in JAX-RS
2017-03-24 23:02:42 +01:00

96 lines
2.6 KiB
Java

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("/")
@Produces({ MediaType.TEXT_PLAIN })
public Response index() {
return Response.status(200).header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization")
.header("Access-Control-Allow-Credentials", "true")
.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD").entity("").build();
}
@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));
}
}