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

@ -15,6 +15,16 @@ 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 })
@ -51,7 +61,8 @@ public class MovieCrudService {
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();
return Response.status(Response.Status.NOT_MODIFIED)
.entity("Movie is not in the database.\nUnable to Update").build();
}
inventory.put(movie.getImdbId(), movie);
@ -65,7 +76,8 @@ public class MovieCrudService {
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();
return Response.status(Response.Status.NOT_FOUND).entity("Movie is not in the database.\nUnable to Delete")
.build();
}
inventory.remove(imdbId);

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);
});
};