Spring和XML响应

时间:2013-03-25 07:31:37

标签: java xml spring

我通过@ResponseBody遇到了Spring MVC和XML Response的问题。 这是我的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>

这是我的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/"/>

和XMLController类:

@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;
        }
}

具有注释@XMLRoot和@XMLElement的类客户端。在我的另一个java应用程序中,我想向dispatcher servlet发送请求并使用Client对象获取XML数据。我试着做下一个:

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();

但我每次都得到错误406,网页浏览器显示下一条消息: 此请求标识的资源只能根据请求“accept”标头生成具有不可接受特征的响应。 任何人都可以帮我解决我的错误。

0 个答案:

没有答案