1

I have a problem with Spring MVC and XML Response via @ResponseBody. Here is my web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
       version="2.5">


<filter>
    <filter-name>FormEncodingSetterFilter</filter-name>
    <filter-class>ua.yura.controllers.EncodingFilter.BKIFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>FormEncodingSetterFilter</filter-name>
    <url-pattern>*.htm</url-pattern>
</filter-mapping>

<servlet>
    <servlet-name>bki</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>    

<servlet-mapping>
    <servlet-name>bki</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

This is my bki-servlet.xml:

<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.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<context:component-scan base-package="ua.yura.controllers"/>

<mvc:annotation-driven/>

<mvc:resources mapping="/img/**" location="/imgs/"/>

and XMLController class:

@Controller
@RequestMapping( value = "/xml" )
    public class XMLController {
        @RequestMapping( value = "/test.htm", headers = "accept=application/xml")
        @ResponseBody
        public Client test() {
            System.out.println("aaaa");
            Client client = new Client(-1,"1","1","1","1","1","1","1");
            return client;
        }
}

class Client with annotation @XMLRoot and @XMLElement. In my another java application i want to send request to dispatcher servlet and get back XML data with Client object. I tried to do next:

URL my = new URL("http://localhost:8080/xml/test.htm");
        HttpURLConnection yc = (HttpURLConnection)my.openConnection();
        yc.addRequestProperty("accept", "application/xml");

        //yc.setRequestMethod("GET");
        BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream() ));
        String inputLine;

        while( (inputLine = in.readLine()) != null)
            System.out.println( inputLine);
        in.close();

but i every time get Error 406, web browser show next message: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers. Can anybody help with my errors.

  • 1
    I think the header should be `Accept` not `accept` Ref: http://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#z3 – Arun P Johny Mar 25 '13 at 07:34
  • [Field names are case-insensitive](http://stackoverflow.com/questions/5258977/are-http-headers-case-sensitive) – andyb Mar 25 '13 at 08:36
  • I've tried that too, but Tomcat in error description write "request "accept" headers", i think the problem is another. – user2206563 Mar 25 '13 at 11:59

0 Answers0