Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8191659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:58:02+00:00 2026-06-07T03:58:02+00:00

I am using Jersey to implement JAX-RS REST-style services along with Jackson 2.0.2 for

  • 0

I am using Jersey to implement JAX-RS REST-style services along with Jackson 2.0.2 for the JSON mapping. One of these REST services returns a List<EntityA> (let’s call it indexA) where EntityA contains another List<EntityB> whereas another service just returns a List<EntityB> (let’s call it indexB):

@Entity
@JsonAutoDetect
public class EntityA {
  @Id
  private String id;

  @OneToMany
  private List<EntityB> b;

  ...
}

@Entity
@JsonAutoDetect
@JsonFilter("bFilter")
public class EntityB {
  @Id
  private String id;

  private String some;
  private String other;
  private String attributes;

  ...
}

@Path("/a")
public class AResource {

  @GET
  @Path("/")
  public List<EntityA> indexA() {
    ...
  }
}

@Path("/b")
public class BResource {

  @GET
  @Path("/")
  public List<EntityB> indexB() {
    ...
  }
}

What I’d like to achieve is to apply a Jackson filter to the indexA invocation so that not all attributes of the child EntityB elements are serialized. OTOH, indexB should return EntityB in its completeness.

I am aware of the existence of a ContextResolver<ObjectMapper>, which I am already using for other purposes. Unfortunately, for the ContextResolver it seems to be impossible to distinguish both service invocations as the Class supplied to ContextResolver.getContext(Class) is ArrayList in both cases (and thanks to type erasure I cannot figure out the generic type parameters).

Are there any hooks better suited at configuring an ObjectMapper/FilterProvider depending on the entity type that is being mapped?

I could use the approach proposed in How to return a partial JSON response using Java?: Manually mapping to a String, but that kills the whole beauty of a declarative annotation-based approach, so I’d like to avoid this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:58:02+00:00Added an answer on June 7, 2026 at 3:58 am

    I was in the same situation, after tons of research, I figured it out, the solution is to use @JsonView and Spring which can inject an ObjectMapper into the JSON Writer without killing the beauty of Jersey.

    I am working on a set of REST APIs, I want to get a list of instances of SystemObject and the detail a specific instance of SystemObject, just like you I just want very limited of number of properties of each instance in the list and some additional properties in the detail, I just define Views for them, and add annotation in the SystemObject class. but by default, all properties with no @JsonView annotation will be output to the JSON, but there is a configuration item(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION) I can use to exclude them.

    The problem is that I have to set it to true to meet my need. but I can not change the ObjectMapper which does the magic to convert the object to JSON, by reading the 3 articles below, I got the idea that the only way I can do is to inject a Modified ObjectMapper to Jersey.
    Now I got what I want.

    It is like you create multiple views against a database table.

    These 3 links will help you in different ways:

    How to create a ObjectMapperProvider which can be used by Spring to inject

    Jersey, Jackson, Spring and JSON

    Jersey + Spring integration example

    REST resource:

    package com.john.rest.resource;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.ws.rs.GET;
    import javax.ws.rs.HeaderParam;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.WebApplicationException;
    import javax.ws.rs.core.Context;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Request;
    import javax.ws.rs.core.UriInfo;
    
    import org.codehaus.jackson.map.annotate.JsonView;
    import org.springframework.stereotype.Component;
    
    import com.midtronics.esp.common.EspException;
    import com.midtronics.esp.common.SystemObject;
    import com.midtronics.esp.mobile.model.SystemObjectView;
    import com.midtronics.esp.model.accesscontrol.AccessControlBean;
    import com.midtronics.esp.model.site.SiteBean;
    
    @Component
    @Path("/hierarchy")
    public class Hierarchy {
    
        // Allows to insert contextual objects into the class, 
        // e.g. ServletContext, Request, Response, UriInfo
        @Context
        UriInfo uriInfo;
    
        @Context
        Request request;
    
        // Return the list of sites
        @GET
        @Path("sites")
        @Produces(MediaType.APPLICATION_JSON)
        @JsonView({SystemObjectView.ObjectList.class})
        public List<SystemObject> listSite(
                @HeaderParam("userId") String userId, 
                @HeaderParam("password") String password) {
            ArrayList<SystemObject> sites= new ArrayList<SystemObject>();
    
            try{
                if(!AccessControlBean.CheckUser(userId, password)){
                    throw new WebApplicationException(401);
                }
                SystemObject.GetSiteListByPage(sites, 2, 3);
    
                return sites;
            } catch(EspException e){
                throw new WebApplicationException(401);
            } catch (Exception e) {
                throw new WebApplicationException(500);
            }
        }
    
        // Return the number of sites
        @GET
        @Path("sites/total")
        @Produces(MediaType.TEXT_PLAIN)
        public String getSiteNumber(@HeaderParam("userId") String userId, 
                @HeaderParam("password") String password) {
            try{
                return Integer.toString(SiteBean.GetSiteTotal()); 
            } catch(EspException e){
                throw new WebApplicationException(401);
            } catch (Exception e) {
                throw new WebApplicationException(500);
            }
        }
    
    }
    

    REST model:

    package com.john.rest.model;
    
    import java.io.Serializable;
    import java.util.ArrayList;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    import org.codehaus.jackson.annotate.JsonIgnore;
    import org.codehaus.jackson.annotate.JsonProperty;
    import org.codehaus.jackson.map.annotate.JsonView;
    
    import com.midtronics.esp.mobile.model.SystemObjectView;
    import com.midtronics.esp.model.common.ICommonDAO;
    
    @XmlRootElement
    public class SystemObject implements Serializable
    {
        private static final long serialVersionUID = 3989499187492868996L;
    
        @JsonProperty("id")
        @JsonView({SystemObjectView.ObjectList.class, SystemObjectView.ObjectDetail.class})
        protected String objectID = "";
    
        @JsonProperty("parentId")
        protected String parentID = "";
    
        @JsonProperty("name")
        @JsonView({SystemObjectView.ObjectList.class, SystemObjectView.ObjectDetail.class})
        protected String objectName = "";
    
        //getters...
        //setters...
    
    }
    

    REST model view:

    package com.john.rest.model;
    
    public class SystemObjectView {
        public static class ObjectList { };
    
        public static class ObjectDetail extends ObjectList { }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JAX-RS REST service implemented using Jersey. One of the cool features
Using Jersey and Jackson to create a REST interface, how do I get List
I am using Jersey/Java to develop my REST services. I need to return an
I'm just getting acquainted with implementing REST web services in Java using JAX-RS and
I'm using Jersey (jax-rs), to build a REST rich application. Everything is great, but
I'm trying to implement REST API using Jersey with Spring on Tomcat but I'm
I'm using jersey API for some REST web services with apache Tomcat. I need
I'm trying to create REST services using Jersey (builtin) on Glassfish. I installed GlassFish
Possible Duplicate: SQL Query with MySQL I implement a Rest Web Service (JAX-RS) using
I'm using Jersey's integrated Jackson processing to transform incoming JSON to a POJO, e.g.:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.