title: Custom HTTP Message Converter Not Being Used, 415 Unsupproted Media Type
body: <p>I am creating a test application to achieve conversion from XML String to Employee object before being passed to the controller. I don't want to use JAXB converter because the purpose is to test Custom HTTP Message Converter which I am going to use in my actual use case that involves XML parsing using SAX parser and some complex rules.</p>
  
  <p>Here are the key steps performed:</p>
  
  <ul>
  <li><strong>Creation of Employee.java Class</strong> : Domain Object</li>
  <li><strong>Creation of EmployeeManagementController.java class</strong> : Spring MVC Controller for Managing Employee</li>
  <li><strong>Creation of EmployeeConverter.java</strong> : Custom Converter for Converting XML String to Employee Object.</li>
  <li><strong>Creation of employee-servlet.xml</strong> : Spring Configuration file</li>
  <li><strong>Creation of web.xml</strong> : The Deployment Descriptor</li>
  </ul>
  
  <p><strong>Employee.java</strong></p>
  
  <pre><code>@Component
  @XmlRootElement(name="employee")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Employee{
  
  @XmlElement(name="name")
  String name;
  @XmlElement(name="designation")
  String designation;
  @XmlElement(name="skill")
  String skill;
  
  public String getName() {
  return name;
  }
  public void setName(String name) {
  this.name = name;
  }
  public String getDesignation() {
  return designation;
  }
  public void setDesignation(String designation) {
  this.designation = designation;
  }
  public String getSkill() {
  return skill;
  }
  public void setSkill(String skill) {
  this.skill = skill;
  }
  
  }
  </code></pre>
  
  <p><strong>EmployeeManagementController.java</strong></p>
  
  <pre><code>@Controller
  @RequestMapping(value="/emp")
  public class EmployeeManagementController {
  
      @RequestMapping(value="/add/employee", method=RequestMethod.POST, consumes="text/html")
      public void addEmployee(@RequestBody Employee employee){
          System.out.println("Employee Name : "+employee.getName());
          System.out.println("Employee Designation : "+employee.getDesignation());
          System.out.println("Employee Skill : "+employee.getSkill());
  
      }
  
  
  }
  </code></pre>
  
  <p><strong>EmployeeConverter.java</strong></p>
  
  <pre><code>@Component
  public class EmployeeConverter extends AbstractHttpMessageConverter&lt;Employee&gt;{
  
      @Override
      protected Employee readInternal(Class&lt;? extends Employee&gt; arg, HttpInputMessage inputMsg) throws IOException,
              HttpMessageNotReadableException {
          // TODO Auto-generated method stub
          Map&lt;String,String&gt; paramMap = getPostParameter(inputMsg);
          BufferedReader file =  new BufferedReader(new StringReader(paramMap.get("xml")));
          Employee employee = null;
          JAXBContext jaxbContext;
          try {
              jaxbContext = JAXBContext.newInstance(Employee.class);
              Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
              employee = (Employee) jaxbUnmarshaller.unmarshal(file);
          } catch (JAXBException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          System.out.println(employee);
          return employee;
      }
  
      @Override
      protected boolean supports(Class&lt;?&gt; type) {
          if(type.getSimpleName().equalsIgnoreCase("Employee")){
              return true;
          }
          return false;
      }
  
      @Override
      protected void writeInternal(Employee arg0, HttpOutputMessage arg1)
              throws IOException, HttpMessageNotWritableException {
          // TODO Auto-generated method stub
  
      }
  
      private Map&lt;String,String&gt; getPostParameter(HttpInputMessage input) throws IOException{
          String payload = null;
          String[] params = null;
          BufferedReader buf = new BufferedReader(new InputStreamReader(input.getBody()));
          Map&lt;String,String&gt; paramMap = new HashMap&lt;String,String&gt;();
  
          String line="";
          while((line = buf.readLine())!=null){
              payload = payload+line;
          }
  
          if(payload.contains("&amp;")){
              params = payload.split("&amp;");
              for(String param : params){
                  paramMap.put(param.split("=")[0],param.split("=")[1]);
              }
          }
  
          return paramMap;
      }
  
  
  }
  </code></pre>
  
  <p><strong>employee-servlet.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&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:util="http://www.springframework.org/schema/util"
                  xsi:schemaLocation="http://www.springframework.org/schema/beans 
                  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                  http://www.springframework.org/schema/context 
                  http://www.springframework.org/schema/context/spring-context-3.1.xsd
                  http://www.springframework.org/schema/mvc
                  http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                  http://www.springframework.org/schema/util
                  http://www.springframework.org/schema/util/spring-util-3.1.xsd"&gt;
  
           &lt;mvc:default-servlet-handler/&gt; 
  
          &lt;context:component-scan base-package="com"/&gt;
  
           &lt;mvc:annotation-driven&gt;
              &lt;mvc:message-converters&gt;
                  &lt;bean class="com.converter.EmployeeConverter"/&gt;             
              &lt;/mvc:message-converters&gt;
          &lt;/mvc:annotation-driven&gt;
  
          &lt;bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"&gt;
              &lt;property name="mediaTypes"&gt;
                  &lt;map&gt;
                      &lt;entry key="json" value="application/json"/&gt;
                      &lt;entry key="xml" value="text/xml"/&gt;
                      &lt;entry key="htm" value="text/html"/&gt;
                  &lt;/map&gt;
              &lt;/property&gt;
              &lt;property name="defaultContentType" value="text/html"/&gt;
          &lt;/bean&gt;
  
          &lt;!-- &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
              &lt;property name="messageConverters"&gt;    
                  &lt;util:list id="beanList"&gt;
                      &lt;ref bean="employeeConverter"/&gt;       
                  &lt;/util:list&gt;
              &lt;/property&gt;
          &lt;/bean&gt;  --&gt;
  
          &lt;!-- &lt;bean id="employeeConverter" class="com.converter.EmployeeConverter"/&gt; --&gt;
  
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"&gt;
    &lt;display-name&gt;TestConverter&lt;/display-name&gt;
    &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;    
    &lt;/welcome-file-list&gt;
  
    &lt;servlet&gt;
      &lt;servlet-name&gt;employee&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;employee&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/employee-servlet.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>When I Use FireFox RestClient I get Response as : <strong><em>415 Unsupproted Media Type.</em></strong></p>
  
  <p>I set the Content-Type and Accept header as text/xml in RestClient and pass the following XML string in the body as parameter:</p>
  
  <pre><code>xml=&lt;employee&gt;&lt;name&gt;Jack&lt;/name&gt;&lt;designation&gt;Account Director&lt;/designation&gt;&lt;skill&gt;Commuication&lt;/skill&gt;&lt;/employee&gt;
  </code></pre>
  
  <p>Can somebody help and let me know what changes are required? I have also tried to use <code>AnnotationMethodHandlerAdapter</code> for registering the message converter.</p>