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 8602169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:00:57+00:00 2026-06-12T02:00:57+00:00

We use Jersey/Jackson for our REST application. Incoming JSON strings get mapped to the

  • 0

We use Jersey/Jackson for our REST application. Incoming JSON strings get mapped to the @Entity objects in the backend by Jackson to be persisted.

The problem arises from the base class that we use for all entities. It has a protected id property, which we want to exchange via REST as well so that when we send an object that has dependencies, hibernate will automatically fetch these dependencies by their ids.

Howevery, Jackson does not access the setter, even if we override it in the subclass to be public. We also tried using @JsonSetter but to no avail. Probably Jackson just looks at the base class and sees ID is not accessible so it skips setting it…

@MappedSuperclass
public abstract class AbstractPersistable<PK extends Serializable> implements Persistable<PK> {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private PK id;

    public PK getId() {
        return id;
    }

    protected void setId(final PK id) {
        this.id = id;
    }

Subclasses:

public class A extends AbstractPersistable<Long> {
    private String name;
}

public class B extends AbstractPersistable<Long> {
    private A a;

    private int value;

    // getter, setter

    // make base class setter accessible
    @Override
    @JsonSetter("id")
    public void setId(Long id) {
        super.setId(id);
    }
}

Now if there are some As in our database and we want to create a new B via the REST resource:

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response create(B b) {
    if (b.getA().getId() == null) 
        cry();
}

with a JSON String like this {"a":{"id":"1","name":"foo"},"value":"123"}.

The incoming B will have the A reference but without an ID.


Is there any way to tell Jackson to either ignore the base class setter or tell it to use the subclass setter instead?

I’ve just found out about @JsonTypeInfo but I’m not sure this is what I need or how to use it.

Thanks for any help!


edit

StaxMan has me worrying I am missing something here, so I’ll add my servlet configuration as this is the only other point I can think of where anything might go wrong, if my described behavior should actually be possible without adding an XML annotation to the setter (as described in my answer):

<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>
        com.sun.jersey.spi.spring.container.servlet.SpringServlet
    </servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>path.to.rest.resources</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
  • 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-12T02:00:58+00:00Added an answer on June 12, 2026 at 2:00 am

    So, found the solution already:

    Have to use @XmlElement(name="id") on all overriding ID setters:

    @XmlElement(name="id")
    public void setId(Long id) {
        super.setId(id);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use jersey-client to consume a REST service. I need both the requested Entity
I'm building REST API with Java (JAX-RS) using Jersey and I use Jackson as
I understand that if you use SUN's Jersey REST implementation framework, you get the
I am looking to use Jersey without the need of installing an application server
I'm using Jersey (jax-rs), to build a REST rich application. Everything is great, but
Does Jackson with Jersey support polymorphic classes over JSON? Let's say, for instance, that
Has anybody managed to use jackson 2.0 with Jersey 1.12. It will be very
I'm trying to use Jersey as a client for a RESTful application. Specifically I'd
I am trying to use the Jersey client API to consume a third-party REST
I am trying to use extjs store to talk to a Jersey rest Java

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.