URL matrix parameters vs. query parameters

The important difference is that matrix parameters apply to a particular path element while query parameters apply to the request as a whole. This comes into play when making a complex REST-style query to multiple levels of resources and sub-resources: http://example.com/res/categories;name=foo/objects;name=green/?page=1 It really comes down to namespacing. Note: The ‘levels’ of resources here are categories … Read more

Combining Spring project and Jersey

NOTE: The linked example projects below are from the Jersey master branch, which is currently a snapshot of Jersey 3, which is not yet released. Jersey 3 will be using Spring 4, so you may notice a dependency jersey-spring4. This dependency does not exist yet, as Jersey 3 is not yet released (probably not for … Read more

Servlet vs RESTful

RESTful is more an architecture style than a different technology. In server perspective, it is designed to be entirely stateless and self-contained on a per-request basis (i.e. there are no sessions). In client perspective, it’s more a way of getting information in different formats via URLs with (self-documenting) path parameters instead of request parameters. Surely … Read more

How to set up JAX-RS Application using annotations only (no web.xml)?

** PLEASE READ IF YOU USE TOMCAT OR JETTY! ** The accepted answer does work, but only if the webapp is deployed to an app server like Glassfish or Wildfly, and possibly servlet containers with EE extensions like TomEE. It doesn’t work on standard servlet containers like Tomcat, which I’m sure most people looking for … Read more

JAX-RS / Jersey how to customize error handling?

There are several approaches to customize the error handling behavior with JAX-RS. Here are three of the easier ways. The first approach is to create an Exception class that extends WebApplicationException. Example: public class NotAuthorizedException extends WebApplicationException { public NotAuthorizedException(String message) { super(Response.status(Response.Status.UNAUTHORIZED) .entity(message).type(MediaType.TEXT_PLAIN).build()); } } And to throw this newly create Exception you simply: … 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

When to use @QueryParam vs @PathParam

REST may not be a standard as such, but reading up on general REST documentation and blog posts should give you some guidelines for a good way to structure API URLs. Most rest APIs tend to only have resource names and resource IDs in the path. Such as: /departments/{dept}/employees/{id} Some REST APIs use query strings … Read more

Inject an EJB into JAX-RS (RESTful service)

I am not sure this is supposed to work. So either: Option 1: Use the injection provider SPI Implement a provider that will do the lookup and inject the EJB. See: @EJB injection. Example for com.sun.jersey:jersey-server:1.17 : import com.sun.jersey.core.spi.component.ComponentContext; import com.sun.jersey.core.spi.component.ComponentScope; import com.sun.jersey.spi.inject.Injectable; import com.sun.jersey.spi.inject.InjectableProvider; import javax.ejb.EJB; import javax.naming.Context; import javax.naming.InitialContext; import javax.ws.rs.ext.Provider; import java.lang.reflect.Type; … Read more

File upload along with other object in Jersey restful web service

You can’t have two Content-Types (well technically that’s what we’re doing below, but they are separated with each part of the multipart, but the main type is multipart). That’s basically what you are expecting with your method. You are expecting mutlipart and json together as the main media type. The Employee data needs to be … Read more

Java 8 LocalDate Jackson format

I was never able to get this to work simple using annotations. To get it to work, I created a ContextResolver for ObjectMapper, then I added the JSR310Module (update: now it is JavaTimeModule instead), along with one more caveat, which was the need to set write-date-as-timestamp to false. See more at the documentation for the … Read more

tech