Skip to content

Commit c3764e3

Browse files
AbhinabKanrarmaibin
authored andcommitted
CORS in JAX-RS (eugenp#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
1 parent 2aca6e0 commit c3764e3

3 files changed

Lines changed: 116 additions & 66 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.filter;
2+
3+
import java.io.IOException;
4+
5+
import javax.ws.rs.container.ContainerRequestContext;
6+
import javax.ws.rs.container.ContainerResponseContext;
7+
import javax.ws.rs.container.ContainerResponseFilter;
8+
import javax.ws.rs.ext.Provider;
9+
10+
@Provider
11+
public class CorsFilter implements ContainerResponseFilter {
12+
13+
@Override
14+
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
15+
throws IOException {
16+
responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
17+
responseContext.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");
18+
responseContext.getHeaders().add("Access-Control-Allow-Credentials", "true");
19+
responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
20+
}
21+
22+
}

‎resteasy/src/main/java/com/baeldung/server/MovieCrudService.java‎

Lines changed: 78 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,71 +13,83 @@
1313
@Path("/movies")
1414
public class MovieCrudService {
1515

16-
private Map<String, Movie> inventory = new HashMap<String, Movie>();
17-
18-
@GET
19-
@Path("/getinfo")
20-
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
21-
public Movie movieByImdbId(@QueryParam("imdbId") String imdbId) {
22-
23-
System.out.println("*** Calling getinfo for a given ImdbID***");
24-
25-
if (inventory.containsKey(imdbId)) {
26-
return inventory.get(imdbId);
27-
} else
28-
return null;
29-
}
30-
31-
@POST
32-
@Path("/addmovie")
33-
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
34-
public Response addMovie(Movie movie) {
35-
36-
System.out.println("*** Calling addMovie ***");
37-
38-
if (null != inventory.get(movie.getImdbId())) {
39-
return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is Already in the database.").build();
40-
}
41-
42-
inventory.put(movie.getImdbId(), movie);
43-
return Response.status(Response.Status.CREATED).build();
44-
}
45-
46-
@PUT
47-
@Path("/updatemovie")
48-
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
49-
public Response updateMovie(Movie movie) {
50-
51-
System.out.println("*** Calling updateMovie ***");
52-
53-
if (null == inventory.get(movie.getImdbId())) {
54-
return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is not in the database.\nUnable to Update").build();
55-
}
56-
57-
inventory.put(movie.getImdbId(), movie);
58-
return Response.status(Response.Status.OK).build();
59-
}
60-
61-
@DELETE
62-
@Path("/deletemovie")
63-
public Response deleteMovie(@QueryParam("imdbId") String imdbId) {
64-
65-
System.out.println("*** Calling deleteMovie ***");
66-
67-
if (null == inventory.get(imdbId)) {
68-
return Response.status(Response.Status.NOT_FOUND).entity("Movie is not in the database.\nUnable to Delete").build();
69-
}
70-
71-
inventory.remove(imdbId);
72-
return Response.status(Response.Status.OK).build();
73-
}
74-
75-
@GET
76-
@Path("/listmovies")
77-
@Produces({ "application/json" })
78-
public List<Movie> listMovies() {
79-
80-
return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new));
81-
}
16+
private Map<String, Movie> inventory = new HashMap<String, Movie>();
17+
18+
@GET
19+
@Path("/")
20+
@Produces({ MediaType.TEXT_PLAIN })
21+
public Response index() {
22+
return Response.status(200).header("Access-Control-Allow-Origin", "*")
23+
.header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization")
24+
.header("Access-Control-Allow-Credentials", "true")
25+
.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD").entity("").build();
26+
}
27+
28+
@GET
29+
@Path("/getinfo")
30+
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
31+
public Movie movieByImdbId(@QueryParam("imdbId") String imdbId) {
32+
33+
System.out.println("*** Calling getinfo for a given ImdbID***");
34+
35+
if (inventory.containsKey(imdbId)) {
36+
return inventory.get(imdbId);
37+
} else
38+
return null;
39+
}
40+
41+
@POST
42+
@Path("/addmovie")
43+
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
44+
public Response addMovie(Movie movie) {
45+
46+
System.out.println("*** Calling addMovie ***");
47+
48+
if (null != inventory.get(movie.getImdbId())) {
49+
return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is Already in the database.").build();
50+
}
51+
52+
inventory.put(movie.getImdbId(), movie);
53+
return Response.status(Response.Status.CREATED).build();
54+
}
55+
56+
@PUT
57+
@Path("/updatemovie")
58+
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
59+
public Response updateMovie(Movie movie) {
60+
61+
System.out.println("*** Calling updateMovie ***");
62+
63+
if (null == inventory.get(movie.getImdbId())) {
64+
return Response.status(Response.Status.NOT_MODIFIED)
65+
.entity("Movie is not in the database.\nUnable to Update").build();
66+
}
67+
68+
inventory.put(movie.getImdbId(), movie);
69+
return Response.status(Response.Status.OK).build();
70+
}
71+
72+
@DELETE
73+
@Path("/deletemovie")
74+
public Response deleteMovie(@QueryParam("imdbId") String imdbId) {
75+
76+
System.out.println("*** Calling deleteMovie ***");
77+
78+
if (null == inventory.get(imdbId)) {
79+
return Response.status(Response.Status.NOT_FOUND).entity("Movie is not in the database.\nUnable to Delete")
80+
.build();
81+
}
82+
83+
inventory.remove(imdbId);
84+
return Response.status(Response.Status.OK).build();
85+
}
86+
87+
@GET
88+
@Path("/listmovies")
89+
@Produces({ "application/json" })
90+
public List<Movie> listMovies() {
91+
92+
return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new));
93+
}
8294

8395
}

‎resteasy/src/main/webapp/script.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function call(url, type, data) {
2+
var request = $.ajax({
3+
url : url,
4+
method : "GET",
5+
data : (data) ? JSON.stringify(data) : "",
6+
dataType : type
7+
});
8+
9+
request.done(function(resp) {
10+
console.log(resp);
11+
});
12+
13+
request.fail(function(jqXHR, textStatus) {
14+
console.log("Request failed: " + textStatus);
15+
});
16+
};

0 commit comments

Comments
 (0)