Spring Boot XML Schema

XML is still the most sophisticated format for distributed data, when you have complete control between your clients and services JSON is the king, but if not, you probably will need to deal with XML, and there is for good reasons:

  • XML is a W3C standard
  • XML can be validated using Schemas
  • XML embraces extensibility
  • Supports multilingual documents
  • Can embed multiple data types

All those reasons are important, but most important is that we can validate any XML using XSD Schemas, in this technical post we will see how to generate Java classes using XSD eschemas with JAXB2 and Spring Boot. NOTE: If you need to know what tools you need to have installed in your computer in order to create a Spring Boot basic project, please refer my previous post: Spring Boot. Let’s start creating a new Spring Boot project with Webflux as dependency:

spring init --dependencies=webflux --build=maven --language=java spring-boot-xml-schema

Here is the complete pox.xml file generated:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <relativePath/> <!-- lookup parent from repository -->
  <description>This project shows how to use XML schemas in a Spring Boot application</description>





Now, please add JAXB api and Apache Commons Lang3 dependencies to your pox.xml file:


And the JAXB2 Maven Plugin so we can transform our XML schemas in Java classes.


Now, under src/main/resources let’s add a Person.xsd schema

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    <xs:element name="Person" />
    <xs:complexType name="person">
        <xs:element name="firstName" type="xs:string" />
        <xs:element name="lastName" type="xs:string" />
        <xs:element name="address" type="xs:string" />


  • xmlns:xs Is our W3C standard definition
  • elementFormDefault Makes qualifying the name spaces of the elements in XML documents
  • targetNamespace Is is name that better identifies your schema.
  • complexType Group all Person’s attributes
  • sequence element makes sure our attributes stay in order.

This could be a XML representation

<?xml version="1.0" encoding="UTF-8"?>
<person xmlns="urn:com:jos:dem:entities"

    30 Frank Llord,
    Ann Arbor Michigan 48105

Let’s move forward and create a service to get that specific person

package com.jos.dem.springboot.xml.schema.service;

import com.jos.dem.entities.Person;

public interface PersonService {

  Person create();


This is the implementation

package com.jos.dem.springboot.xml.schema.service;

import com.jos.dem.entities.Person;

import org.springframework.stereotype.Service;

public class PersonServiceImpl implements PersonService {

  public Person create(){
    Person person =  new Person();
    person.setAddress("30 Frank Lloyd, Ann Arbor MI 48105");
    return person;


Now, we can use CommandLineRunner to call our service and print out our person entity

package com.jos.dem.springboot.xml.schema;

import org.springframework.context.annotation.Bean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.jos.dem.springboot.xml.schema.service.PersonService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DemoApplication {

  private Logger log = LoggerFactory.getLogger(this.getClass());

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);

  CommandLineRunner start(PersonService personService){
    return args -> {
      log.info("Person: {}", ToStringBuilder.reflectionToString(personService.create()));


The CommandLineRunner is a call back interface in Spring Boot, when Spring Boot starts will call it and pass in args through a run() internal method. Finally, if you execute our Spring Boot application, you should be able to see this output:

2019-11-16 16:06:54.149  INFO 13286 --- [main] ication$$EnhancerBySpringCGLIB$$5c2c9c7e : Person: com.jos.dem.entities.Person@459cfcca[firstName=Jose,lastName=Morales,address=30 Frank Lloyd, Ann Arbor MI 48105]

Finally, let’s test our person service

package com.jos.dem.springboot.xml.schema;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.jos.dem.entities.Person;
import com.jos.dem.springboot.xml.schema.service.PersonService;

class DemoApplicationTests {

  private PersonService personService;

  @DisplayName("Should create person")
  void shouldCreatePerson() {
    Person person = personService.create();

        () -> assertEquals("Jose", person.getFirstName(), "should get first name"),
        () -> assertEquals("Morales", person.getLastName(), "should get last name"),
        () -> assertEquals("30 Frank Lloyd, Ann Arbor MI 48105", person.getAddress(), "should get address")


To browse the project go here, to download the project:

git clone git@github.com:josdem/spring-boot-xml-schema.git

To run the project:

mvn spring-boot:run

To test the project:

mvn test

Return to the main article

comments powered by Disqus