title: Spring: Validate REST controller against XSD schema
body: <p>At the moment I have RestController with the following code</p>
  
  <pre><code>package be.smartask.api;
  
  import be.smartask.api.model.NumberValue;
  import be.smartask.api.model.TextValue;
  import be.smartask.api.model.Translations;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.annotation.*;
  
  
  /**
   * @author Glenn Van Schil
   *         Created on 21/01/2016
   */
  @CrossOrigin
  @RestController
  @RequestMapping(path = "/values")
  public class Controller {
  
      @RequestMapping(method = RequestMethod.POST, headers = "Value-Type=text")
      ResponseEntity&lt;?&gt; createAttribute(@RequestBody TextValue value) {
          System.out.println(value.getCode());
          for (Translations.Translation translation : value.getTranslations().getTranslation()) {
              System.out.println(translation.getLang());
              System.out.println(translation.getValue());
          }
          return new ResponseEntity&lt;&gt;(HttpStatus.CREATED);
      }
  
      @RequestMapping(method = RequestMethod.POST, headers = "Value-Type=number")
      ResponseEntity&lt;?&gt; createAttribute(@RequestBody NumberValue value) {
          System.out.println(value.getMinValue());
          System.out.println(value.getMaxValue());
          return new ResponseEntity&lt;&gt;(HttpStatus.CREATED);
      }
  }
  </code></pre>
  
  <p>This works great, I can post a NumberValue like this:</p>
  
  <pre><code>&lt;numberValue id="id" minValue="0" maxValue="10"/&gt;
  </code></pre>
  
  <p>But when I post a TextValue to NumberValue method</p>
  
  <pre><code>&lt;textvalue code="LUXE"&gt;
      &lt;translations&gt;
          &lt;translation lang="en"&gt;luxury car&lt;/translation&gt;
      &lt;/translations&gt;
  &lt;/textvalue&gt;
  </code></pre>
  
  <p>It still works. It just leaves the minValue and maxValue to 0,0.</p>
  
  <p><strong>My question is: How can i enforce a 400: Bad Request (or something like that) when the body is not exactly the same as defined in my xsd file? By defined I mean the xml tag's name is not the same or a required xs:attribute is missing, ...</strong></p>
  
  <p><em>My xsd file:</em></p>
  
  <pre><code>&lt;xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
             xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  
              &lt;!-- ValueVO --&gt;
      &lt;xs:complexType name="geopoint"&gt;
          &lt;xs:attribute type="xs:double" name="lat"/&gt;
          &lt;xs:attribute type="xs:double" name="lon"/&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="translations"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="translation" minOccurs="0" maxOccurs="unbounded"&gt;
                  &lt;xs:complexType&gt;
                      &lt;xs:simpleContent&gt;
                          &lt;xs:extension base="xs:string"&gt;
                              &lt;xs:attribute type="xs:string" name="lang" use="required"/&gt;
                          &lt;/xs:extension&gt;
                      &lt;/xs:simpleContent&gt;
                  &lt;/xs:complexType&gt;
              &lt;/xs:element&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="value"&gt;
          &lt;xs:attribute type="xs:string" name="id" use="required"/&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="textValue"&gt;
          &lt;xs:complexContent&gt;
              &lt;xs:extension base="value"&gt;
                  &lt;xs:sequence&gt;
                      &lt;xs:element type="translations" name="translations"/&gt;
                  &lt;/xs:sequence&gt;
                  &lt;xs:attribute type="xs:string" name="code" use="required"/&gt;
              &lt;/xs:extension&gt;
          &lt;/xs:complexContent&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="numberValue"&gt;
          &lt;xs:complexContent&gt;
              &lt;xs:extension base="value"&gt;
                  &lt;xs:attribute type="xs:double" name="minValue" use="required"/&gt;
                  &lt;xs:attribute type="xs:double" name="maxValue" use="required"/&gt;
              &lt;/xs:extension&gt;
          &lt;/xs:complexContent&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="dateValue"&gt;
          &lt;xs:complexContent&gt;
              &lt;xs:extension base="value"&gt;
                  &lt;xs:attribute type="xs:date" name="minValue" use="required"/&gt;
                  &lt;xs:attribute type="xs:date" name="maxValue" use="required"/&gt;
              &lt;/xs:extension&gt;
          &lt;/xs:complexContent&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="cityValue"&gt;
          &lt;xs:complexContent&gt;
              &lt;xs:extension base="value"&gt;
                  &lt;xs:sequence&gt;
                      &lt;xs:element type="geopoint" name="geopoint"/&gt;
                      &lt;xs:element type="translations" name="translations"/&gt;
                  &lt;/xs:sequence&gt;
              &lt;/xs:extension&gt;
          &lt;/xs:complexContent&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="timeValue"&gt;
          &lt;xs:complexContent&gt;
              &lt;xs:extension base="value"/&gt;
          &lt;/xs:complexContent&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:element name="value" type="value"/&gt;
      &lt;xs:element name="textValue" type="textValue"/&gt;
      &lt;xs:element name="numberValue" type="numberValue"/&gt;
      &lt;xs:element name="dateValue" type="dateValue"/&gt;
      &lt;xs:element name="cityValue" type="cityValue"/&gt;
      &lt;xs:element name="timeValue" type="timeValue"/&gt;
  
      &lt;xs:element name="values"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element ref="value" maxOccurs="unbounded" minOccurs="0"/&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
      &lt;!-- END ValueVO --&gt; 
  &lt;/xs:schema&gt;
  </code></pre>
  
  <h2>UPDATE</h2>
  
  <p>I added the following to my spring config based on Sheetal Mohan Sharma's answer</p>
  
  <pre><code>&lt;?xml version="1.0" ?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xmlns:p="http://www.springframework.org/schema/p"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  
  
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;context:component-scan base-package="be.smartask.api"/&gt;
  
      &lt;mvc:annotation-driven/&gt;
  
      &lt;bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/&gt;
  
      &lt;bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"&gt;
          &lt;property name="messageConverters"&gt;
              &lt;list&gt;
                  &lt;ref bean="marshallingHttpMessageConverter"/&gt;
                  &lt;bean class="org.springframework.http.converter.StringHttpMessageConverter"/&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&gt;
      &lt;/bean&gt;
      &lt;bean id="marshallingHttpMessageConverter"
            class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"
            p:marshaller-ref="jaxb2Marshaller" p:unmarshaller-ref="jaxb2Marshaller"/&gt;
  
      &lt;bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"&gt;
          &lt;property name="schema" value="classpath:schema/xsd/smartaskRead.xsd"/&gt;
          &lt;property name="classesToBeBound"&gt;
              &lt;list&gt;
                  &lt;value&gt;be.smartask.api.model.NumberValue&lt;/value&gt;
                  &lt;value&gt;be.smartask.api.model.TextValue&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>But NumberValue still accepts TextValue...</p>