How to handle CORS using JAX-RS with Jersey

Note: Make sure to read the UPDATE at the bottom. The original answer includes a “lazy” implementation of the CORS filter With Jersey, to handle CORS, you can just use a ContainerResponseFilter. The ContainerResponseFilter for Jersey 1.x and 2.x are a bit different. Since you haven’t mentioned which version you’re using, I’ll post both. Make … Read more

Why use JAX-RS / Jersey?

Why use JAX-RS / Jersey? Short Answer Because it makes the development of RESTful services easier. Long Answer JAX-RS is a standard that makes it easy to create a RESTful service that can be deployed to any Java application server: GlassFish, WebLogic, WebSphere, JBoss, etc. JAX-RS is part of Java EE, and when JAX-RS is … Read more

What exactly is the ResourceConfig class in Jersey 2?

Standard JAX-RS uses an Application as its configuration class. ResourceConfig extends Application. There are three main ways (in a servlet container) to configure Jersey (JAX-RS): With only web.xml With both web.xml and an Application/ResourceConfig class With only an Application/ResourceConfig class annotated with @ApplicationPath. With only web.xml It is possible to configure the application in a … Read more

Jersey client: How to add a list as query parameter

@GET does support List of Strings Setup: Java : 1.7 Jersey version : 1.9 Resource @Path(“/v1/test”) Subresource: // receive List of Strings @GET @Path(“/receiveListOfStrings”) public Response receiveListOfStrings(@QueryParam(“list”) final List<String> list){ log.info(“receieved list of size=”+list.size()); return Response.ok().build(); } Jersey testcase @Test public void testReceiveListOfStrings() throws Exception { WebResource webResource = resource(); ClientResponse responseMsg = webResource.path(“/v1/test/receiveListOfStrings”) .queryParam(“list”, … Read more

How can I grab all query parameters in Jersey JaxRS?

You can access a single param via @QueryParam(“name”) or all of the params via the context: @POST public Response postSomething(@QueryParam(“name”) String name, @Context UriInfo uriInfo, String content) { MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(); String nameParam = queryParams.getFirst(“name”); } The key is the @Context jax-rs annotation, which can be used to access: UriInfo, Request, HttpHeaders, SecurityContext, … Read more

Jersey: Print the actual request

If you’re just using Jersey Client API, LoggingFilter (client filter) should help you: Client client = Client.create(); client.addFilter(new LoggingFilter(System.out)); WebResource webResource = client.resource(“http://localhost:9998/”); ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON) .get(ClientResponse.class); Otherwise, you can again log both request and response on server using other LoggingFilter (container filter).