How do you map multiple query parameters to the fields of a bean on Jersey GET request?

In Jersey 2.0, you’ll want to use BeanParam to seamlessly provide what you’re looking for in the normal Jersey style. From the above linked doc page, you can use BeanParam to do something like: @GET @Path(“find”) @Produces(MediaType.APPLICATION_XML) public FindResponse find(@BeanParam ParameterBean paramBean) { String prop1 = paramBean.prop1; String prop2 = paramBean.prop2; String prop3 = paramBean.prop3; … Read more

How can I customize serialization of a list of JAXB objects to JSON?

I found a solution: replace the JAXB JSON serializer with a better behaved JSON serializer like Jackson. The easy way is to use jackson-jaxrs, which has already done it for you. The class is JacksonJsonProvider. All you have to do is edit your project’s web.xml so that Jersey (or another JAX-RS implementation) scans for it. … Read more

A message body writer for Java class not found

I finally found my answer. I added <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.8</version> </dependency> to my pom.xml file. Then I added <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> to my web.xml file, and everything works fine. No change was required to my code above.

Could not serialize object cause of HibernateProxy

You can do without manually unproxying everything by using a custom TypeAdapter. Something along these lines: /** * This TypeAdapter unproxies Hibernate proxied objects, and serializes them * through the registered (or default) TypeAdapter of the base class. */ public class HibernateProxyTypeAdapter extends TypeAdapter<HibernateProxy> { public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() { @Override … Read more

Staying DRY with JAX-RS

Here is a workaround I’m using: Define a constructor for the BaseService with ‘id’ and ‘xyz’ as params: // BaseService.java public abstract class BaseService { // JAX-RS injected fields protected final String id; protected final String xyz; public BaseService (String id, String xyz) { this.id = id; this.xyz = xyz; } } Repeat the constructor … Read more

Practical advice on using Jersey and Guice for RESTful service

Guice integration with Jersey has not stagnated. The opposite is true. Thanks to Paul and his cohorts behind Jersey, the latest 1.7 release contains a special JerseyServletModule class to work with Guice-based servlets. Guice-based constructor injection into JAX-RS resource works! The issue is using JAX-RS annotations such as @QueryParam in the constructor of a JAX-RS … Read more