Volodymyr Rudyi
Volodymyr Rudyi

Reputation: 648

One set of JAXB classes for two equal namespaces

I'm consuming data from Web-service that has two endpoints - one is test endpoint and another one is production(I can't modify this). Problem is that production and test endpoints have different "xmlns" in the root element. So if XSD works for test, then JAXB fails to parse prod responses and vice versa.

Data is consumed using Spring RestTemplate and Jaxb2Marshaller.

Example document fragment for test:

<element xmlns="https://example.com/test"

Example document fragment for prod:

<element xmlns="https://example.com/prod"

What is the correct way to allow consuming data from both test/prod endpoints?

Upvotes: 2

Views: 257

Answers (1)

artbristol
artbristol

Reputation: 32397

First, complain about the insanity of having different namespaces for test and production.

Then check http://blog.bdoughan.com/2012/11/applying-namespace-during-jaxb-unmarshal.html.

Generate the schema from the production XSD. For the test endpoint, you need to configure the JAXBContext that the RestTemplate is using (for both marshalling and unmarshalling), by adding a custom org.springframework.http.converter.xml.MarshallingHttpMessageConverter.

Your custom NamespaceFilter's startElement and endElement methods should check for the namespace that you're trying to replace and replace it if necessary.

Upvotes: 1

Related Questions