我通过@ResponseBody 遇到了Spring MVC 和XML 响应问题。这是我的 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 应用程序中,我想向调度程序 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,Web 浏览器都会显示下一条消息: 此请求标识的资源只能生成具有根据请求“接受”标头不可接受的特征的响应。 任何人都可以帮助解决我的错误。