Question

We recently upgraded to GlassFish Jersey 2.7 from the Sun Jersey implementation. When we did this Jackson started trying to deserilize our Domain objects. Which in our case isn't what we want. We have Domain setup so that domain objects are never directly sent out via the web services. Everything is transformed in to a DTO and then sent out. So basically we just call

return Response.ok(new SomeFooBarDTOObject(someFooBarDomainObject)).build()

and this use to work with the previous version of Jersey/Jackson. With the more up to date code we're getting errors like the following:

Conflicting setter definitions for property "preferredItem": com.foo.domain.FooBar#setPreferredItem(1 params) vs com.foo.domain.FooBar#setPreferredItem(1 params)

Even though we never try to send out the actual domain object. How do we tell Jackson to only look at objects that are actually received or sent via a web service.

Here is the web service that is causing the issue I've simplified it down to just this. If I take out the RestaurantItemDTO restaurantItemDTO from the method parameters it works, but if I keep it in there it doesn't.

RestaurantItemDTO only has base types in it's fields. The only way it references a domain object is through a constructor parameter, and there is also a public no parameter constructor as well. So it's not the only constructor.

@Controller
@Path("/restaurant/restaurantItem")
public class RestaurantItemWebService {
    @PUT
    @Path("/ordersheets/{ordersheetId}/{locationId}/restaurantItems/{restaurantItemId}")
    @Produces(value = MediaType.APPLICATION_JSON)
    @Consumes(value = MediaType.APPLICATION_JSON)
    @Transactional
    @PreAuthorize("hasRole('" + PermissionNames.SOME_PERMISSION+ "')")
    public Response saveExistingRestaurantItem(@PathParam("ordersheetId") Long orderSheetID, @PathParam("restaurantItemId") Long restaurantItemID, RestaurantItemDTO restaurantItemDTO) {
        return Response.ok(restaurantItemDTO).build();
    }
Was it helpful?

Solution

Well I wasn't able to figure out why Jackson behaved the way it did, but I did find that the issue was that I had a constructor for the DTO that referenced the domain object, and even though Jackson should have never called that constructor since there was a default public parameterless constructor available, it still tried to map the domain object. I got around this but using @JsonIgnore on the constructor that referenced the domain object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top