How to make Jersey work with Dagger dependency injection?

You shouldn’t think of it as “how to integrate dagger with jersey”. Figure out how to setup jersey, then once you have that figured out, then you can worry about using dagger. Here’s (very roughly) how I would do it. Create your own implementation of the ResourceConfig class. @ApplicationPath(“/service”) public class MyResourceConfig extends ResourceConfig { … Read more

Jersey/JAX-RS : How to cascade beans-validation recursively with @Valid automatically?

Actually, according to the specification, adding @Valid is exactly for this usecase. From the JSR 303 specification: In addition to supporting instance validation, validation of graphs of object is also supported. The result of a graph validation is returned as a unified set of constraint violations. Consider the situation where bean X contains a field … Read more

User authentication on a Jersey REST service

I’m sucessfully using spring security for securing my Jersey-based API. It has pluggable authentication schemes allowing you to switch from Basic Auth to something else later. I’m not using Spring in general, just the security stuff. Here is the relevant part from my web.xml <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/security-applicationContext.xml, /WEB-INF/applicationContext.xml </param-value> … Read more

How do I use the Jersey JSON POJO support?

You can use @XmlRootElement if you want to use JAXB annotations (see other answers). However, if you prefer pure POJO mapping, you must do the following (Unfortunately it isn’t written in docs): Add jackson*.jar to your classpath (As stated by @Vitali Bichov); In web.xml, if you’re using com.sun.jersey.config.property.packages init parameter, add org.codehaus.jackson.jaxrs to the list. … Read more

Jersey Exception : SEVERE: A message body reader for Java class

To make it work you only need two things. Check if you are missing: First of all, you need @XmlRootElement annotation for your object class/entity. You need to add dependency for jersey-json if you are missing.For your reference I added this dependency into my pom.xml. <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.17.1</version> </dependency>

How should I log uncaught exceptions in my RESTful JAX-RS web service?

For lack of a better way to implement logging for uncaught JAX-RS exceptions, using a catch-all ExceptionMapper as in Other Ideas: #1 seems to be the cleanest, simplest way to add this functionality. Here’s my implementation: @Provider public class ThrowableExceptionMapper implements ExceptionMapper<Throwable> { private static final Logger log = Logger.getLogger(ThrowableExceptionMapper.class); @Context HttpServletRequest request; @Override public … Read more

How to get jersey logs at server?

If you want to turn on logging on the server side, you need to register the LoggingFilter Jersey filter (on the container side). This filter will log request/response headers and entities. Here’s what you need to add to your ResourceConfig class: @ApplicationPath(“https://stackoverflow.com/”) public class MyApplication extends ResourceConfig { public MyApplication() { // Resources. packages(MyResource.class.getPackage().getName()); register(LoggingFilter.class); … Read more

Listing all deployed rest endpoints (spring-boot, jersey)

Probably the best way to do this, is to use an ApplicationEventListener. From there you can listen for the “application finished initializing” event, and get the ResourceModel from the ApplicationEvent. The ResourceModel will have all the initialized Resources. Then you can traverse the Resource as others have mentioned. Below is an implementation. Some of the … Read more