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
This commit is contained in:
Abhinab Kanrar 2017-03-25 03:32:42 +05:30 committed by maibin
parent 2aca6e085b
commit c3764e3f44
3 changed files with 99 additions and 49 deletions

View File

@ -0,0 +1,22 @@
package com.baeldung.filter;
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;
@Provider
public class CorsFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
responseContext.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");
responseContext.getHeaders().add("Access-Control-Allow-Credentials", "true");
responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
}
}

View File

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

View File

@ -0,0 +1,16 @@
function call(url, type, data) {
var request = $.ajax({
url : url,
method : "GET",
data : (data) ? JSON.stringify(data) : "",
dataType : type
});
request.done(function(resp) {
console.log(resp);
});
request.fail(function(jqXHR, textStatus) {
console.log("Request failed: " + textStatus);
});
};