Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

JaxbAnnotationIntrospector creates an element from @XmlAttribute instead of an attribute #6

Closed
rynam0 opened this issue May 17, 2012 · 9 comments

Comments

@rynam0
Copy link

rynam0 commented May 17, 2012

Expected: <Individual identifier="1"><givenName>Jay</givenName><surName>Unit</surName></Individual>
Actual: <Individual xmlns=""><identifier>1</identifier><givenName>Jay</givenName><surName>Unit</surName></Individual>

package com.example.tests;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
import org.junit.Test;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import static junit.framework.Assert.assertEquals;

public class PersonTest {

@XmlRootElement(name = "Individual")
class MyPerson {

    @XmlAttribute(name = "identifier")
    private Long id;

    @XmlElement(name = "givenName")
    private String firstName;

    @XmlElement(name = "surName")
    private String lastName;

    public Long getId() {
        return id;
    }
    public void setId(final Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(final String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }
    public void setLastName(final String lastName) {
        this.lastName = lastName;
    }
}


@Test
public void testPersonAsXml() throws Exception {
    MyPerson person = new MyPerson();
    person.setId(1L);
    person.setFirstName("Jay");
    person.setLastName("Unit");

    XmlMapper mapper = new XmlMapper();
    mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector());
    String actual = mapper.writeValueAsString(person);
    System.out.println("Person: " + actual);

    String expected = "<Individual identifier=\"1\"><givenName>Jay</givenName><surName>Unit</surName></Individual>";
    assertEquals(expected, actual);
}

}

@cowtowncoder
Copy link
Member

I can not reproduce this with version 2.0.2 (added unit test). One difference is that I do not see that extra namespace declaration; could be because tests use Woodstox Stax implementation (not the JDK 1.6 default Sjsxp).

@rynam0
Copy link
Author

rynam0 commented Jul 27, 2012

I am running this as a maven project and it would appear stax-api is coming down w/ the jackson dependency. Might you try running this in maven to see if you can reproduce it there?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>jackson-jaxb-poc</groupId>
  <artifactId>jackson-jaxb-poc</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-xml</artifactId>
      <version>2.0.2</version>
      <scope>compile</scope>
    </dependency>

  </dependencies>

</project>

@cowtowncoder
Copy link
Member

I will try it.

@geoffrr
Copy link

geoffrr commented Sep 14, 2012

Is this issue still active?

I am experiencing similar behaviour, on a very small maven module based on Java 6, using Jackson version 2.0.5 libraries.

Serialized through JAXB:

    <jurisdiction name="Aversan" messageStandard="CeRx"/>

Serialized through Jackson:

  <jurisdiction>
    <name>Aversan</name>
    <messageStandard>CeRx</messageStandard>
  </jurisdiction>

Object:

@XmlAccessorType(value = XmlAccessType.FIELD)
public class Jurisdiction {

    @XmlAttribute(name="name",required=true)
    private String name;
    @XmlAttribute(name="messageStandard", required=true)
    private String messageStandard;
...

Jackson mapping code:

    @Test
    public void serializeXML() throws Exception {

        ObjectMapper mapper = new XmlMapper();
        mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector());
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        logger.info(mapper.writeValueAsString(constructSampleScenario()));

    }

Maven dependencies:

    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
            <version>2.0.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.0.5</version>
            <scope>test</scope>
        </dependency>

        <dependency>
          <groupId>org.codehaus.woodstox</groupId>
          <artifactId>woodstox-core-asl</artifactId>
          <version>4.1.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

Thanks for the awesome library!

@cowtowncoder
Copy link
Member

Thank you for code sample: I will try to reproduce this with 2.1.0-SNAPSHOT.

@cowtowncoder
Copy link
Member

Ok, @geoffrr, I can not reproduce this issue with 2.1.0-SNAPSHOT; for me attribute annotation works with both Jackson and JAXB annotations. I can try to see if I can reproduce it with 2.0.5...

@cowtowncoder
Copy link
Member

Can not reproduce with 2.0.5 either.

@cowtowncoder
Copy link
Member

Can not reproduce, closing. Please file a new issue with unit test (and version), if still problematic with 2.1.1 or above.

@cowtowncoder
Copy link
Member

Ok: as per #6 I finally understand the issue, and this was reproducible.

The issue is now solved for 2.4 (not yet released); but with earlier version, the work-around is to use XmlJaxbAnnotationIntrospector and NOT vanilla JaxbAnnotationIntrospector.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants