users@jersey.java.net

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

From: Norbert Kiesel <nkiesel_at_metricstream.com>
Date: Wed, 23 Oct 2013 11:49:09 -0700

Update on this: all actually works as expected as long as I just request
a single Task (i.e. passing the task_id as shown below): I now also get
the <myAttr>myAttr1</myAttr> in the response.

However, when I marshall a List<Task>, I just get the tasks w/o the
additional attribute. Is there something special about collections I
have to do to make this work as well?

Best,
   Norbert

On 10/22/2013 11:08 AM, Norbert Kiesel wrote:
> 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_at_metricstream.com | 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
>


-- 
Norbert Kiesel
Systems Architect | Engineering
MetricStream
2600 E. Bayshore Road | Palo Alto, CA - 94303
+1-650-620-2954 | nkiesel_at_metricstream.com | www.metricstream.com