2

I'm using springfox 3.0.0 and my XML Swagger looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Foo>
    <anotherBar>
        <MyStr>string</MyStr>
    </anotherBar>
    <bar>
        <MyStr>string</MyStr>
    </bar>
    <MyDate>2021-10-08T16:08:57.795Z</MyDate>
</Foo>

Here is what I would like:

<?xml version="1.0" encoding="UTF-8"?>
<Foo>
    <AnotherBar>
        <MyStr>string</MyStr>
    </AnotherBar>
    <Bar>
        <MyStr>string</MyStr>
    </Bar>
    <MyDate>2021-10-08T16:08:57.795Z</MyDate>
</Foo>

As you can see, MyStr and MyDate are displayed correctly in the XML but not bar and anotherBar without the uppercase.

I don't know why the @XmlElement annotation is ignored by Springfox for nested objects only. But it works great with String or Instant.

Here is my Foo class:

@XmlRootElement(name = "Foo")
public class Foo {
    @XmlElement(name = "MyDate")
    private Instant myDate;

    @XmlElement(name = "Bar")
    private Bar bar;

    @XmlElement(name = "AnotherBar")
    private Bar anotherBar;

    // Getter / Setter...
}

And here is my Bar class:

public class Bar {

    @XmlElement(name = "MyStr")
    private String myStr;

    // Getter / Setter ...

}

Here is my XmlMapper config:

XmlMapper xmlMapper = new XmlMapper();
xmlMapper.registerModule(new JaxbAnnotationModule());

I already tried to replace @XmlElement with @JacksonXmlProperty but it's the same result.

I found also this answer https://stackoverflow.com/a/64562167 but I don't want to use @JsonProperty since I need the JSON from Swagger to use attributes name.

Do you know why @XmlElement is ignored by Springfox in this case only?

JsonMraz
  • 109
  • 2
  • 10
  • What happens if you add `@XmlAccessorType(XmlAccessType.NONE)` to the class `Foo` ? With that annotation only fields actually annotated will be considered. – k5_ Oct 13 '21 at 19:33
  • @k5_ I tried to add `@XmlAccessorType(XmlAccessType.NONE)` to the class `Foo` but it doesn't change anything. – JsonMraz Oct 14 '21 at 08:14
  • 1
    `Springfox` is dead, use `springdoc-openapi` instead. We migrated and a lot of long unsolved problems disappeared. Try it and if your problem persist, solve it only after. See e.g. here: https://stackoverflow.com/q/58953513/2886891 – Honza Zidek Oct 14 '21 at 18:22
  • 1
    @honza-zidek I also tried with springdoc instead of springfox. There is also the exact same thing. However, the `@Schema` annotation works to rename a field with springdoc, but I want to rename only the field for the XML media type and not the JSON. And the `@Schema` annotation renames both in the same time. – JsonMraz Oct 18 '21 at 08:51

0 Answers0