title: Accept SOAP message in RestController using JAXB
body: <p>We created a Spring Boot @RestController that will accept XML request using SOAP but I could not get it to work.</p>
  
  <p>Sample Message:</p>
  
  <pre><code>&lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;soap:Body&gt;
      &lt;PostPayload xmlns="http://mynamespace/"&gt;
        &lt;Message&gt;
          &lt;Header type="Main" version="1.0" desc="Some header description..."&gt;
            &lt;Title content="The quick brown fox." /&gt;
          &lt;/Header&gt;
          &lt;Content /&gt;
          &lt;Footer type="Copyright" name="Footer01"&gt;Some random content...&lt;/Footer&gt;
        &lt;/Message&gt;
      &lt;/PostPayload&gt;
    &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>Rest Controller:</p>
  
  <pre><code>@RestController
  @RequestMapping("rest-service")
  public class MessageController {
  
      @PostMapping(consumes = MediaType.APPLICATION_XML_VALUE)
      public String handler(@RequestBody Message message) {
          System.out.println(message);
          System.out.println("\n\n\n");
  
          try {
              JAXBContext jaxbContext = JAXBContext.newInstance(Message.class);
              Marshaller marshaller = jaxbContext.createMarshaller();
              marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
              marshaller.marshal(message, System.out);
          } catch(JAXBException ex) {
              System.out.println(ex.toString());
          }
  
          return "Done!";
      }
  
  }
  </code></pre>
  
  <p>Message Class</p>
  
  <pre><code>@XmlAccessorType(XmlAccessType.FIELD)
  @XmlRootElement(name = "Message")
  public class Message {
     @XmlElement(name = "Header", required = true)
     private Header header;
  
     @XmlElement(name = "Content", required = true)
     private Content content;
  
     @XmlElement(name = "Footer", required = true)
     private Footer footer;
  
     // Getters and setters here...
  
     @Override
     public String toString() {
        // In here, I outputted the values of the header and footer.
     }
  }
  </code></pre>
  
  <p>Header Class</p>
  
  <pre><code>@XmlRootElement(name = "Header")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Header {
      @XmlAttribute(name = "type", required = true)
      private String type;
  
      @XmlAttribute(name = "version", required = true)
      private String version;
  
      @XmlAttribute(name = "desc", required = true)
      private String description;
  
      // Getters and setters here...
  }
  </code></pre>
  
  <p>Content Class</p>
  
  <pre><code>@XmlRootElement(name = "Content")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Content {
  }
  </code></pre>
  
  <p>Footer Class</p>
  
  <pre><code>@XmlRootElement(name = "Footer")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Footer {
      @XmlValue
      private String value;
  
      @XmlAttribute(name = "name")
      private String name;
  
      @XmlAttribute(name = "type")
      private String type;
  
      //Getter and setters here...
  }
  </code></pre>
  
  <p>So if I send that soap message to my rest service, it is returning null. The marshaller in the rest controller is just to check if the output is the same with the request, that means that I'm reading the XML properly. Unfortunately, it is returning null. Any thoughts why is this?</p>