users@jersey.java.net

[Jersey] [Jersey 2.x] Still struggling with inheritance

From: Norbert Kiesel <nkiesel_at_metricstream.com>
Date: Tue, 22 Oct 2013 11:08:03 -0700

Hi all,

I has what I thought was a very simple problem but I can't seem to make it work. Here is the scenario: I have a base class (e.g. Task) and a derived class (e.g. MyTask). Only difference is that MyTask has 1 additional attribute (e.g. `private String myAttr;`) with public getter and setter. What I want it that marshalling of MyTask shows that additional attribute. However, that does not happen. Instead I just get all the attributes from Task. I also don't require right now to be able to unmarshall these tasks is that makes a difference.

Below is an abbrevated copy of my test code and the generated output.

@XmlRootElement(name = "task")
public class Task {
  private String name;
  public String getName() { return name; }
  public void setName(String name) { this.name = name; }
}

---------------

@XmlRootElement(name = "task")
public class MyTask extends Task {
  private String myAttr;
  public String getMyAttr() { return myAttr; }
  public void setMyAttr(String myAttr) { this.myAttr = myAttr; }
  public MyTask(String name, String myAttr) { setName(name); setMyAttr(myAttr); }
}

--------------------

@Path("{user}/tasks")
public class TasksResource {
    @GET
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    @Path("{task_id}")
    public Task getTask(@PathParam("task_id") String taskId) {
        return new MyTask("name1", "myAttr1");
    }
}

----------------------

<task>
  <name>name1</name>
</task>

----------------------


--
Norbert Kiesel
Systems Architect | Engineering
MetricStream
2600 E. Bayshore Road | Palo Alto, CA - 94303
+1-650-620-2954 | nkiesel@metricstream.com<mailto:nkiesel@metricstream.com> | www.metricstream.com<http://www.metricstream.com>
Confidentiality Notice:This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited